How to smoothly move an object in a given amount of time

Hi,
I have created a ghost system for a racing game, so I have positions values stored in an array, and I’m trying to move my object from one point to the other every 5 physic ticks. Given “physic tick per second” is set at 120, it means a gap of 1/24 of a second between each position, if my math is correct.

I’ve tried using this in _physics_process :

global_position = lerp(global_position, next_position, some_number * delta )

The motion is smooth, but for the ghost to be at the right position, I would need some_number to be set at a value I have frankly no idea how I can find. With trial and error I found that something around 3 is good, but not good enough, the object always seems to be off from a few tenth of a second.

So I’ve tried using a tween I trigger every 5 ticks :

tween.tween_property(self, “global_position”, next_position, 1/24)

Now the object seems to be at the right position, but the movement is super choppy.

There must be a way to combine the smoothness of one and the precision of the other.

First, 1/24 is 0.041, thats why your animation is super fast.
Second, you can use lerp function in a way:

time += delta / 2 #Replace with any number for making it slow, or multi for fast
time = clampf(time, 0.0, 1.0)
global_position = lerp(global_position, next_position, time)

But you need to reset the time variable to 0 while changing the next position.
Or use the lerp normally, just try to multiply the delta by lower values like this:

global_position = lerp(global_position, next_position, 5.0 * delta )

First, 1/24 is 0.041, that’s why your animation is super fast.

It might be fast, but as I’ve said it’s the interval between 2 recorded position, I need it to be at this exact speed.

Second, you can use lerp function in a way:

I’m not sure I’m following you, where would I put my 1/24 value here ?

Or use the lerp normally, just try to multiply the delta by lower values like this:

Well, that’s exactly what I was doing, but again I have no idea what value I should use for my ghost to be moving at the right speed.

1 Like

Ok, I fixed this.
First thing, I forgot that you should NEVER use 1/24 and instead use 1.0/24.0, otherwise Godot thinks you are using integers and always returns 0.
But it was still a bit choppy, so I simply doubled the amount of time between two position recording, anyway it had no reason being that precise.
Now it works, it shows the precise position while being silky smooth.

1 Like