我创建了一个跟随主要玩家的 NPC。当玩家在NPC的一定范围内时,NPC应该根据玩家与NPC的距离来行走、奔跑和攻击。NPC附有一个C# MutantMovement Script,其中还包含动画。NPC 也是一个 NavMesh 代理。我的问题是动画在逻辑上运行不顺畅。我希望有人能帮帮忙。下面是 MutantMovement 脚本和动画控制器。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent (typeof (NavMeshAgent))]
[RequireComponent (typeof (Animator))]
public class MutantMovement : MonoBehaviour {
[SerializeField] private float _range = 10f;
public Transform _goal; // destination
public Transform _player;
private Animator _anim;
private bool _alive;
private float _distance;
private NavMeshAgent _agent;
void Start ()
{
_goal = GameObject.Find("Player").transform;
_player = GameObject.Find("Player").transform;
_alive = true;
// get a reference to the NavMesh Agent component
_agent = GetComponent<NavMeshAgent>();
_anim = GetComponent<Animator>();
}
void Update()
{
Vector3 direction = _goal.position - this.transform.position;
float angle = Vector3.Angle(direction, this.transform.forward);
if(Vector3.Distance(_goal.position, this.transform.position) < 10 &&
angle < 50){
_anim.SetBool("isIdle", false);
if ((_alive && direction.magnitude > 5))
{
_agent.destination = _goal.position;
_anim.SetBool("isWalking", true);
_anim.SetBool("isAttacking", false);
_anim.SetBool("isRunning", false);
if((_alive && direction.magnitude > 6.5)){
_agent.destination = _goal.position;
_anim.SetBool("isRunning", true);
_anim.SetBool("isWalking", false);
_anim.SetBool("isAttacking", false);
}
}
else{
_agent.isStopped = false;
_anim.SetBool("isAttacking", true);
_anim.SetBool("isWalking", false);
_anim.SetBool("isRunning", false);
}
}else{
_anim.SetBool("isIdle", true);
_anim.SetBool("isWalking", false);
_anim.SetBool("isAttacking", false);
}
}
public void SetAlive(bool alive)
{
_alive = alive;
}
}