- Home /
waypoint script, how to remove lerp easing
I have this simple ai waypoint script I just wrote, the slowing down at the end of each segment is annoying, I'd like a linear movement ( or more control of the speed and acceleration).
I read a lot about that problem but couldn't understand properly how to fix it.
Any help would be welcome, any feedback on the script itself as well.
Thanks
#pragma strict
var points : GameObject[];
private var i:int;
private var curpoint : GameObject;
function Start () {
i=0;
if (curpoint == null) {
curpoint = points[i];
}
}
function Update () {
if(Vector3.Distance(transform.position, curpoint.transform.position) > 1) {
transform.position = Vector3.Lerp(transform.position, curpoint.transform.position, 0.08);
}
else {
if(i < points.length-1) {
i= i+1;
curpoint = points[i];
}
}
}
Answer by Owen-Reynolds · Apr 29, 2013 at 08:56 PM
MoveTowards is often better at this than Lerp. Same general idea, except the last input is the amount to move that frame, so something like SpeedPerSec*Time.deltaTime
.
You can hand control the exact speeds. For example this will quickly slow down to 2 meters/sec when you get within 5 meters (I picked a slowdown speed of 8 at random):
// your code computes distance, save it in D:
float d = Vector3.Distance(transform.position, curpoint.transform.position);
if(d<5) { // slow down
SpeedPerSec -= 8*Time.deltaTime;
if(SpeedPerSec<2) SpeedPerSec=2;
}
// your old code:
if(d<1) // moveTowards instead of lerp here
Your answer
Follow this Question
Related Questions
Problem in moving via waypoint 0 Answers
How can i smoothly rotate a gameobject 1 Answer
Problems with Lerp. 1 Answer