How to stop object when it achieve destination point?

When use methods like moveTo(), the moving object doesn’t stop moving once it reaches the destination coordinates, so how to change this behavior properly?
I tried to use body.hitTest() in update() method, but for little objects and when the speed is pretty fast, it may not work every time, since the hit test may skip the update call in which object reaches destination coordinates.

Hello @PavelMishin. Can you give us more informations about the context you are using moveTo()? Maybe the corresponding code parts? It will be easier to help you.

Thank you for response!
I consider that this questions not relates by context, it’s not about specific method or part of code, I can rephrase it this way: “How to stop moving object when it reaches certain point?”
But here is an example, anyway.
In this example if you change speed (line 45) to 100, it will work (but anyway sprite will stopped with different offsets regard of end point)

I can see the problem now. In this example they used another approach with setting a distance tolerance. I tried it with your example and it worked quite well when I set the tolerance high enough (around 50). But sometimes the circle still continue to travel.

Maybe you need another approach: Try to make an empty game object, that you create when you click with the pointer. Then you could use the overlap() function to detect when the circle overlaps with the empty game object. Looking forward to hear again if that helped?

1 Like

Well, I had an idea to use invisible object and collide or overlap function to stop moving, but it’s pretty dirty hack, I think there is should be more properly and simple way for this task.
The approach with distance seems more preferable (but still not perfect) in my opinion, thanks for the link to example.

I agree. You might use zone instead (example). But that is a dirty hack too. Maybe somebody else knows about a better way to solve this.

Add a timer event to stop the object. Or skip moveTo() and use a tween instead.

But in a physics simulation, you have to either test for distance, test for overlap, or use some more-complex variation of those (reverse interpolation).

2 Likes