Rigidbody, Collisions & Unity3D

Working on a new game app, and I wanted to know when my player object collided with a crate.  Thought I did everything right, but it turns out I was missing on critical thing.

Screen Shot 2015-06-06 at 8.29.49 PMMy player object had a rigidbody, neither gravity or kinetic were checked.  I added a box collider and scaled it to enclose my 3D player object.

The crates had a box collider with trigger checked, but no rigidbody.  My thought here was as I had one player and upwards of 60 crates in the scene, I could get away with one rigidbody instead of 60 which would save memory and maybe some execution time without any drawbacks.

The game has the crates moving towards the player and the player doesnt move (at the moment).

Turns out when you do this, the rigidbody attached to the player will ‘fall asleep’ and not detect any collisions.  Took me quite some time to figure that out!  To correct it, you could add a zero force to the rigidbody, or just call the WakeUp() method.  I chose the later method by adding the following two lines in my player’s Update method:

		if (rigidbody.IsSleeping())
			rigidbody.WakeUp();

That solved my problem, and my player now generated OnTriggerEnter calls!  Hopefully this post will help save someone else from the same hair-pulling exercise!

Leave a Reply