- Home /
Moving object to specific location
I have a gameobject that I wish to move to a specific location.
I can already move the gameobject instantly to a location by using:
transform.position = Vector3(objectPosX,objectPosY,objectPosZ);
However, I would like to see the object move to that position over a bit of time. I have been playing around with the following code.
var speed : float = 5.0;
function Update () {
transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
}
What I don't understand is how I could move the object to objectPosX,objectPosY,objectPosZ. In the code above I move the object to Vector3(0,0,speed) but Vector3(objectPosX,objectPosY,objectPosZ,speed) clearly doesn't work. Any help to clarify would be appreciated. Thank you!
Answer by Nevadaes · Aug 03, 2013 at 11:54 AM
What you can try is translate your object along a unit vector, which would be the direction where you want to go.
If you calculate the normalized vector that goes from the point you want to go to your object and then translate along that, you should be able to travel to your point.
When you want to take your object to a specific point bit by bit, you should generally think of it as walking along the direction, which is the vector
Vector3 dir = (End - Start).normalized