Question by
Ploppy3 · Jun 05, 2017 at 09:36 AM ·
raycastcollider2d
Raycast calls sometime passing through Collider2D's points
Hello, I'm a Unity beginner!
The rays from my Raycast2D calls sometimes pass through points of my PolygonCollider2D and I don't know why.
Hits are marked in the following images by a small yellow horizontal line.
Here is my code:
Debug.DrawLine(origin, new Vector3(path[i].x, path[i].y, 0), Color.yellow, 0, false);
float angle = Mathf.Atan2(path[i].y - origin.y, path[i].x - origin.x);
Vector2 direction = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
RaycastHit2D hit = Physics2D.Raycast(origin, direction);
Debug.DrawLine(new Vector3(hit.point.x, hit.point.y, 0), new Vector3(hit.point.x - .2f, hit.point.y, 0), Color.yellow);
Debug.DrawRay(new Vector3(origin.x, origin.y, 0), new Vector3(direction.x, direction.y));
Is it due to bad rounding?
If so, how can I fix it?
Comment
Best Answer
Answer by Ploppy3 · Jun 07, 2017 at 02:11 PM
I was told that it was in fact a of float rounding issue.
First solution: Add a "skin" to the objects so that the ray hit the skin and does not miss slightly the object.
Second solution: Offset the ray a tiny bit so that it is sent a little bit into the polygon and not at the exact coordinates of the vertices.