- Home /
OnControllerColliderHit function is not called if player is not performing a move.
Hello,
In my 2D game, there is a part where boulders falls on the player and I need to detect the collisions. If my player is not performing a move the OnControllerColliderHit function is not called and I cannot detect the collisions. I have read that I can use SimpleMove(Vector3.zero);
somewhere in the script (allowing gravity to be applied hence my player will be performing a move). But I am not using charactercontroller script, (required for SimpleMove). I am currently using the character motor + platforming Input controller.
What can I do to solve this issue? If I have to edit my character motor which lines should I look at?
Any help is appreciated. Thank you very much.
Answer by Owen-Reynolds · May 30, 2013 at 04:28 AM
If you read the docs, that's how it should work: "OnControllerColliderHit is called when the controller hits a collider while performing a Move." (emphasis is mine.) If means it only checks during CC.Move or CC.simpleMove.
To check for other stuff hitting you when it moves, just use a regular OnCollisionEnter.
It's a weird beast since charControllers are weird. They sort of move using "physics," but not really, since you don't want players to slide, shove or get shoved in the same way blocks would be.
I have read the docs, and I want to force my player to make a move which is not visible to the eye in order to force the OnControllerColliderHit to be called. OnCollisionEnter does not work with character controllers. What I want to do is something like this. He used simple$$anonymous$$ove to force his character to move, but he is using the charactercontroller script. I am using the character motor script. What adjustments do I need to make in order to get the results I want. When can I know in the character motor script that the character is not moving so I can edit it and perform my move. Hope I am more clear now.
Thank you for your reply.
OnCollisionEnter really will fire when a rigidbody hits you (tested, just now.) You can put it right on the script with the CC, next to your OnConCollideHit.
The Simple$$anonymous$$ove trick isn't what it looks like. You moving Zero doesn't seem to trigger OnContCollider. Simple$$anonymous$$ove also moves you down, so will trigger if you land on something. But that's the same as using $$anonymous$$ove with -Y. Either way, you have to be pushing into the collider to count as a ControllerHit.
The exact script doesn't matter than much. If you have a CharCont component on you, and do charCont.$$anonymous$$ove
then OnContColliderHit follows the same "did I ram it" rules to fire.
Thanks for the reply. I must be missing something because it is not working for me. In the docs it says in the description for OnCollisionEnter "OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider." Anyways you tested it and it worked so here is my script if you can figure out what is wrong.
function OnControllerColliderHit (hit : ControllerColliderHit)
{
if (hit.gameObject.tag == "Boulder")
{
var velocity = hit.rigidbody.velocity.magnitude;
if(velocity > 0.1) // Crushed By Boulder
{
dead = true;
}
}
}
function OnCollisionEnter(collision : Collision)
{
if (collision.gameObject.tag == "Boulder")
{
print("worked");
var velocity = collision.rigidbody.velocity.magnitude;
if(velocity > 0.1) // Crushed By Boulder
{
dead = true;
}
}
}
$$anonymous$$y OnControllerColliderHit is being called only when the character is perfor$$anonymous$$g a move as you stated in your comment, while OnCollisionEnter is not being called at all since my message is not appearing in the console. This script is attached to the character and has a character controller attached to it. It would be ideal if OnCollisionEnter function works. Thanks again.
$$anonymous$$ove the print statement to outside the if (maybe the collision happens and the tag is messed up.) Test by putting the script (theh OnCollision part) on just a fresh, non-moving cube and drop something on it (you know it should work there.)
I tried that still nothing. And the tag is working for OnControllerColliderHit so it should work for OnCollissionEnter. I know that the function will work if I drop something other than a character controller on a cube. I want to get a message when something hits the character when not making a move!
Answer by darthbator · May 29, 2013 at 06:30 PM
Do you happen to add or activate something like a rigidbody when you do "a move" character controllers don't seem to report collisions unless they have a rigidbody attached.
No I do not. I am using OnControllerColliderHit function which is used for detecting collisions of character controllers.