- Home /
Raycasting out from a first person controller
As I posted before I was trying to get OnMouseOver to do this for me but it wasnt happening, after looking into Raycasting I still cant get it to work. I want the raycast to tell me if the crosshair (GUI Crosshair at centre screen, locked to mouse position) is both pointing at and the player is close enough to an object, the intention is for this to then tell another script whether it should work (if statement checking the boolean of the ray) and also turn the crosshair another colour.
The issue I am having is getting the Ray to follow the camera on the First Person Controller. The public GameObject has been set to the main camera. The Debug is showing the ray stationary at a point away from the camera.
Please be aware I have no programming experience bar a couple of previous very simple scripts I did last week.
Here is how far I got with the Raycasting script.
public class CrosshairRay : MonoBehaviour
{
public float interactionDistance = 3f;
public bool imnear;
public GameObject cam;
void Update ()
{
RaycastHit hit;
Ray pointer = new Ray(cam.transform.localPosition, Vector3.forward);
Debug.DrawRay(cam.transform.localPosition, Vector3.forward * interactionDistance);
if(Physics.Raycast (pointer, out hit, interactionDistance))
{
if(hit.collider.tag == "Clickable")
{
imnear = true;
}
else
{
imnear = false;
}
}
}
}
Also note the script is attached to the camera Object.
A ray is defined with an origin and a direction.
The origin is in World-Space, you have defined a local position, so the ray will never start from where you expect. Check your DrawRay where it is being drawn.
Vector3.forward is in World-Space, you want the direction the camera is facing, so theObject.transform.forward.
This is what you're after :
Ray pointer = new Ray(cam.transform.position, cam.transform.forward);
Answer by robertbu · Jun 20, 2013 at 11:32 PM
You want 'cam.transform.position'. If the camera does not have a parent, 'localPosition' will always be (0,0,0). Also you may want the direction to be 'cam.transform.forward' instead of 'Vector3.forward'. 'Vector3.forward' will work if the camera rotation is (0,0,0), but cam.transform.forward will work no matter what the rotation of the camera. Also, if this script is attached to the camera, there is no need of the 'cam' game object variable. And there is a form of Physics.Raycast() that takes a position and direction, so there is no need to build a Ray. Putting the changes together:
public class CrosshairRay : MonoBehaviour {
public float interactionDistance = 3f;
public bool imnear;
void Update () {
RaycastHit hit;
Debug.DrawRay(transform.position, transform.forward * interactionDistance);
if(Physics.Raycast (transform.position, transform.forward, out hit, interactionDistance)) {
imnear = (hit.collider.tag == "Clickable");
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613104546im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Distribute terrain in zones 3 Answers
Unity bug with ray casting? 0 Answers
Multiple Cars not working 1 Answer
Issue with Raycast Detection in Editor 3 Answers