- Home /
Move a GameObject for certain amount of time, and then move it again for a certain amount of time...
Hey, So, I tried to do this a horrible way (Looping... don't ask), and it didn't really work, but I was wondering what approach/how you would do this.
I need a GameObject to move left for like, 5 seconds, then right for 5 seconds... and back and forth...
If you have an idea on how to do this, please say so, I'll be thinking about it though...
Thanks, Justin W.
Answer by Hibame · Nov 21, 2010 at 11:34 PM
A quick way to go about this is to work with the Time. When you start moving store a float of the current time with Time.time.
float savedTime = Time.time;
From there you can do
if(Time.time - savedTime >= 5)
to know if 5 seconds to has passed since you set the variable. All of this should be put inside something like the update function.
$$anonymous$$akes sesne, will try it out... Thanks!
Answer by Justin Warner · Nov 21, 2010 at 11:47 PM
function Update () {
float savedTime;
savedTime = Time.time;
if(Time.time - savedTime <= 5)
{
transform.Translate(Vector3.forward * Time.deltaTime * 10);
}
else if (Time.time - savedTime >= 5 && Time.time - savedTime <= 10)
{
transform.Translate(Vector3.forward * Time.deltaTime * -10);
}
else if (Time.time - savedTime > 10)
{
savedTime = Time.time;
}
}
Gives error: Assets/Move.js(2,6): UCE0001: ';' expected. Insert a semicolon at the end.
I was working in C#. You should convert the float savedTime; to what ever js equivalent. I think there needs to be a var in there somewhere.
Omg, sorry, I'm so used to java from school, forget that Unity changes it a bit.
LOL wow, it's from November when I just joined... congrats.
Your answer
