raycast from rotating camera

Hi guys,

I’m trying to cast a ray from a mouse orbit camera so that whatever the player looks at is what the ray hits. In other words, if the player turns his 'head" and looks left, the ray is cast directly down the line of sight to the left. The problem I’m having is that if I use:

if (Physics.Raycast (transform.position, Vector3.forward, hit))

and attach the script to the camera, it casts the ray down the camera z axis, which apparently isn’t the field of view axis. The field of view rotates separately from the transform axis. Is there a way to cast a ray along the direction the camera is actually looking? As always, thanks in advance for the help.:face_with_spiral_eyes:

Sorry. Found the answer. In case it helps anyone else:

var ray : Ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));

Use object.transform.forward, as that is what represents local directions. Vector3.forward is +Z in the world; transform.forward is the object’s +Z axis.

1 Like

Thank you for that.It helps a lot. I’m having another related problem. I’m trying to use the hit point of a raycast from the camera as a target. In other words, I’d like to slerp a missile from the transform position to the hit.point of the ray. Wherever the camera is pointing, that’s where I want to hit.

Here’s the code I’m using attached to the camera:

static var missilelock : Vector3;

if (Physics.Raycast (ray, hit)&hit.distance<1000)
{
missilelock = hit.point;
}

Then on the instantiated missile:

var newtarget = CameraControl.missilelock;

 var targetRotation = Quaternion.LookRotation (newtarget - transform.position, Vector3.up);
   transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime *0.5);

The trouble is that the missile seems to veer off toward the world position (0,0,0) and not toward the hit.point. Either it’s not reading the static variable, or there’s something else wrong. Can’t figure it out. Thanks again for the help.

If you want to move the missile’s position, you should be using Vector3.Lerp between the current location and the hit point. If that is not what you are doing then I’m not too sure what you need to do with Quaternion.Slerp, in which case perhaps you can give a bit more explanation.

Hi Andeeee,

What I’d like to do is to pass the hit.point of the raycast to the missile and have it gradually turn toward the hit point. I’m using Add.Force for thrust, so I don’t really need Vector3.Lerp. I’m launching the missile from a moving airplane sim and want it to home in on the hit.point, which could possibly be another moving airplane. So I don’t want it to turn instantly, but to launch straight forward from the plane and gradually turn toward the hit.point. With the above code, the missile launches and gradually veers off, but not toward the hit.point. I’m assuming it’s going toward Vector3 (0,0,0), but I’m not sure. I did a Debug.Log of the hit.point and it seems to be seems to be returning values ok.

I guess one question is, can hit.point be passed to the Quaternion.LookRotation function as the target rotation. That’s what I’m doing right now.Instead of using (other.position - transform.position), I’m using (hit.point-transform.position). Don’t know if this is valid. It’s not throwing an error, but I’m not getting the proper rotation either. Thanks so much for the help.