# Move Towards A Point In Room, Then Stop?

9 replies to this topic

### #1 hanyunanodesudc

hanyunanodesudc

GMC Member

• GMC Member
• 363 posts

Posted 29 March 2009 - 11:59 PM

This thing can be anywhere, but once it reaches some thing (let's say it reaches 0 hp or something< this doesnt matter),
it will move to a point in the room (in my case, 224,96).
I don't want it to jump to that point, but to slowly move to that point.
When I do move_towards (224,96), it goes there, but continues in the direction and passes it.
Friction didn't work since it was too inaccurate,
and when I did "if self is at point 224,96, then stop", it somehow didn't work, and it seemed like
the obj wasn't even moving in the correct direction. Or if it did, it kept stopping at weird places.
how do you fix this or do this...?

Edited by hanyunanodesudc, 30 March 2009 - 02:15 AM.

• 0

### #2 joethedumblr

joethedumblr

GMC Member

• New Member
• 253 posts

Posted 30 March 2009 - 12:06 AM

did you try putting move towards point in the step event?

Edited by joethedumblr, 30 March 2009 - 12:07 AM.

• 0

### #3 hanyunanodesudc

hanyunanodesudc

GMC Member

• GMC Member
• 363 posts

Posted 30 March 2009 - 12:11 AM

did you try putting move towards point in the step event?

yea, if I do that, the obj spazzes D: (vibrates lol)
• 0

### #4 joethedumblr

joethedumblr

GMC Member

• New Member
• 253 posts

Posted 30 March 2009 - 12:15 AM

did you try putting move towards point in the step event?

yea, if I do that, the obj spazzes D: (vibrates lol)

hmm... well im fairly new to game maker, but i imagine there should be a way to do something like

if obj.x/y = x/ycoordinates then speed=0

that or maybe you could try putting the move towards point in the end step. (that smoothed out some move towards point spazzyness i was having with my lighting system.

Edited by joethedumblr, 30 March 2009 - 12:15 AM.

• 0

### #5 hanyunanodesudc

hanyunanodesudc

GMC Member

• GMC Member
• 363 posts

Posted 30 March 2009 - 12:21 AM

D: end step isn't working either...
hm...
I did think of this method:
put an invisible object (placing in 224,96), and wen the first obj collides with the invis obj,
stop it.

but i think that method's too.... messy.
• 0

### #6 asfastasdark

asfastasdark

GMC Member

• New Member
• 55 posts

Posted 30 March 2009 - 01:44 AM

I had the same problem in a small engine I built and experimented with. I eventually placed an invisible object there and checked for collision. A solution might be this:
Basically, say the moving object is coming from -100,-100, and going towards the point at 0,0. Then we can check whether it has reached this by in the step event checking:
if (object.x>0 && object.y>0).
That might solve it.
• 0

### #7 hanyunanodesudc

hanyunanodesudc

GMC Member

• GMC Member
• 363 posts

Posted 30 March 2009 - 02:15 AM

I had the same problem in a small engine I built and experimented with. I eventually placed an invisible object there and checked for collision. A solution might be this:
Basically, say the moving object is coming from -100,-100, and going towards the point at 0,0. Then we can check whether it has reached this by in the step event checking:
if (object.x>0 && object.y>0).
That might solve it.

ohh!
yes, it works perfectly! thanks
• 0

### #8 shockwave259

shockwave259

GMC Member

• New Member
• 4 posts

Posted 27 April 2010 - 03:09 AM

i have been trying to figure out the same thing... but how do i put the last post in motion? please describe in steps...completly clueless
• 0

### #9 Tennx

Tennx

The Entrepreneur

• GMC Member
• 260 posts
• Version:GM:Studio

Posted 28 February 2012 - 02:48 AM

i have been trying to figure out the same thing... but how do i put the last post in motion? please describe in steps...completly clueless

http://wiki.yoyogame...e_towards_point

Seems like it's well explained. Put that code inside your object, in a trigger for example (can be done when you press a keyboard button, or when you click with the mouse, or a game state change, and so on...):

```if distance_to_object(obj_player) < 128 then {
move_towards_point(obj_player.x,obj_player.y,4);
}```

And then, in the step event of the same object:

```if distance_to_object(obj_player) < 4 then {
speed = 0;
}```

And that's all

Edited by Tennx, 28 February 2012 - 02:49 AM.

• 0

### #10 IceMetalPunk

IceMetalPunk

InfiniteIMPerfection

• Retired Staff
• 9314 posts
• Version:Unknown

Posted 28 February 2012 - 03:11 AM

The "vibration" is because unless the distance to that point is an exact multiple of your speed (which is rare), your instance will overshoot the target and try to move back. That will make it overshoot again, etc.

The best solution, which will work for all points and speeds (not just when switching from positive to negative or vice-versa) is to move at either full speed, or a speed equal to the distance between you and the target point, whichever is smaller. This way, when the distance is less than your speed, you'll just jump directly to the target instead of overshooting.

So, this in the step event:

```distance = point_distance(x, y, targetX, targetY);
move_towards_point(targetX, targetY, min(moveSpeed, distance));```

-IMP
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users