Search code examples
c#unity-game-enginegame-enginegame-physicsraycasting

Unity - how to make object ignore ray cast


I have a game object (lets say Enemy) and it has an Audio Trigger script on it with a Sphere Collider.

Now the attack system uses Ray Casting to detect the layer however when you point on an enemy that has an Audio Trigger on it with a Sphere Collider the Ray casting dont recognize it. How do you solve this?


Solution

  • 1st of all many thanks to Ben Tristten I took the answer from one of his aswome courses.

    On your Audio Trigger script (or a script that is on top of the game object you want to ray cast to). add the following line to the Start() or Awake() method :

    gameObject.layer = LayerMask.NameToLayer ("Ignore Raycast");
    

    Hope this helps anyone who struggles with this :)