How to stop Player from jittering once it reaches target(click to move, 3D)?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tymkii

Hey there, I’m fairly new to Godot - have read some of the Doc’s.

I am making a 3D game with “click to move” functionality, I am using this script:

Everything works perfectly EXCEPT once it arrives at its destination it jitters annoyingly, I tried LERP but still couldn’t get the jitter to leave. one thing of note is that if i change the “close enough to target” distance to 2 instead of .5 it stops perfectly fine. I thought it might be the “move_and_slide” function call reseting causing the jitter but the fact that it doesn’t jitter after i change the distance to 2 has me puzzled.

My Player node(a seperate scene/ asset, velocity/movement script) is a child of my Floor node(static body, with script function for getting the click_position). --if this is a problem let me know, i looked it up but couldn’t find if it was.
^Just as I was typing this a thought occurred to me - could it be because the ground is a child mesh and that’s where i’m “clicking” but the click_position isn’t accurate as per the ground mesh?? as in i click on the ground but the Floor node is outputting a click_position value below the ground mesh, causing the cube to attempt to go through the ground, but collision is getting in the way causing stuttering? (probably overthinking it)

Any help would be extremely appreciated as I’ve been trouble shooting this issue ALL DAY and just want to move on with the project. Thanks in advance!

:bust_in_silhouette: Reply From: kidscancode

It depends on the scale of your object and your speed. I suspect you have a larger speed than the one in the demo.

Imagine your speed is 5, and you are 3 units away from your target. This frame, you’ll move 5 in the target’s direction. Now you’re 2 units away, but on the other side. So you’ll move 5 units in that direction, and you’re back to 3. This is what’s happening (although I’ve exaggerated the values to illustrate).

To fix it, you can either:

  1. halt movement within a certain distance of the target. This is fine if your speed is fairly low relative to your object’s size. The difference will not be very visible. This is what the example is doing.

  2. Ramp down your speed to 0 once you get within a certain distance. This will make it look like the object is braking to a stop at its target.

Either way, just as you choose a speed that works for you, you need to choose values of how close to get and/or how much to slow down.

Lastly, your guess about the “depth” factor is almost right, but it’s about the height of your object. Your object’s origin is its center, so if it’s taller than 1 unit, then even when it’s “at” the target location, the 3D distance between the object and the target is > 0.5.

thanks for the fast response! i understand, but my speed and distance to target are exactly the same as in the code (speed = 5, < .5) and it still jitters, (however i’m not using a triangle but a cube instead and no marker - i’m assuming this is not the problem).

That brings me to the last point in your reply - are you referring to the ground plane (in my case a cube mesh with height of 2)? if i’m understanding correctly then i would have to change it to a height of 1 to fix the jitter? or is there some way to make the click_position value imitate the ground mesh? is there a way to fix this without changing either the dimensions of my player cube or the ground mesh? or am i way off? sorry if i am.

tymkii | 2020-05-16 05:00

ah! sorry, i re-read your reply again and realized it’s all connected. from what i can gather i can forget about the ground plane. instead, it’s the size of my object in relation to my speed and distance from target. so if my player object is different in dimension than the one in the example, I need to adjust the speed and distance accordingly. correct?

tymkii | 2020-05-16 05:20

and when you’re referring to speed are you talking about the actual speed variable or the Vector3(velocity), because with a speed of 5 and the distance to target set to < 2 when i bring the velocity back to zero it just stops with no jittering, but that’s to far away from the click_position.

tymkii | 2020-05-16 05:41