The sprite is twitching

Hi! I wrote an example of the sprite movement for myself, it moves to a point at the point of pressing. But when the point is reached, it begins to “twitch”. How can I fix this?

Checking whether the NewObject is in place is not correct. Due to the fact that the position values do not change in whole values, but in fractional values, it is difficult to achieve an accurate position. Therefore, the coordinate check fails. Better to check the distance between the center of the object and the destination. If it is less than the set minimum value (choose what is optimal for you), then set the coordinates of the object to the desired point (this will be the exact positioning) and turn off the movement.

Something like this:

Only for the NewObject2 object, I set the Origin point to the center. This function checks the distance between two objects, but it is also possible just between two points (if I am not mistaken, there is such a function in the engine).
Update: You can use the NewObject.DistanceToPosition(X, Y) function

1 Like

Thanks! Your option is excellent!But I can’t find where to enable newObject. DistanceToPosition (X, Y) Can you take a screenshot with this function?

1 Like

Oh sure:

1 Like

How are you moving the sprite - setting the x & y positions, by applying a force, or something else?

I attached an example, you can download and see

Maybe next time describe the link a bit better - I assumed it was to a video of what was happening :smiley: .

Anyway, the problem is that by applying the force, the object is overshooting and goes past the mouse position in one frame, and the next frame this is corrected by the force getting applied in the opposite direction. This again will result in an overshoot, and you get the repeated overshoot which appears like it’s twitching.

Also, using forces will produce positions with many decimal places, and if these aren’t exactly the same as the mouse position, then the force will be applied to get the object to the mouse position, even if visually it looks like it’s at the right spot. (e.g. 344.654 is not the same as 344.655, even though on screen they are the same pixel)

You have a few ways of overcoming this :

  1. Only apply the force if the mouse isn’t over the object. This will stop all the twitching
  2. Only apply the force if the distance between the mouse and object is greater than, say, 2 pixels (just using an example here - you may need to increase or decrease this value)
  3. Use a mouse joint, with a high threshold. This will require adding the Physics 2 behaviour to the object.

I forgot option 4 - check that a point of the moving object is within the target object.

Just checked your code. I believe the other issue is that the checks are using the centre of your object is at the mouse, but force is being applied to the origin, and these aren’t exactly the same. This is causing an issue too.

I made the following changes:
- Moved the origin of both NewObject & NewObject2 to the exact centre :
- Changed the condition for applying the force (only apply the force if the NewObejct centre is not within NewObejct2), and removed the event that stops all forces on NewObject. :

And that fixed the twitching.

1 Like