- Home /
Using two trigger colliders
As the title says, I am needing help with two trigger colliders. I am creating a co-operative puzzle game about two cubes, a big cube and a small cube. In this game I am creating a turret that finds and shoots the cube. However, I want the big cube to be detected anywhere around the turret in a circular range (currently working) and the small cube to only be detected in a smaller cube range in front of the turret (not working). In a sentence, my problem is I am trying to get input from the different triggers separately, and currently just using OnTriggerStay/Enter/Exit only detects the big circle collider (as it is the biggest). Here is a picture if it helps. Notice the big collider, then the small one.
Thank you if you can help, and sorry if my explanation is a little vague, also I work in C# but java is fine if that is all you know. I already made some code for detecting the big cube, as seen below, but I need a way to detect them separately. (I have edited it a bit to show you more or less how the small cube should function.)
public Transform target;
public Transform target1;
public GameObject barrel;
public bool inrange;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update() {
}
void OnTriggerEnter(Collider other) {
if (other.tag == "Player1") {
inrange = true;
barrel.particleSystem.Play ();
}
if (other.tag == "Player2") {
inrange = true;
barrel.particleSystem.Play ();
}
}
void OnTriggerStay(Collider other) {
if (other.tag == "Player1") {
transform.LookAt (target);
}
if (other.tag == "Player2") {
transform.LookAt (target);
}
}
void OnTriggerExit(Collider other) {
inrange = false;
}
}
Answer by curiouspers · Mar 02, 2015 at 10:41 PM
As far as i know you can't get data about what trigger triggered the action, and it's not recommended to use more than 1 trigger on one gameObject.
However there is a workaround:
You may attach your triggers to 2 child gameObject of your turret. Then make a simple script wich onCollisionEnter will call custom function of your turret script with 2 params, the object that calling this function and Collider information that has triggered it.
This should work, but you want to modify it for you:
Here i assuming that children gameObjects is named TriggerRound and TriggerFront.
// on child gameObject with trigger
public class TurretChildScript : MonoBehaviour {
private TurretScript parentScript;
void Start(){
parentScript = transform.parent.GetComponent<TurretScript>();
}
void OnTriggerEnter(Collider other){
parentScript.recieveTriggerEnter (name, other);
}
}
//and on parent gameobject (turret):
public class TurretScript : MonoBehaviour {
public void recieveTriggerEnter(string fromObject, Collider other){
if(fromObject == "TriggerRound"){
if (other.tag == "Player1") {
transform.LookAt(other.transform);
Debug.Log(fromObject);
}
}
if(fromObject == "TriggerFront"){
if (other.tag == "Player2") {
transform.LookAt(other.transform);
Debug.Log(fromObject);
}
}
}
}
If you want OnTriggerStay or OnTriggerExit, you can add it in both scripts
If this was helpfull please upvote the answer and mark it as checked. Best regards!
I think this will work. I'll try it out as soon as I finish school! Thank you!
I was trying to control 2 triggers on 2 different objects at the same time and was stuck for like an hour. Your answer helped alot so thank you so much!