- Home /
How do you detect which collider is triggered when you have multiple?
I have an enemy gameobject with 3 colliders attached to it, one is a trigger to tell it when something is in it's "sight" to target other enemies, there is another trigger to detect when something is hit by it's attacks, and a third that is not a trigger to detect when it gets hit by attacks. The problem is that I cant find a way to tell which of the two trigger colliders is being triggered with the OnTriggerEnter() method. I need it to do separate things when the sensor trigger is hit than when something is in it's attack range. I have all of the colliders in my code as variables so I can differentiate between them but I can't find a variable for the collider that calls the OnTriggerEnter() method. Here is the code for my enemy class:
public int health, damage;
//Linger = time the attack stays active, Delay = for the player to dodge attacks before they activate
public float linger, delay, range, attackRange, speed;
//Accumulation of time between updates to tell how ling it's been since an attack started
private float timePassed;
private Transform target;
private bool attacking = false;
//Two trigger colliders sensor is used to aquire a target, attackCol is used to deliver attacks
public SphereCollider sensor, attackCol;
public void Start()
{
attackCol.enabled = false;
sensor.radius = range;
attackCol.radius = attackRange;
}
public void Update()
{
if (attacking)
{
timePassed += Time.deltaTime;
}
if (timePassed >= delay)
{
attack ();
}
if (timePassed >= linger + delay)
{
attacking = false;
attackCol.enabled = false;
timePassed = 0;
}
if (target != null && !attacking)
{
attackTarget(target, Time.deltaTime);
}
}
public void attackTarget(Transform t, float deltaTime)
{
//Checks if the target is in attack range
float distance = Mathf.Sqrt (Mathf.Pow (t.position.x - transform.position.x, 2) + Mathf.Pow (t.position.y - transform.position.y, 2));
if (distance > attackRange / 2) {
attack ();
attacking = true;
} else {
//Faces and moves toward the target
transform.rotation = Quaternion.Euler(0, getAngle(transform.position, t.position) * (180/ Mathf.PI), 0);
transform.position += new Vector3(Mathf.Cos(getAngle(transform.position, t.position)), 0, Mathf.Sin(getAngle(transform.position, t.position))) * speed * deltaTime;
}
}
public void attack()
{
attackCol.enabled = true;
}
public void OnTriggerEnter(Collider other)
{
if(other.gameObject.GetComponent<Player>() != null)
{
target = other.transform;
}
}
The colliders are set in the inspector and although you cant tell the difference by name I made sure they are not the same collider listed twice
And both colliders show up in the editor when the game is run
However the script cannot tell the difference between the two trigger colliders
Answer by atifbutt · Dec 09, 2016 at 03:33 PM
Place two child gameObjects with different tag names after that you will identify which collider its triggered
I was really hoping to do it all without child objects but this works too thanks
Answer by RobAnthem · Dec 11, 2016 at 05:52 AM
public void OnTriggerEnter(Collider other)
{
if(other.gameObject.GetComponent<Player>() != null)
{
if (other.collider == other.gameObject.GetComponent<Player>().attackCol)
target = other.transform;
}
}
Your answer
Follow this Question
Related Questions
Can't click gameobject when over another trigger? 1 Answer
OnTriggerEnter fails to activate 1 Answer
OnTriggerEnter firing twice 1 Answer
Enemy line of sight using linecast and colliders 1 Answer
Rigidbody Trigger 1 Answer