How to get the look (or forward) vector of the camera?


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

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.

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.

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,,14);
if (Physics.Raycast(t.position, look, out hit, weaponlength, layerMask)) {

Use transform.InverseTransformDirection.