- Home /
OnTriggerExit happens too soon!
I am working on a 2D platformer, and when the box collider of my rigidbody character inches off the side, the "ontriggerexit" function is switched on, and the character shifts into the jumping pose. What script can I use to tell unity to preform such a command only when ALL of the character's collision exits the collision it stands on??
here's a video of what is happening: http://youtu.be/UUrTgSostBg
Here is my ontriggerexit/enter script:
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.tag == "floor"){
isgrounded = true;
}
}
function OnCollisionExit(theCollision : Collision){
if(theCollision.gameObject.tag == "floor"){
isgrounded = false;
}
}
just so you know, I have this script because if I dont, the character jumps into infinity. I use it so that the game knows when he is not grounded he cannot continue to jump. If you know a way around all this, let me know. Thanks!
EDIT: so I have a platform composed of a few 1 by 1 by 1 cubes. Apparently the on trigger exit function isn't use to that or something, and thats why my character is acting up. well. that's a bummer. the same problem doesnt occur on normal, longer platforms. strange..
when you enter it get them all in array or list
and when you exit them you get them all out from array or list and ONLY when they are all out make your code
I hope I understood your Q correctly
I believe you understood the question, but I'm new to unity and have no idea what an array is. $$anonymous$$ind explaining/showing?
For arrays/collections info please check this tutorial video and this wiki page.
Arrays and collections are crucial for almost all programs, so you have to know them inside out.
after watching that I'm not sure what it has to do with my issue here... All I want is for my oncollisionexit to tell unity that the ground is false only when my entire player collision actually leaves it. But at the moment, whenever a bit of it leaves the platform, the oncollisionexit is triggered. This keeps my player from jumping off the platform and kind of breaks the game... does that make my question any more clear, and if so, is there a way of fixing that? Thanks for any help!
Answer by Owen-Reynolds · Oct 29, 2013 at 04:52 PM
Trigger exit does only happen when your collider completely leaves. That's not the problem.
The problem is that you have 3 triggers. Each time you fully leave one of them, an exit happens. In your video, as the guy walks to the center of the platform, just as his back edge leaves the first trigger, trigger#1Exit fires and animates him. Not what you wanted, but following the rules perfectly.
An obvious fix is to use one big trigger.
A variant of sdgd's comment (this is an old operating system trick) is to have a count of how many triggers you are in. Enter says triggerCount++. Exit says triggerCount--. You are grounded if triggerCount is 0. If the count gets off, the game just breaks, but I think triggerEnter/Exit is reliable enough.
Or just switch to the raycast downward method.
raycast is so unbelievably beyond my scope lol. I can't understand it. But that does make sense. I wanted the triggers separate just in case I wanted to destroy the bricks a la mario.
All that aside, your array idea sounds cool. Would you $$anonymous$$d showing how that would look in code? I don't even know where to start.
sdgd is suggesting that you remember all the floor's you are touching, in a list (an array.) The simplified version is you don't care exactly what you're touching, just how many things. So you don't need an array, just one int, such as floorCount
. So what I wrote is all you need.
But program$$anonymous$$g is a whole thing. It's like giving a dog commands in Hebrew. It's more of a Hebrew problem than a dog problem. Once you learn any kind of program$$anonymous$$g, all this stuff, including raycasting, makes more sense.
Your answer
Follow this Question
Related Questions
Child calling parent's OnTriggerEnter when no Rigidbody is attached to child 1 Answer
OnTriggerEnter not working, tried everything! :( (C#) 3 Answers
Waypoint Question(Conflicting Collision) 1 Answer
Transform collider not detecting collision on rigidbody collider 2 Answers
Box collider with strange behaviour 1 Answer