Vector3.Lerp

So I looked at the reference, and found it quite useful.
But um, how does it move it… like slowly?
Is there anyway to make it go to a half of startpos to endpos is 3 seconds?

What is this “it” you’re talking? what kind of movement do you want?

Look at title.

I want even movement, all spread out in 3 seconds, using Vector3.Lerp

:mrgreen:

Vector3.Lerp is a basic interpolation method for vectors. Lerp is an abbreviation of: Linear interpolation.

Interpolation is finding new values within a known data set that follows some progression or relation to the known data. See:

Linear Interpolation is just saying that the approach we’ll take to finding the new values is a linear approach. If we have 2 known points, we have enough information to define a line, and we can then find new values across that line.

The formula for a linear interpolation is:
c = (b - a) * t + a

where:
a is your starting value
b is your target value
t is a percentage of how far you want to go from a to b (0 to 1 for points between a and b, larger values will go past b on a line through a and b)
c is your result

note - this formula isn’t just 3-d, it can be used for all dimensions for a linear interpolation. From 1d, 2d, 3d, upto N-d.

This can be used for all sort of things. But is not magic. You need to know what you’re doing with it.

Lets say for instance you’re having a camera chase a player. But you want it to delay a little bit… feel a little swimmy as it chases. Instead of setting the position of the camera to some location with an offset from the player. You instead could calculate this target position, and then lerp to the target. The closer to 1.0 your t is, the more it feels rigid and stuck to the player, the closer to 0 the more swimmy it feels.

//props defined for script
public float FollowRigidness = 0.5f;
public Transform FollowTarget;


//in update
var targ = this.FollowTarget.position + offset;
var cur = this.tranform.position;
this.transform.position = Vector3.Lerp(cur, targ, this.FollowRigidness);

Now by doing this in the update loop. Your result isn’t just the lerp. It’s a more complex formula where you’re basically accelerating/decelerating at the rate of your lerp relative to the acceleration/deceleration of your target.

I could define this equation for you… but it’d be useless in a practical sense, and also would just be a complicated math equation.

Now the point here is this isn’t actually “even” movement. I’m not sure what you mean by “even” movement, but I’m going to guess you mean smooth and constant. This would be something like an object moving at a constant velocity over time.

This is easy, and doesn’t use lerp.

Say you have a velocity defined as <1,0,0> (moving 1 unit along the x-axis per second). Your update would merely be:

//in update
Vector3 v = new Vector3(1f,0f,0f);
this.transform.position += v * Time.deltaTime;

(though technically speaking that formula there is very similar to lerp, and can be defined within the bounds of interpolation… but we don’t use Vector3.Lerp to calculate it).

By multiplying by the deltaTime we’re moving only a little bit of the target velocity, so that as the deltaTime adds up to 1 whole second, you would have accumulated enough small movements to be equal to the velocity per second.

If none of what I said is what you’re looking for. Well… you did NOT ask your question clearly enough.

Thanks, I’m speechless :smile:/