Yup!
Simply have it check the tag of the colliding object, and have it perform the corresponding action.
For example:
public void OnTriggerEnter(Collider col) {
// Grab a referance to the triggering object's
// tag.
string tag = col.tag;
// Checks the tag of the triggering object
// and compares it to the cases below.
switch (tag) {
case "Player":
// Player related code
break;
case "Wall":
// Wall related code
break;
case "Door":
// Door related code
break;
case "Ceiling":
// Ceiling related code
break;
// ETC
}
}
I think you or I misunderstand, it’s not about having multiple “Ifs” attached to 1 collider.
It’s about having rules for multiple colliders in 1 script.
It’s not about 1 gameobject having rules for 10 different things touching it in a script.
It’s about having rules for 10 different colliders in a script that all do 1 thing when “Player” comes in contact.
Well, each object would need its own script then, as well as its own Trigger collider.
If you want it all in a single script, where you don’t have to create a script for each object, then switch my above code to check the object’s tag (the object that has the trigger, not the object doing the triggering).
Like so:
public void OnTriggerEnter(Collider col) {
// Grab this trigger's tag.
string tag = gameObject.tag;
// Compares this object's tag with those below.
switch (tag) {
case "Player":
// Player related code
break;
case "Wall":
// Wall related code
break;
case "Door":
// Door related code
break;
case "Ceiling":
// Ceiling related code
break;
// ETC
}
}
But you would still need to put this script on each object, and there really is no way around it.
No matter what you do, you will need to put a script on an object with a trigger, either to just detect that it has been triggered and send this information to another script to deal with, or to handle it’s own trigger events.
That said, do the above, and you will not need to make a script for each object, you will just have to put the script on each object.
To put it simply, in order to detect a trigger, a script with an OnTrigger event must be present on the object with the Trigger collider. You cannot have an outside script detect when another game object’s trigger has been tripped.
Hehe, thanks. At my laptop, only had paint and had to make due.
At any rate, thanks for your clarification, really. I’ve revised the script and works better than intended with a prefab structure with public properties. (It’s for a system in which the camera moves depending on your location)
Thanks, lads! I really love this forum some times.