- Home /
How to move GameObject to an initial Point?
I need help in creating a script that moves my GameObject (cube) from the initial place where it was. My cube persues and kicks a ball, when he do this he stays in the place where he kicks the ball, but I want to move him to it's original place. How to do this? Any help are welcome, thanks!
var initialPosition : Transform;
var Cube : Transform;
function Update () {
var distance = Cube.position - initialPosition;
if ( ( distance > initialPosition)) {
Cube.transform.position = initialPosition.transform.position;
}
}
Answer by giano574 · Mar 11, 2014 at 02:09 PM
Are you asking how to set a GameObject back to its original position? If so, it is really easy. Any GameObject's position (transform.position) is a just a 3-dimensional vector coordinate (Vector3 in Unity). You just have to make a Vector3 member variable and assign it to transform.position in the Start method like this:
var initialPosition : Vector3;
void Start(){
initialPosition = myTransform.position;
}
Hi, thanks for helping, but it doens't work in this case. I apply you suggestion on this script and The GameObject (a cube) in this case didn't return to it's original place. The cube persued and kicked the ball and stayed in the place where he kicked the ball...
You have to set myTransform.position to initialPosition when you want it to move back. So:
//When you want the ball to return:
myTransform.position = initialPosition;