- Home /
Find a Position on the Axis of a GameObject
Hi Guys!
I'm kinda annoyed that I can't figure this out...
I want to move my camera forward along it's z axis (which can be facing any direction). Instead of using Translate, I want to find a position along the camera's z axis and have it move to that position.
The problem is I can't figure out how to get that position along the camera's z axis. I think it has something to do with transform.TransformDirection(), but I can't figure out how to get it to work.
Thanks for the help in advance, and if you need any more info I'd be happy to give it!
What isn't working about Translate for you? That's how you "should" be moving your object around. (I.e. transform.Translate(0, 0, distance);
)
Answer by mightysprite · May 21, 2014 at 04:12 AM
The transform's Z vector points in that direction and is unit length. So just scale that vector by the amount you want to move it and add that to the position vector.
Vector3 movement = transform.forward * distanceToMove;
Vector3 newLocation = transform.position + movement;