Search code examples
c#unity-game-enginecollision-detection

Unity 3d collision detection


I've been tearing my hair out for weeks just trying to detect a collision between a RigidBody and a BoxCollider tied to a spot light that is tied to the camera, I want to detect when the player is flashing their flashlight at something, but for some reason this doesn't work.

I don't think it's detecting the collision at all, the variable "test" does not change and nothing appears in console, the flashlight hitbox I'm sure is large enough, but the console still gives no indication of anything happening, I was following this tutorial: https://www.youtube.com/watch?v=QRp4V1JTZnM

and here's the simple code I made:

void OnCollisionEnter(Collision col) {
    if (col.gameObject.name == "Spot_Light") {
        Debug.Log("detected");
        test = 375;
    }
}

Solution

  • If you marked trigger in your collider, you cannot use the OnCollisonEnter to detect a collison, you should use the OnTriggerEnter instead.