Physics2D.Raycast hits him self.

Hello

I am trying to detect collisions with walls so i can change enemy direction movement & rotate it face.
i use RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.right,1f); (in case the enemy face is to the left)

This code is inside the enemy object.

the issue is that it always detects the enemy … like it detect its inner self collider all the time instead of detecting other collisions (colliders) so the enemy can rotate to the other direction if hit a wall .

any solutions for this?

Yes, you can specify what layers to cast against with a layermask. http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

The easiest way to specify this is to create a public variable of type LayerMask and feed it into your Raycast, this way you can set the layer through the inspector. Then just be sure to assign your layers correctly to your objects.