unity 怪兽到监测距离时怪兽只有走路动作没有走路的移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GuaiWu : MonoBehaviour
{
public enum EnemyState
{
idle,
run,
attack,
death,
}
public EnemyState CurrentState = EnemyState.idle;
Animator imator;
UnityEngine.AI.NavMeshAgent agent;
Transform Player;
// Start is called before the first frame update
void Start()
{
imator = GetComponent<Animator>();
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
Player = GameObject.FindWithTag("Wolf").transform;
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(Player.position, transform.position);
switch (CurrentState)
{
case EnemyState.idle:
if (distance > 2 && distance <= 10)
{
CurrentState = EnemyState.run;
}
if (distance < 2)
{
CurrentState = EnemyState.attack;
}
imator.Play("Idle");
agent.isStopped = true;
break;
case EnemyState.run:
if (distance > 10)
{
CurrentState = EnemyState.idle;
}
if (distance < 6)
{
CurrentState = EnemyState.attack;
}
imator.Play("Run");
agent.isStopped = false;
agent.SetDestination(Player.position);
break;
case EnemyState.attack:
if (distance > 2)
{
CurrentState = EnemyState.idle;
}
if (distance < 1)
{
CurrentState = EnemyState.attack;
}
imator.Play("Attack");
agent.isStopped = false;
agent.SetDestination(Player.position);
break;
}
}
}