- Home /
Vector3.Lerp Only working once
I'm trying to make a very simple AI script. All I want the enemy to do is travel to its target when a variable is no longer checked. When the game starts everything works how its supposed to, however when the variable is checked and the enemy waits (This gives the "player" a chance to move away). The next time the variable is unchecked, instead of traveling to the target like it did the first time, it instantly transports itself to the targets location. Any ideas on why this is happening?
var duration : int = 3;
var start : Transform;
var end : Transform;
var waiting : boolean = false;
var targetObj : GameObject;
private var targetScript : Target;
function Awake (){
targetScript = targetObj.GetComponent(Target);
}
function Update(){
if(waiting == false){
transform.position = Vector3.Lerp(start.position, end.position, Time.time/duration);
}
}
Answer by whydoidoit · Jun 11, 2012 at 12:20 PM
It works the first time because Time.time starts at 0. After that , it's going to be bigger than 1.
You need the last parameter of the Lerp to be a number between 0 and 1 - if it's 0 it's at the start position and 1 it's at the end position.
You would normally add Time.deltaTime to something to calculate it:
var t : Float = 0;
...
t += time.deltaTime;
transform.position = Vector3.Lerp(start.position, end.position, t/duration);
And reset t when you want to start again.
No problem :) Please mark the question as answered to get it off the list by clicking on the tick under my answer...
Your answer
Follow this Question
Related Questions
Vector3 Lerp using Time.deltaTime? 3 Answers
Smooth translation 2 Answers
Vector3 Lerp finish too quicly 1 Answer
How to smooth my camera (Vector3.Lerp) 0 Answers
Vector3.Lerp doesn't work on build 0 Answers