How to get "on trigger enter"( or exit) to only react to a specific tag
I have tried to modify the code that I downloaded from the asset store to only react to a specific tag, such as box. The would collider would serve as a button to open a door by playing an animation. I have tried a few different ways to tweak the code so it will work with a specific tag, but I know very, very little about how to code. Only the "button" part doesn't work, the script works just fine otherwise. It is in "javascript".
Can someone explain, or send me a link to a tutorial? I would very much appreciate it. This is the 'base' code, one i did not edit.
pragma strict
function OnTriggerEnter (obj : Collider) { var thedoor = gameObject.FindWithTag("SF_Door"); thedoor.GetComponent.().Play("open"); }
function OnTriggerExit (obj : Collider) { var thedoor = gameObject.FindWithTag("SF_Door"); thedoor.GetComponent.().Play("close"); }
Answer by Umresh · Oct 16, 2015 at 05:14 AM
Check if the collider object's tag is "Door". Like this
function OnTriggerEnter (obj : Collider) {
if(obj.gameboject.tag == "SF_Door")
{
obj.GameObject.GetComponent(componentname).Play("open");
}
}
Your answer
Follow this Question
Related Questions
Get list of enemies in range 1 Answer
Need Help With Collider Script. 2 Answers
OnTriggerEnter2D not working 1 Answer
Javascript error on runtime semi colon required 1 Answer
How to let someone win in a racing game 0 Answers