- Home /
Question by
SteenPetersen · Feb 01, 2018 at 01:50 PM ·
physics2draycasthit2dscreentoworldpoint
Raycast2D hitting colliders it shouldnt be. 2018.1 pictures included.
Hi,
I have a bit of a problem with raycasting in my 2D game. Now I have my camera positioned in such a way that it looks at the world at an angle. (-30 on x rotation)
I have made a raycast in orthographic projection with the following code:
Vector3 screenNear = new Vector3(Input.mousePosition.x,Input.mousePosition.y,cam.nearClipPlane);
Vector3 screenFar = new Vector3(Input.mousePosition.x,Input.mousePosition.y,cam.farClipPlane);
Vector3 farPoint = cam.ScreenToWorldPoint(screenFar);
Vector3 nearPoint = cam.ScreenToWorldPoint(screenNear);
RaycastHit2D hit = Physics2D.Raycast(nearPoint, farPoint - nearPoint);
Debug.DrawRay(nearPoint, farPoint - nearPoint, Color.green);
the debug line that gets drawn seems to do exactly what I want it to, as you can see in my image. however it is still reporting that it is hitting the collider which it clearly is not.
Any help is greatly appreciated.
raycast-explanation.png
(153.2 kB)
Comment
I have done some further testing and it seems as though the raycast will interact with anything that is above or below it. so anything on the Y axis.