[Unity3D] Vector3.Lerp(x, y, Time.deltaTime )What do you mean?

Time:2021-4-20

Recently, I saw some code and found that it would be written like thisVector3.Lerp(x, y, Time.deltaTime), delta time is the time from the last call, it is not [ zero , one ] So what does this interpolation mean?

In fact, this is a linear interpolation of deceleration. Let me give a very simple example. For example, to move the position of the current object from 0 to 100.

float currPosition = 0;

void Update() {
    float currPosition = ...;
    float targetPosition = Vector3.Lerp(currPosition, 100, Time.deltaTime);
    // use target position to update
}

hypothesisTime.deltaTimeIf it is 0.2, then the progress from 0 to 100 after the first call is 0.2, and the difference is 20,targetPosition = 20
In the next call, it is 20 – > 100, the progress is 0.2, and the difference is $(100 – 20) * 0.2 = 16 $, sotargetPosition = 20 + 16 = 36

By analogy, it can be seen that since delta time is a constant value, the starting value and target value will be close to the target value with iteration, so the difference between each step will be smaller and smaller.

Iteration start > target delta time step difference the result of the current iteration
0:    0  -> 100,   0.2,       20,       20
1:    20 -> 100,   0.2,       16,       36
2:    36 -> 100,   0.2,       12.8,     48.8
3:    48.8 -> 100, 0.2,       10.2,     59
4:    59 -> 100,   0.2,       8.2,      67.2
5:    ....

I hope the above will help you.

Recommended Today

Libp2p RS version 0.3.0 introduction

V0.3.0 released on 4.23, usingAsyncRead & AsyncWriteTo replace ourReadEx & WriteEx & SplitEx; SimplifiedKad/DHTImplementation logic. modify ReadEx & WriteEx & SplitEx: At first we tried to useasync-traitTo define their own IO operationsTraitFor more pure useasync/awaitTo write code. withReadExFor example, it is roughly as follows: #[async_trait] pub trait ReadEx { async fn read(&mut self, buf: &mut […]