- Home /
How do I find this direction vector?
I'm trying to find a vector which is facing the direction of the camera and is parallel to the x-axis so that I can apply an even force regardless of the x-rotation of the camera. What I have below works but I feel like it's sloppy and there's another way. Is there and if so, what is it?
float camAngle;
Vector3 moveDirection;
camAngle = Camera.main.transform.eulerAngles.x;
Camera.main.transform.eulerAngles = new Vector3(0, Camera.main.transform.eulerAngles.y, Camera.main.transform.eulerAngles.z);
moveDirection = Camera.main.transform.TransformDirection(Vector3.forward);
Camera.main.transform.eulerAngles = new Vector3(camAngle, Camera.main.transform.eulerAngles.y, Camera.main.transform.eulerAngles.z);
Answer by Kryptos · Apr 20, 2012 at 03:19 PM
Try this:
// Get the direction of the camera
moveDirection = Camera.main.transform.forward;
// Make this vector planar (xz-plane)
moveDirection.y = 0.0f;
// Normalize to get a unit vector (optional)
Vector3.Normalize(moveDirection);
Wow, I don't know why I didn't think of that at all...
Note: tranform.forward
seems to be already normalized, at least in Unity 5.2 it is.
transform.forward has always been a unit vector. The normalisation is required to maintain a unit vector after having set y=0.
Answer by ExTheSea · Apr 20, 2012 at 03:20 PM
When you have a 3-dimensional vector whith x, y and z and you delete the y coordinate you get a 2dimensional vector which is not pointing up or down.
newVector=Vector3(oldVector.x, 0, oldVector.z);
hope this works if it isn't the y try the same thing with x and z.
Your answer

Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Dynamically change Vector3.up/right?,How to make a cylinder chase mechanic? 2 Answers
Instantiated objects always facing the center 2 Answers
How to snap a model to the direction being moved in, without also rotating the character controller 3 Answers
Multiple Cars not working 1 Answer