- Home /
Need help moving object to turn around
Im learning Unity, and i want to make an object move, go a certain distance, and turn around. I can handle the turn around part. My main problem is making the object stop after moving a certain distance. This is my script so far:
function Update ()
{
transform.Translate(Vector3.forward * 3 * Time.deltaTime, Space.World);
}
Answer by Josh707 · Nov 22, 2012 at 10:57 PM
You could get it's position, start moving it with your code and keep checking it's coordinates x,y, or z values and if it equals a certain distance, break the loop.
Something like:
public GameObject yourobjectname;
public Transform objectPosition;
public GameObject stopPoint; //You could either predefine the
//stopping point with a Transform
//value or empty gameObject and
//get its position
void Update(){
objectPosition = yourobjectname.Transform.position;
if(objectPosition != stopPoint){ //use stopPoint.transform.position if your using the empty gameobject
yourobjectname.transform.Translate(Vector3.forward * 3 * Time.deltaTime, Space.World);
}
}
I only know C# so sorry about that. I don't know how to translate it into JS but without testing this I'm gonna guess it should work with minor tweaking. Kind of inebriated and learning python so sorry if I mixed anything up, and maybe using while instead of if would be better? I don't even know. Just felt like you should get an answer.
Another idea:
while(1){
(Put moving code here)
if(objectPosition == stopPoint.transform.position){
break //should stop the whole thing
}
}
Don't know about this one, like I said I do not know if while(1) and break, from python, are used the same in C#. It's just an infinite loop until you break it with a keyword.