- Home /
How to get the look (or forward) vector of the camera?
Hi,
I need to raytrace a line from the camera until the point on the ground the user is looking at. For this I need the look vector of the camera, any idea how I can get it?
Thanks, Nick
Answer by PrimeDerektive · Feb 07, 2011 at 02:54 PM
Should be the same as any other GameObject, transform.forward.
Unity camera's frustrums always point in the same direction as their transform's forward vector (transform.forward).
So if you do Physics.Raycast(transform.position, transform.forward, hit, length) it will shoot a ray towards whatever is directly in the center of the camera's frustrum.
How have I not heard of that until now?! Well, I guess you learn something new every day. :)
Answer by e-bonneville · Feb 07, 2011 at 02:54 PM
InverseTransformDirection() isn't relevant to the problem (at least not as stated).
Answer by LarryTheBrave · Mar 23, 2019 at 10:33 PM
Forward in Unity or "Which way this object is considered pointing?" would be along the local Z axis. Parent the object to the camera and have everything 0 except add something to Z. Then it will appear "in front of" the parent object at all times.
Answer by ChrisP999 · Apr 21 at 04:52 AM
The other answers are all wrong, at least in my usage case, here is the code that works:
t = _mainCamera.transform; // CinemachineCameraTarget.transform;
Vector3 look = t.TransformDirection(Vector3.forward);
Debug.DrawRay(t.position, look,Color.green,14);
if (Physics.Raycast(t.position, look, out hit, weaponlength, layerMask)) {
...
}