Vaguely why Time.deltaTime causes uneven motion

I am trying to move an object so that it visually moves at the same speed regardless of frame rate fluctuations. In my opinion, I should use Time.deltaTime

to determine how far to move during each iteration Update

. My code Update

looks like this:

void Update()
{
    this.rigidBody.MovePosition(this.transform.position + new Vector3(Time.deltaTime * 2f, 0, 0));
}

      

However, the object moves faster and slower as it moves across the screen. If I move it by a constant amount during each Update

(like below) it appears to move evenly.

void Update()
{
    this.rigidBody.MovePosition(this.transform.position + new Vector3(0.02f, 0, 0));
}

      

I am confused because I thought that moving an object a constant distance during each Update

should be what causes the jitter as its speed will be affected by the frame rate. And I thought that using it Time.deltaTime

should result in a constant speed of movement, independent of the frame rate.

My values Time.deltaTime

go from 0.002 to 0.17, which I also don't understand because I have a very simple scene with only 2D sprites, so I don't understand why the variance is so high.

+3


source to share


1 answer


If you want to calculate the exact new position of yours RigidBody

, you just have to set position

directly. For example:.

this.rigidBody.position += new Vector3(Time.deltaTime * 2f, 0, 0);

      



Alternatively, run the code you already have, but in a method FixedUpdate()

.

MovePosition()

intended to be used in the context of a method FixedUpdate()

. Unity3d will try to calculate the movement speed according to the frame rate, which will interpolate the positions between the current position and the new position. You mix it up by doing this interpolation as well and using the result when you call MovePosition()

.

+3


source







All Articles