- Home /
Sigh... Need help with moving object a distance over time
I want to move a cube a certain distance over a certain time to the right, pause, and move to the left, pause and repeat.
So far all I have is this, it kind of works but not good enough, and its a dumb aproach no doubt. It moves the directions its suppose to but the distance and speed are dependent on each other when i change them in the inspector and i just want to set a distance and a speed that it passes that distance. Also the cube kind of jitters before it starts moving I don't like that either. >.<
#pragma strict
var speed : float = 5;
var distanceTime : float = 1;
var pause : float = 1;
function Update ()
{
moveCrush();
}
function moveCrush()
{
yield WaitForSeconds(pause);
transform.Translate(Vector3(speed,0,0) * Time.deltaTime);
yield WaitForSeconds(distanceTime);
transform.Translate(Vector3(-speed,0,0) * Time.deltaTime);
yield WaitForSeconds(pause);
transform.Translate(Vector3(-speed,0,0) * Time.deltaTime);
yield WaitForSeconds(distanceTime);
transform.Translate(Vector3(speed,0,0) * Time.deltaTime);
Update();
}
I've searched all over the place and google and didn't find any answers so I'd appreciate help on this. :S
Answer by Graham-Dunnett · Jun 11, 2013 at 01:45 PM
Did you see the documentation for Lerp? It has an example close to what I think you want.
Yup that did the trick. I didn't use it before cause I didn't understand how it works but I kind of get it now. Although, I get what Start $$anonymous$$arker and End $$anonymous$$arker are in the Inspector, but what is the Target transform variable for? Do I need to assign something to it?