how can i make a raycast go in the direction the camera is pointing

hey, i want to make a raycast that starts from the camera and goes to in ther direction the camera is looking at, i am a little new to raycasting so here is what i have:

function Update () {
  var hit : RaycastHit; 
 
   if (Physics.Raycast(transform.position, /*idk what to put here*/,  hit, distance)){
     target = hit.transform;
   }
   else{
    target = null;
   }
}

1 Answer

1

If you are adding this script to the camera object then you can just use:

transform.forward

This will get the forwards direction of the camera, while

Vector3.forward

will get the world z axis.

and if you just need to find the camera from any script, you can use Camera.main.transform.forward

–