# Unity 2D - Raycast not colliding with collider in very specific circumstances [duplicate]

I've been trying to work out what could be causing this bug for days now but can't figure it out for the life of me.

I'm trying to make a fish avoid the walls of it's tank in a Unity game I'm creating by: using circle colliders to notify the fish when it hits a wall, making the fish then send out a ray in all 4 cardinal directions towards identical (although obviously with different positions/rotations) boundary colliders, then finding the ray with the shortest distance to know which edge to move away from.

My issue is that generally when my fish is facing left, the ray cast to the right returns null, and vice versa when the fish is facing right (the ray to the left will return null). The only rare exception I've to this has been when the fish is facing left and in the right half of the tank, in that case it will not find the left collider. In all cases, it will not find either the left or right collider.

The white circle visible in the screenshots is "Outer Sensor" and the red circle is "Inner Sensor", both have a Circle Collider 2D component. The red boxes visible are the boundaries, these are all identical and each have a Box Collider 2D component.

The method for detecting the closest edge is as follows:

string FindClosestEdge()
{
string guid = System.Guid.NewGuid().ToString().Substring(0, 4);

Debug.DrawRay(transform.position, Vector2.up, Color.red, 1, false);
Debug.DrawRay(transform.position, Vector2.right, Color.green, 1, false);
Debug.DrawRay(transform.position, -Vector2.up, Color.yellow, 1, false);
Debug.DrawRay(transform.position, -Vector2.right, Color.blue, 1, false);

Dictionary<string, RaycastHit2D> distances = new Dictionary<string, RaycastHit2D>();

float closestEdgeDistance = 999;
string closestEdge = null;

RaycastHit2D up;
RaycastHit2D right;
RaycastHit2D down;
RaycastHit2D left;

up = Physics2D.Raycast(transform.position, Vector2.up, layerMask);
right = Physics2D.Raycast(transform.position, Vector2.right, layerMask);
down = Physics2D.Raycast(transform.position, -Vector2.up, layerMask);
left = Physics2D.Raycast(transform.position, -Vector2.right, layerMask);

distances.Add("up", up);
distances.Add("right", right);
distances.Add("down", down);
distances.Add("left", left);

foreach (var item in distances)
{
Debug.Log(guid + " " + item.Key.ToUpper() + " ray collided with: " + item.Value.collider.gameObject.name  + " at a distance of: " + item.Value.distance);
if (item.Value.distance < closestEdgeDistance)
{
closestEdge = item.Key;
closestEdgeDistance = item.Value.distance;
}
}

distances.Clear();
Debug.Log(guid + " Closest edge: " + closestEdge);
return closestEdge;
}


The below screenshots show the Unity editor in two cases of this happening, and the results of some testing I just tried:

Please let me know if you need any further info to figure this out, any help would be greatly appreciated!

## 1 Answer

Upon further investigation, it seems that I was experiencing the same issue described in this previous GameDev question.

Hope this helps anyone encountering a similar problem.