- Home /
Is there an equivalent to OnCollisionExit for OnControllerColliderHit?
I am trying to make a 2D characterController climb an object. I can get them to start moving up when they come into contact with that object using:
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.transform.tag == "treeTrunk")
{
ClimbingTree();
}
}
but unfortunately I can't figure out how to tell it to stop when it reaches the top and is no longer touching the object anymore. Any suggestions?
I don't really understand the problem. I was under the impression that OnControllerColliderHit
is called only when you call controller.$$anonymous$$ove()
?
So you want a function called, when controller.$$anonymous$$ove()
does not call OnControllerColliderHit
?
You could just set a bool member variable to false before calling controller.$$anonymous$$ove, then set it to true in OnControllerColliderHit and if its still false after the $$anonymous$$ove, then no collider hit was called.
But this seems rather ... .. "indirect" to me :). So I guess I just didn't understood your problem. :(
Sorry, maybe I'm trying to use the OnControllerColliderHit function wrong. I'll just explain my problem and maybe there is another function that will work better for my situation.
I have a characterController that walks automatically along a 2D plane. I want it to begin climbing (moving up) automatically when it collides with an object. But once the characterController reaches the top of the object, it should stop moving up and begin walking across the top of the object. How can I tell my characterController that it is not touching the side of the object anymore so that it stops moving up? Hopefully that makes sense.
Should've tried this before, but I added a rigidbody2D to my ground and now I get onexit calls. This is still not an ideal solution. $$anonymous$$y ground is actually planets that float around and the player can jump between them. Adding a rigidbody2D allows them to move. I suppose I can just raise their mass or force them not to move with a script, but I would prefer not to. I've also checked $$anonymous$$inematic, which is fine for now but later I might want to have them orbit and move with forces.
Any info on the original question would still be helpful.
Answer by Immanuel-Scholz · Jun 29, 2013 at 09:06 PM
If your "climbing" surface is the same for all obstacles and the only one colliding with the obsticles is your player, then I recommend you use OnTriggerEnter
and OnTriggerExit
to detect when the player hits an obstacle.
If you need more information about the collision (e.g. to tweak your animation), you can use OnCollisionEnter
/OnCollisionExit
instead which gives you some information about the contact point and normal.
Thanks for your response. I'm still having an issue though. I changed it to:
function OnCollisionEnter (theCollision : Collision)
{
if(theCollision.gameObject.tag == "treeTrunk")
{
isClimbing = true;
Debug.Log("Touching the tree");
}
}
But this does not seem to work. It won't even print the text for me. Is there something wrong in my code here?
Hm.. do both objects have a rigid body? And at least one of them is non-kinematic? (reference documentation)
No. One is a characterController and the other is just a cube with a box collider. So OnCollsionEnter only works with two rigidbodies?
Your answer
Follow this Question
Related Questions
Struggling with jumping! 3 Answers
2d Ladder with character controller 2 Answers
Collision Detection 0 Answers
Increasing Camera "Bobbing" speed when character is sprinting 1 Answer
Character Controller moves forever after colliding with object 1 Answer