- Home /
OnTriggerEnter - Same collider triggers multiple times?, is size a factor?
I have a collider set as a trigger on my moving rigidbody, which is the size of the rigidbody. I have another trigger, which detects when my rigidbody goes through, and prints the name of the rigidbody collider.
When the rigidbody collider enters the trigger, it prints the rigidbody's name more then once (up to 5 times). But if I reduce the size of the rigidbody collider so that it's much smaller then the trigger checking for it, it only prints the name once.
Does the size of the other collider matter? If the collider entering the trigger is bigger then said trigger, is it going to detect the collision more then once?
Thanks for your time! Stephane
After the trigger, are you doing anything in code to end
the collision (i.e., removing one of the GameObject
s) ?
@kolban - I wish I had high enough rep, level, whatever it's called on this platform, to upvote that comment. xD
No I'm not, I just detect OnTriggerEnter() and that's it. I have tried using a bool to tell the trigger that it's been triggered already, but it still happens a $$anonymous$$imum of twice.
Answer by bodec · Jun 12, 2012 at 04:36 AM
This is a problem i recently had and asked about and was asked before here is the link to the answer. "Edited from here." here is the chunk of code i have been using with the most up to date version of unity and it seems to be working fine.
function OnTriggerEnter(col : Collider){
if(col.tag == "waypoint" && !isTriggered ){
isTriggered = true;
ChangeWayPoint();
}
}
function ChangeWayPoint(){
if(waypoints != 11){
curWayPoint ++;
isTriggered= false;
print(curWayPoint);
Yes I tried setting a flag like in the example, but like I said above, it still happens twice no matter what I do...weird.
Any ideas on this because I have made 6 diiferent checkpoint systems and they all fail from this multiple trigerr thing :( driven me nuts for months coz I thought I was the one doing it wrong fot months :(
Answer by vekkna · Apr 06, 2014 at 09:22 PM
I had a similar problem today. It turned out I somehow had two "Circle Collider 2D components attached to the offending object. Removing the extra one via the inspector got rid of the extra onTriggerEnter.
Answer by Gunarvingren GP · Jul 12, 2015 at 01:56 PM
In my case the problem was that both colliders (player and enemy) was flagged as "IsTrigger", this way enemy collider fire OnTriggerEnter, then in the same time player collider also fire OnTriggerEnter. To solve that i just set the player flag "IsTrigger" to false.
Answer by Gunarvingren GP · Jul 12, 2015 at 01:56 PM
Here is the definitive solution i found to this problem:
void OnTriggerEnter(Collider enemy) {
enemy.hitCount++;
if(enemy.hitCount==1){
enemy.doStuff();
}
}
void OnTriggerExit(Collider enemy) {
enemy.hitCount--;
if(enemy.hitCount<0){
enemy.hitCount=0;
}
}
In this case go to the enemy script and add the variable:
public int hitCount=0;
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Terrain doesn't collide with my mesh 1 Answer
Scoring Issue 0 Answers
Trigger Enter and Exit not working properly? 2 Answers
Question about collision and trigger 2 Answers