1
\$\begingroup\$

I'm making a game in Unity where when the player comes into contact with an NPC it will switch scenes. I have the code for it and made sure all NPC's have rigidbody as well as using gravity and convex. They do not fall through the world but as soon as I click the trigger box on the mesh collider they start falling straight through the world. The switching scenes code works fine - I tested it and it does switch the scene.

\$\endgroup\$
1
\$\begingroup\$

When you set the isTrigger property of a Collider, it no longer registers collisions with incoming rigidbodies/colliders. As a result, your NPC falls through the floor.

If you want the scene transition to occur immediately upon contact with the NPC, you could simply uncheck isTrigger and put your scene transition code inside of OnCollisionEnter() instead of OnTriggerEnter().

\$\endgroup\$
  • \$\begingroup\$ Done that thanks makes sense but now it wont transition at all. My code for it is. Void OnCollisionEnter(Collider collider) and then a simple if statement to check the tag of the object its colliding with and if it is the right tag it will switch scenes \$\endgroup\$ – Declan Wadsworth Mar 21 at 13:32
  • \$\begingroup\$ @DeclanWadsworth Does your player also have collider/rigidbody components? \$\endgroup\$ – Alex Myers Mar 21 at 13:56
  • \$\begingroup\$ Yes both my player and object (which is a t-rex that is supposed to move around) have rigidbody and mesh colliders @Alex Myers \$\endgroup\$ – Declan Wadsworth Mar 21 at 14:36
  • \$\begingroup\$ Could you please post your OnCollisionEnter() code in a GitHub Gist or something? \$\endgroup\$ – Alex Myers Mar 21 at 14:38
  • 3
    \$\begingroup\$ Please consult the docs for the OnCollisionEnter message. Its argument is of type Collision, not Collider like OnTriggerEnter uses. Since your method signature does not match what the engine is looking for, it does not call the method when a collision occurs. Fix the signature and it will be called. \$\endgroup\$ – DMGregory Mar 21 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.