Does the OnCollisionEnter method only get called when the game object on which the script is attached to is the one which initiates the collision?
Listed below are the results of a few tests I've done that have led me to this query.
Object 1 (CC) = character controller with collider
Object 2 (RB) = non-kinematic rigidbody with collider
Case 1: CC is not moving and is below RB, RB is falling down due to gravity
Result: RB falls on top of CC, RB initiates the collision, OnCollisionEnter is called
Case 2: RB is not moving and is below CC, CC is falling due to gravity simulated by a character controller movement script
Result: CC falls on top of RB, CC initiates the collision, OnCollisionEnter is NOT called
Case 3: CC is not moving and is above RB, RB is falling up due to reversed gravity
Result: RB falls on the bottom of CC, RB initiates the collision, OnCollisionEnter is called
Case 4: RB is not moving and is above CC, CC is falling up to reversed gravity simulated by a character controller movement script
Result: CC falls on the bottom of RB, CC initiates the collision, OnCollisionEnter is NOT called
The OnControllerColliderHit method on my character controller always get called in all cases.
Several varieties of these 4 cases have also been tried, but the results were always the same.
So, like I was asking, does the OnCollisionEnter method only get called when the game object on which the script is attached to is the one which initiates the collision, or is my program just wonky?
asked Feb 27, 2011 at 11:04 PM
While answering a forum post, I realized what the actual issue was. A static rigidbody would technically be sleeping, and since collision events with character controllers aren't detected by a sleeping rigidbody, no collision message would be sent.
This is an old post. I just wanted to post the "answer" in case someone else was wondering about this. No further answers are necessary.
answered Jul 07, 2011 at 07:15 PM
Ah thank you for clarifying. you can either try what owenpat has suggested, i think it's quite common, however if your want to keep your character controller on (we've used CC from the beginning and never had a problem,plus i believe it's easier to move and customise than a rigidbody) still not sure why you want this to happen, you know a rigidbody is usually for moving a simulating physics, if it always stays still then you can just have a collider on it to act as a trigger zone. If possible a little more info about what exactly you want to achieve in game (like "i want my character, on entering the trigger, to jump") Then we may be able to help even more. on your playable character make sure it has a character controller, i don't think it needs the collider. Make sure it's tagged "Player" on your non moving game object (the one with a rigidbody) make sure it does have a collider and it is ticked to be a trigger. On the rigidbody object put a script that says something like: this states that when the player enters the trigger area (initiates collision) the object will access the players script and change a variable.
Then on the PlayerMoveScript something along the lines of:
Sorry for the forum code, we have an object with a trigger on it which our player enters to jump (its basically a jump pad) obviously change some of the variable so that it fits your game and what you need to do. im just making an exapmle based on what you've told me if im missing a trick here, tell me and i will edit this post accordingly!
answered Feb 28, 2011 at 02:04 PM
I think the problem is that CharacterControllers are a little odd. Some people here advocate for using a standard collider+RB for your character, with a redone script, if you want them to act more like "physics objects."
I recently performed a similar test where I dropped a rigidBody collider onto a (non-RB) collider, and everything worked fine -- both OnCollisionEnters got called every time, for every bounce.
I believe what happens when a CharacterCollider hits something is the same reason charControllers can't push things. Some magic jumps in and deals with the situation, stopping the character short, before it officially hits and calls the other collider.
Other things I've noticed: triggerBoxes don't work when they are moved (use SphereCasts,) and CollisionStay isn't called for objects rolling on the ground (but that was 2 versions ago.)
answered Feb 28, 2011 at 01:56 AM
I'm not sure what you mean by initiates the collision. What object has the OnCollisionEnter script attached to it and what are you actually trying to do? It may just be me but what your trying to explain isn't clear at all. To answer your question OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider. check out http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html for a bit more info on the process.
answered Feb 27, 2011 at 11:33 PM