Search code examples
unity-game-enginecollision-detection

Destroy an enemy with spotlight


function OnCollisionEnter(theCollision : Collision)
{
    if(theCollision.gameObject.name=="Spotlight") 
    {
        Destroy(gameObject);
        Debug.Log("Dead");
        dead = true;
    }
}

This is my code here, I have a spotlight attached to my enemy which acts like a torch, what I want is for the enemies to be destroyed once they walk into the spotlight.

I tested the collider with the player and it works fine but for some reason, when I set it to the spotlight, nothing happens at all.

Can anyone help me out here?


Solution

  • You can ray cast from the origin of the light to your surface. Once you have found the intersection point of this ray with your surface you can control the XZ position of a capsule or sphere collider of size(radius, N, radius) - attach your detection script to that collider and you'll be able to work with it as if it were a physical object in the scene.