Search code examples
unity-game-enginecollision-detectioncollisionraycastinggameobject

How do you raycast from an object ignoring only that object (Unity3d)?


How can I raycast from a gameobject, while ignoring ONLY that gameobject (in Unity)? I am creating a script that detects which side of the gameObject is collided with. I am using a raycasting script that I created to achieve this. I thoroughly understand layers in Unity, so please no explanations on using layers.

Here is the reason why layers will not achieve my goal: So lets say I have one enemy prefab that sends raycasts ignoring its own layer. If I only have one enemy instantiated (unlikely), everything will work fine. So lets say I have two of the same enemy instantiated. Their layers are the same, so it will NOT detect the other enemy as an object, and whatever I needed to do on a collision will not happen (e.g. turn the enemies direction around).

How can I raycast from a gameobject, while ignoring ONLY that gameobject? If this is not possible, how can I create a collision detection script that returns the side of the gameObject that was collided with (e.g left, right, top, or bottom)?

Thank you so much for any support/answers. It is greatly appreciated as this is a problem that I have run into a couple times now, and have found no reasonable answer to.

========================================================================= EDIT:

I found that if you go to Edit -> Project Settings -> Physics2d and Uncheck the box that says "Raycasts Start In Colliders" it solves this issue. I am not sure how to close the question...


Solution

  • I found that if you go to Edit -> Project Settings -> Physics2d and Uncheck the box that says "Raycasts Start In Colliders" it solves this issue.