Check if character has entered trigger, if it has play animation on character.
I'm trying to problem solve an issue I'm having.
Basically I have a trigger zone, and when a character enters the trigger zone, I want an animation to play on the character that has just entered the trigger zone.
I vaguely understand that to make this happen I need to attach a script to the trigger zone that says
Play("Jump1", PlayMode.StopAll);
But I don't know how to get the trigger to recognize when and which character has entered it, so the animation plays on the character inside the trigger, not the several instantiated ones outside the trigger that are tagged the same.
Is there a way I can change a boolean value only on the character that has entered the trigger?
Answer by Nitin22 · Jul 27, 2016 at 06:14 PM
First tag your players on gameobject . After that just check the tag and do your rest work. Like :
void OnTriggerEnter(Collider other) {
if(other.tag.Equals("Player")){
//Rest Work
}
}
Thanks for your reply Nitin22,
I tried at first doing it like this:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "triggerZone")
{
enemyAnimations.Play("Jump", Play$$anonymous$$ode.StopAll);
}
}
This script is attached to the enemy characters, from my understanding when the enemy enters the triggerZone it should make the "Jump" animation play... only it doesn't work :|
The triggerZone has a collider and is set to trigger, the enemies also have colliders, I tried giving them rigidbodies but it didn't make a difference...
Please check first tag is assigned or not . If it is then print in debug to get the result and figure out what actually going on .
Tag is definitely assigned correctly but debug does not detect any collisions of enemy with the triggerZone, it's like it's not even there.
I've checked the animations are working by adding a public boolean value to the script along the lines of:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "triggerZone")
{
Debug.Log(gameObject.name + " was triggered by " + other.gameObject.name);
triggerZone = true;
}
}
public void Update()
{
if (triggerZone)
{
enemyAnimations.Play("Jump", Play$$anonymous$$ode.StopAll);
}
}
and switching it on manually during game Play, so I know the animation works etc.
On the enemies I have two colliders, one that's a trigger for a different script (tied to a scoreboard), the other so they don't walk through each other.
I tried deleting the scoreboard script just to see if it was the cause of the problem but it didn't help, debug doesn't detect any enemy collision with the triggerZone.