- Home /
Raycast From Camera to Object
I'm trying to do a raycast from the main camera to an object.
RaycastHit hitInfo = new RaycastHit();
Vector3 screenPos = Camera.main.WorldToScreenPoint(gameObject.transform.position);
Ray rayToPlayerPos = Camera.main.ScreenPointToRay(screenPos);
int layermask = (int)(1<<8);
if(Physics.Raycast(rayToPlayerPos, out hitInfo, 1000, layermask))
{
//DO SOMETHING IF HIT, I NEED hitInfo RAYCASTHIT
}
However this does not work. If I do a
Debug.DrawRay(rayToPlayerPos.origin, screenPos);
it draws a ray to the right and up of the object. Like completely off. Any ideas? I followed this, or I thought I did anyway: http://answers.unity3d.com/questions/386951/raycasting-from-camera-to-player.html
EDIT: Inversely with a raycast from object to Camera does not work...
RaycastHit hitInfo;
Ray rayToCameraPos = new Ray(transform.position, Camera.main.transform.position);
int layermask = (int)(1<<8);
if(Physics.Raycast(rayToCameraPos, out hitInfo, 1000, layermask))
{
Debug.Log ("HIT");
}
Answer by robertbu · Apr 26, 2014 at 05:27 AM
Your first the problem with your Debug.DrawRay(). This function needs world coordinates, plus the second parameter is a direction not a position. You are passing a screen coordinate as the second parameter, not a world direction.
As for your Raycast to the camera, there is one issue, and potential issue. First you are passing in two position, Raycast() takes a position and a direction. And Raycasting requires an active collider, so your camera will not have a collider by default. If you are trying to see if something is between a position and the camera, an alternate choice is a Physics.Linecast(). This takes two position. A LineCast() from the position to the camera does not require a collider. If the Linecast() hits anything, then there is an object between the position and the camera. If you wanted to make your current Raycast code from the object to the camera (and you have a collider on the camera), you can do it this way:
Ray rayToCameraPos = new Ray(transform.position, Camera.main.transform.position-transform.position);
As for your Raycast code, this typically how it would be done:
dir = gameObject.transform.position - camera.main.transform.position;
if(Physics.Raycast(camera.main.transform.position, dir, out hitInfo, 1000, layermask))
{
Debug.Log(hitInfo.collider.name+", "+hitInfo.collider.tag);
}
There is no reason to construct a ray, and therefore there is no reason to convert to screen coordinates to create a ray. And you can construct the direction by subtracting the camera position from the object position.
Thanks!
What ended up happening was, which I forgot to mention in the question which may have helped, was I have a layered plane over the game between objects and the camera. so what ends up happening is you get a raycast from the object to the camera that gets the intersection of the cast. BUT, the plane was flipped. So when I tried to raycast from the object to the camera it wasn't actually colliding (one sided plane!) Thus, once flipped, it then registered the hit.
Your mentioning of the collider on the camera re$$anonymous$$ded me of the silly mistake.