Physics2D.Raycast producing bizarre results.
This problem is driving me bananas!
I have a 2d scroller. The player has a script that has the following code in Update():
RaycastHit2D hit = Physics2D.Raycast (new Vector2(-23f, 5.9f), Vector.down, 10f, layer);
if (hit != null) {
Debug.Log(hit.distance);
} else Debug.Log("no luck");
The "layer" is set to ground, which is rotated around z-axis. Turns out that hit is null unless the player walks over position.x = -23. I have no idea why the player actually has to walk through the Ray for it to work. Why doesn't it just produce the ray at the origin I specified regardless of where the player is??
I read through the manual a thousand times but I can't figure it out. Any ideas are greatly appreciated. Thanks!!
Hmm try adding this in the update() Debug.DrawLine(new Vector2(-23f, 5.9f), new Vector2(-23f,5.9f-10f), Color.blue ); will allow you to see the ray.
i tested your code and the ray is where you want it. do u want it to be at the origin of your player? also check your player layer.
Your answer
![](https://koobas.hobune.stream/wayback/20220612042219im_/https://answers.unity.com/themes/thub/images/avi.jpg)