- Home /
Does OnCollisionEnter only get called when the scripted object initiates the collision?
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?
Answer by Dreamblur · Jul 07, 2011 at 07:15 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.
I just ran into the same issue, and I'm not using a character controller. Turns out a sleeping rigidbody won't generate OnCollisionEnter messages for any collider. $$anonymous$$y.
Answer by owenpat · Feb 28, 2011 at 01:56 AM
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.)
Yes, I think the character controller component is the one causing the problem as well. The rigidbody calls the OnCollisionEnter method when it's the one colliding with a non-moving character controller, but nothing happens when said rigidbody is the one not moving.
Answer by AngryOldMan · Feb 27, 2011 at 11:33 PM
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.
What I basically want is some way for the "character" (a game object with a character controller and a collider) to activate OnCollisionEnter from a script attached to a non-moving game object with a non-kinematic rigidbody and a collider. When I say "initiate the collision," what I mean is that the object is moving and colliding into a non-moving object. In all the testing that I've done, OnCollisionEnter on the non-kinematic rigidbody doesn't get called when the non-kinematic rigidbody is not moving and the character controller is.
Answer by AngryOldMan · Feb 28, 2011 at 02:04 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.
var playerPrefab : Gameobject;
function OnTriggerEnter (collision : Collider)
{
if (collision.gameObject.CompareTag ("Player"))
{
JumpPlayer = playerPrefab1.GetComponent(PlayerMoveScript);
JumpPlayer.JUMPON = JumpPlayer1.JUMPON = true;
}
}
Then on the PlayerMoveScript something along the lines of:
static var JUMPON : boolean = false; var jumpAccel : int; var jumpHeight : int;
if (JUMPON == true) { moveDirection = transform.TransformDirection(moveDirection*jumpAccel); moveDirection.y = jumpHeight; JUMPON = false; }
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!
Your answer
Follow this Question
Related Questions
CharacterController and Normal rigidbody Box Collider Collision issues 0 Answers
Two Character Controllers Colliding... 0 Answers
Collision Only being detected on one of the objects involved in the collision - C# 0 Answers
Character Controller goes through objects 2 Answers
Using rigidbody for collisions only, not movements? 1 Answer