Raycast2D from camera does not hit objects
Hey,
I am using Physics2D.Raycast to hit a 2d object, with vector zero as my direction. However, the collider value is always null. When I change the direction to other, for example Vector2.up, it does hit the object above the mouse click location.
this is my code:
Vector3 position = Input.mousePosition;
_hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(position), Vector2.zero);
And this is some raycast start location:
Camera location: (0,0,-10)
The Gameobjects that I try to hit has Z = 0 value.
Answer by ilagef · May 06, 2020 at 09:53 PM
I found the problem and the solution
I had to checked Queries Start In collider. Edit -> Project Settings -> Physcis 2D -> Raycasts Start In Colliders.
I have unchecked it in the past, so the 2DRaycast would not hit the object that create, but I didn't know that its make a problem.
(here the post that I used to make the 2DRaycast not to hit itself: link, but again, it created my problem)
Your answer
Follow this Question
Related Questions
RayCastHit2D fails to hit script generated grid 1 Answer
Perpetual Pendulum 1 Answer
Tiles are only being removed if raycast fired between 0 and 90 degrees clockwise from character 0 Answers
Hookshot in Unity 2D not working,Hookshot 2D not working 0 Answers
top down 2d game, aim weapon with mouse. please help 1 Answer