How to activate a trigger for two animations?

Hello everyone,

I have very beginner level with animations, thus a beginner question.

I have a enemy that attacks the player if close enough. I’m trying to get the mechanim to activate an attack animation when close to the player. The issue is the trasition between the enemy walking and attack isn’t functioning. I have called the walking state “Walk” and the attack state “Attack” with two transitions and added a trigger to each one of them.

Walk —>Attack (the trigger for the transition is called attack)
Attack —>Walk (the trigger for the transition is called walk)

I have just understood that I need a script to activate those but don’t really understand what’s online…
I’m trying to modify a script I found online but it gives me errrors

using UnityEngine;
using System.Collections;
 
public class TriggerScript : MonoBehaviour
{
     Animator anim;
   
     void Start(){
         anim = GetComponent<Animator>();
     }
 
     void OnTriggerEnter(Collider other)
     {
         if(collision.transform.tag == "Enemy")
            {
                 anim.setTrigger("Attack");
            }
           
     }
}
}

I’ll take any help given :slight_smile:

R u doing bools?

no i’m using trigger parameters

@Hidrem I see. But why don’t you create 2 bools to track is player within(or not) the range of enemy attack and on that dependence build your script and transitions?

@diagymiaw
I would love too but i’m not sure how to do this. The bools you are talking about would be in the transition system in mecanim right? True or false? But for the scripting part i’m not very good at it.

@Hidrem Yep. When the player is out of range of the enemy, your bool for Walk will be active and the enemy will chase player, so when he reaches the player, your next bool which is response for attack will became active and the enemy will attack the player. Of course with all conditions checked in the transitions.

@diagymiaw Okay that makes more sense now but earlier you said that i’d need to script that? Any help on that?