- Home /
Question by
Rapcyss · Jun 12, 2017 at 09:10 AM ·
raycastrigidbody2dcollider2d
Raycast doesn't work on edge colliders in Unity 5.5.4
The centre object has 4 EdgeColliders, everything else is using a BoxCollider2D
I have a fairly simple raycasting method which uses Collider2D.Raycast to find the closest point of contact to the active object by shooting off 90 rays at regular intervals. The raycasting works correctly with all the boxcolliders in the scene but it only seems to register a hit on the start and end points of the edge colliders. This problem arose when I updated from 5.4.4 to 5.5.4 but I can't find any possible cause in the change logs
void FixedUpdate ()
{
if (dragging)
{
contact = false;
contactDist = raycastDistance;
contactCollider = null;
contactAngle = 0;
contactPoint = Vector2.zero;
for(float i =0; i <= raycastAccuracy; i++)
{
RaycastHit2D[] hits = new RaycastHit2D[1];
float angle = Mathf.PI * 2 * (i / raycastAccuracy);
Vector2 direction = new Vector2(Mathf.Cos(angle),Mathf.Sin(angle));
int hit = coll.Raycast(direction, hits, contactDist, mask.value);
if (hit > 0)
{
contactDist = hits [0].distance;
contactCollider = hits [0].collider;
contactPoint = hits [0].point;
contact = true;
}
}
if (contact && contactCollider != null)
{
Vector2 target = contactPoint;
Vector2 position = new Vector2 (transform.position [0], transform.position [1]);
}
}
}
Comment