Hi, Im creating a game that needs a moving train for a mission. I have all the points where the track changes direction marked in an array using empty game objects. Im using lerp to move the train model from the points, however I want the speed of the train to change based on player input. How would I go about doing this. Im using the lerp code found on the official docs: Unity - Scripting API: Vector3.Lerp
On the docs it says the speed is here:
float distCovered = (Time.time - startTime) * speed; <-------
However what if I want to change speed in the middle of 2 points?
On way is just to not use Lerp. There’s nothing special about it – it’s only a multiply and two pluses. Lerp is (obviously) good for when you know how long a movement should take, but don’t care about the speed. MoveTowards takes a speed as input, so it’s very easy to control the speed of MoveTowards.
But, there is an easy way to control Lerp speed. Instead of recomputing distCovered each frame, add speed each frame. Something like this:
distCovered+=speed*Time.deltaTime;
if( something ) {
speed = speed + ??; // clearly, this will only affect future movement
// check speed here
}
if(distCovered>=1) // we got there