- Home /
OnTriggerEnter triggering twice on one collider?
I'm Make a Melee Attack system using a hit box to damage everything close by, i'm doing this by having a collider on my player at the front set to "Trigger", then im getting a reference to the objects im attacking by adding anything inside it to a list. The issue is that if i move towards the other object at a certain angle its added to the list twice and im dealing double the damage? Any idea why its doing it? :)
Answer by TonyLi · Jan 08, 2014 at 03:24 PM
It could be a Physics setup issue. It the hit box is a mesh collider, try ticking Convex. Also, depending on how you move/animate your character, maybe you can move in FixedUpdate(). I can set up a scenario, for example, that intermittently "double reports" OnTriggerEnter by giving the player a CharacterController that's moved by CharacterController.Move() in Update().
I also came across this good answer with guidelines on Physics movement and collision detection: http://answers.unity3d.com/questions/7671/guidelines-for-using-rigidbody-collider-characterc.html
[EDIT 1: Also check that your objects don't have two or more colliders.]
[EDIT 2: If you're using rigidbodies, use Rigidbody.MovePosition() instead of changing transform.position directly.]
$$anonymous$$y problem was updating my rigid body's velocity in Update()
ins$$anonymous$$d of FixedUpdate
. Thanks so much!
Sigh, never$$anonymous$$d. $$anonymous$$y issue still persists with FixedUpdate()
:(
Do your objects perhaps have two or more colliders? For example, if an NPC has separate colliders for body parts or attachments (e.g., weapons), when the player's trigger collider intersects with the NPC it will receive an OnTriggerEnter message for each collider.
$$anonymous$$an this is an old question! $$anonymous$$y issue was not realising that scripts like character controllers and nav mesh agents act as colliders as well. So the double up was due to it getting the Collider I had on it as well as the Character controller.
Ah I see, I don't think that's my issue :) Thanks for responding to such an old question though!
TonyLi, you're a legend! Using Rigidbody.$$anonymous$$ovePosition() solved my issue :) Thanks so much!
Answer by Tomer-Barkan · Jan 08, 2014 at 12:24 PM
I'm not sure why it would be added twice to the list, maybe something to do with the melee attack mechanics that you use, but anyway the solution is quite simple - use a hashset instead of a list, that will eliminate all duplicates.
To convert a list to a hashset (hence removing all duplicates):
HashSet<Collider> uniqueCollidrs = new HashSet<Collider>(colliderList);
This raises more issues as now this finds all the colliders, so I need a way to differ between them, before I was using "Compare Tag" so that only one of the colliders was being triggered, which worked fine, except for just one side on the other player it would pick up 2. it dosn't make any sense to me
if you want one collider per tag, you can use the following linq command:
List<Collider> uniqueColliders = (from collider in colliders
group collider by collider.tag into tagGroup
select tagGroup.First()).ToList();
Don't forget to add using System.Linq;
.
Will that work if all the colliders I want to hit have the same tag?
I'm not sure I understand what you want to do anymore... Do you want all the colliders that have some tag, but without duplicates? Or do you just want each tag to appear once in the list (ie you won't have two colliders in the list that have the same tag)?
Why don't you share your current code, it might help shed some light.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class $$anonymous$$ELEEDamage : $$anonymous$$onoBehaviour {
[SerializeField]
private List<GameObject> m_aEnemys = new List<GameObject>();
// Use this for initialization
void Start ()
{
transform.gameObject.tag = "Enemy";
}
// Update is called once per frame
void Update () {
}
protected virtual void OnTriggerEnter(Collider enemys)
{
if(enemys.gameObject.CompareTag("Enemy"))
{
m_aEnemys.Add(enemys.gameObject);
Debug.Log("Hi");
}
}
protected virtual void OnTriggerExit(Collider enemys)
{
m_aEnemys.Remove(enemys.gameObject);
}
}
So I want it to list any player in that trigger collider to be listed so that i can then deal damage to all of them in it, but also its critical it can work on 1 tag name, in this case Enemy, as im using the photon System which basically makes multiple clones of the same player so the tag can not be unique between players.
Answer by yaapelsinko · Apr 28, 2014 at 08:35 AM
I'm facing same problem now.
There is two colliders and I use an additional script component to give them types. So when I want to know what exactly one collider have collided to, I do:
void OnTriggerEnter(Collider other)
{
Debug.Log(other.name)
switch (other.gameObject.GetComponent<ColliderType>().Type)
{
....
}
}
Then in log I see "AttackTrigger" name has shown twice, and second is followed by NullReferenceException. For second time it can't GetComponent() from exactly the same object. This is strange and inconvenient as I have to invent some additional checking for not to fire an event twice.
Answer by Olakehs · Dec 14, 2019 at 04:17 AM
It seems Destroy(other.gameObject) took too long, so I put a other.gameObject.SetActive(false) before and it solved the problem for me
Answer by Sun_Jie · Dec 24, 2020 at 04:56 PM
gameobject[A] has rigibody component,gameobject[B] is a trigger.[A] Initiatively touch [B]. Don't write OntriggerEnter into [B],please put OntriggerEnter script on [A]. It worked for me!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Weird gravity when enabling gravity OnTriggerEnter 0 Answers
I'm trying to adjust tagged objects meshrenderer in C# code. 1 Answer