- Home /
Other
Raycasting not working on all sides?
Hi,
I'm having an issue with 2d raycasting. I have a player object(blue rectangles) that moves around a polygon(blue polygon). When I run a physics2d.raycast(green lines) from the player to the center(trasform.position) of the target polygon, it returns a hit, but ONLY on the left side of that polygon. If I move to the right, it ceases to return a hit and the hit.point position sticks to the top-most or bottom-most vertex of the polygon collider.
Does anyone know why such an error occurs?
Here is my raycasting:
//raycast from rectangle to selected shape/body
RaycastHit2D hit
= Physics2D.Raycast(transform.position,
polygon.transform.position,
Mathf.Infinity,
1 << LayerMask.NameToLayer("Ground"));
Thanks for any suggestions.
Answer by Bioinformatizer · Feb 09, 2016 at 03:02 AM
Just a tool to help you along the way with this, Debug.DrawRay(). I would also take out the infinity, since you are not limiting your raycast with it.
Debug.DrawRay(transform.position, polygon.transform.position, Color.red);
Good Luck!