敌人ai自动寻路时在导航点前停住,导致后面的导航都失败,unity版本是2017.1.1p4,运行网盘里的项目代码敌人的自动寻路也是有问题的
EnemyAnimation
using UnityEngine;
using System.Collections;
using UnityEngine.AI;
public class EnemyAnimation : MonoBehaviour
{
public float speedDampTime = 0.3f;
public float anglarSpeedDampTime = 0.3f;
private NavMeshAgent navAgent;
private Animator anim;
void Awake()
{
navAgent = this.GetComponent<NavMeshAgent>();
anim = this.GetComponent<Animator>();
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (navAgent.desiredVelocity == Vector3.zero)
{
anim.SetFloat("Speed", 0, speedDampTime, Time.deltaTime);
anim.SetFloat("AnglarSpeed", 0, anglarSpeedDampTime, Time.deltaTime);
}
else
{
float angle = Vector3.Angle(transform.forward, navAgent.desiredVelocity);
float angleRad = 0f;
if (angle > 90)
{
anim.SetFloat("Speed", 0, speedDampTime, Time.deltaTime);
}
else
{
Vector3 projection = Vector3.Project(navAgent.desiredVelocity, transform.forward);
anim.SetFloat("Speed", projection.magnitude, speedDampTime, Time.deltaTime);
}
angleRad = angle * Mathf.Deg2Rad;
Vector3 crossRes = Vector3.Cross(transform.forward, navAgent.desiredVelocity);
if (crossRes.y < 0)
{
angleRad = -angleRad;
}
anim.SetFloat("AnglarSpeed", angleRad, anglarSpeedDampTime, Time.deltaTime);
}
}
}
EnemyMoveAI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyMoveAI : MonoBehaviour {
public Transform[] wayPoints;
public float patrolTime = 3f;
private float patrolTimer = 0;
private int index = 0;
private NavMeshAgent navAgent;
void Awake()
{
navAgent = this.GetComponent<NavMeshAgent>();
navAgent.destination = wayPoints[index].position;
navAgent.updatePosition = false;
navAgent.updateRotation = false;
}
void Update()
{
patrolling();
}
private void patrolling()
{
if (!navAgent.pathPending && navAgent.remainingDistance < 0.5f)
{
patrolTimer += Time.deltaTime;
if (patrolTimer>patrolTime)
{
index++;
index %= 4;
navAgent.destination = wayPoints[index].position;
navAgent.updatePosition = false;
navAgent.updateRotation = false;
patrolTimer =0;
}
}
}
}
运行截图