[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

Deeply analyze the principle and practice of RSA key

1、 Preface After experiencing many dark moments in life, when you read this article, you will regret and even be angry: why didn’t you write this article earlier?! Your darkest moments include: 1. Your project needs to be connected with the bank, and the other party needs you to provide an encryption certificate. You have […]