- Home /
My waypoint animation script is progressively accelerating
I am making a simple gameobject-animation system, to use with my simple character. What i am trying to do is make the character's hand move trough the positions of other "waypoint" gameobjects. My problem is, each time the waypoint is hit, the travel speed of the hand goes faster and faster intil it simply blinks to the positions, then enough to be invisible. The script was supposed to animate the position evenly trought the waypoints.
0 -> 0 -> 0
<- <-
Here's my code:
var start : Transform;
var end : Transform[];
var speed : float;
private var time : float;
private var state : int = 0;
function Start () {
start = transform;
time = Time.time;
}
function Update () {
transform.position = Vector3.Lerp(start.position, end[state].position, (Time.time - time)*speed);
if (transform.position == end[state].position) {
state++;
}
if (state == end.Length){
state = 0;
}
}
Answer by Kilometers · Dec 31, 2011 at 10:02 PM
In your Lerp() function, (Time.time - time) will eventually exceed 1 if it is not reset. Remember, the third value in Lerp(), the float t, is supposed to be a value from 0 to 1. If it is 0, Lerp() returns a Vector3 equal to the "from" Vector3. If it is 1, it returns a Vector3 equal to the "to" Vector3. If it is set to .5, you get the midpoint. So, I believe all you need to do is to reset "time" to equal Time.time at each state++.
It worked, many thanks! $$anonymous$$ost of my program$$anonymous$$g errors are such trivial things.