0

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;
    }
}
6
  • How does your box collider work in relation to the flashlight? Are you able to get any collisions with the collider? Commented Jul 22, 2021 at 21:13
  • what collider your flashlight is using? Commented Jul 22, 2021 at 22:37
  • 1
    How are you moving your objects? Is there at least one Rigidbody involved? Do both objects have colliders that are not isTrigger? Is the name correct? Commented Jul 22, 2021 at 23:25
  • The flashlight has a box collider marked as "is trigger" extending outwards, and the object I have this code tied to has a rigid body, but no box collider, should I give it a collider of some sort? Commented Jul 22, 2021 at 23:43
  • I am completely sure the name has nothing to do with it, I've tried renaming the object, setting it to a letter so there's no confusion, and many more. Commented Jul 22, 2021 at 23:50

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.