Having problems with making a Navmesh Agent stop and play an attack animation
Hi! So I'm trying to make some simple AI code for an enemy in my game. All I want him to do is follow the player play the walk animation and if he gets close enough he stops and attacks. Now the walking part works just as I want it to the Agent follows the Player and plays the walk animation. But the stop and attack part doesn't really work he just walks into the Player and doesn't play the attack animation. Here's the code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
private NavMeshAgent Enemy;
public Transform Player;
Animator EnemyAnimator;
// Start is called before the first frame update
void Start()
{
Enemy = GetComponent<NavMeshAgent>();
EnemyAnimator = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
Enemy.SetDestination(Player.position);
if(Enemy.updatePosition = true)
{
//Reset the "Idle" trigger
EnemyAnimator.ResetTrigger("Idle");
//Send the message to the Animator to activate the trigger parameter named "Run"
EnemyAnimator.SetTrigger("Run");
}
else
{
//Reset the "Idle" trigger
EnemyAnimator.SetTrigger("Idle");
}
if(Enemy.transform.position == Player.transform.position)
{
//Reset the "Idle" trigger
EnemyAnimator.ResetTrigger("Idle");
//Send the message to the Animator to activate the trigger parameter named "Attack"
EnemyAnimator.SetTrigger("Attack");
}
}
}
Comment