I need to move an object a set distance over a certain amount of time. Example: I want it to move exactly 8 units over 3 seconds.
I need to be able to do this without using iTweens. Any help with this would be great!
I need to move an object a set distance over a certain amount of time. Example: I want it to move exactly 8 units over 3 seconds.
I need to be able to do this without using iTweens. Any help with this would be great!
Using coroutines is the easiest.
static function MoveOverTime( theTransform : Transform, d : Vector3, t : float )
{
var rate : float = 1.0/t;
var index : float = 0.0;
var startPosition : Vector3 = theTransform.position;
var endPosition : Vector3 = startPosition + d;
while( index < 0.0 )
{
theTransform.position = Vector3.Lerp( startPosition, endPosition, index );
index += rate * Time.deltaTime;
yield;
}
theTransform.position = endPosition;
}
If you’d place this in a script named AnimationFunction, for instance, you can then from any script call it like this:
function Start()
{
AnimationFunction.MoveOverTime( transform, Vector3( 0, 0, 8 ), 3.0 );
}
Which will move it eight units (over the z axis) in three seconds.
There is an error with that code. The line: while( index < 0.0 ) Should be: while( index < 1.0 )
– wiley-a