添加优化,实时移动时 当目标点距离很小,直接设置目标点位置
// 距离
var distance = Vector3.Distance(points[i], unit.trans.position);
if (distance > unitSpeed)
{
// 方向
var dir = points[i] - unit.trans.position;
// 线性差值设置方向,朝向目标点方向
unit.trans.forward = Vector3.Lerp(unit.trans.forward, new Vector3(dir.x, 0, dir.z), 5 * Time.deltaTime);
// 动画混合
unit.speed = distance > 0.8f ? distance : 0.8f;
unit.ani.SetFloat("Speed", unit.speed);
// 移动
unit.trans.position = unit.trans.position + (points[i] - unit.trans.position).normalized * unitSpeed;
}
else
{
// 距离很小,直接设置目标点位置
unit.trans.position = points[i];
}
using System;
using UnityEngine;
using UnityEngine.AI;
using Debug = UnityEngine.Debug;
public class MoveNavMeshAgent : MonoBehaviour
{
public NavMeshAgent agent;
[SerializeField]
bool _isMoveing = false;
[SerializeField]
float remainingDistance;
[SerializeField]
Vector3 curr_destination;
[SerializeField]
Vector3 last_destination;
private Transform Transform;
void Awake()
{
agent = gameObject.GetComponent<NavMeshAgent>();
if(!agent)Debug.LogError(gameObject.name + " no NavMeshAgent compent");
Transform = gameObject.transform;
agent.updateRotation = true; //允许NavMesh来旋转角色
agent.updatePosition = true; //允许NavMesh来移动角色
agent.enabled = false;
}
/// <summary>
///使用了NavMeshAgent就不能用 transform.position = newPos;用 WarpPoint(newPos)代替或者移动赋值前先NavMeshAgent.enabled = false
/// </summary>
/// <param name="destination"></param>
public void WarpPoint(Vector3 destination)
{
if (agent.enabled == false)
{
agent.enabled = true;
}
agent.Warp(destination);
agent.enabled = false;
}
/// <summary>
/// 是否在移动中
/// </summary>
/// <returns></returns>
public bool IsMoving
{
get
{
if (agent != null && agent.enabled)
{
remainingDistance = Vector3.Distance(Transform.position, agent.destination);
_isMoveing = agent.pathPending || remainingDistance > agent.stoppingDistance || agent.velocity != Vector3.zero;
}
else
{
_isMoveing = false;
}
return _isMoveing;
}
}
/// <summary>
/// 是否移动结束
/// </summary>
/// <returns></returns>
public bool IsMoveEnd
{
get
{
return !IsMoving;
}
}
//停止移动
public void StopMove()
{
if (agent.enabled == true) //停止NavMeshAgent ,停止移动
{
agent.ResetPath();
agent.Stop();
agent.enabled = false;
}
}
/// <summary>
/// 移动到点
/// </summary>
public void MovePoint(Vector3 destination, float stoppingDistance = 0.17f)
{
curr_destination = destination;
if (agent.enabled && curr_destination == last_destination && IsMoving && Math.Round(stoppingDistance, 2) == Math.Round(agent.stoppingDistance, 2))//与上一次目的地相同,就跳过
{
return;
}
if (agent.enabled == false)
{
agent.enabled = true;
}
agent.stoppingDistance = stoppingDistance;
agent.destination = destination;
last_destination = curr_destination;
}
/// <summary>
/// 移动到距离敌人包围盒最近的点,如果是远程英雄的话 stoppingDistance 就是攻击距离,
/// </summary>
/// <param name="stoppingDistance"></param>
/// <param name="enemyCollider"></param>
public void MoveToEnemy(Collider enemyCollider, float stoppingDistance = 0.17f)
{
Vector3 destination = enemyCollider.ClosestPointOnBounds(Transform.position);
MovePoint(destination, stoppingDistance);
}
/// <summary>
/// 绘制移动路线
/// </summary>
void OnDrawGizmos()
{
if (agent != null && agent.enabled == true)
{
var path = agent.path;
// color depends on status
Color c = Color.white;
switch (path.status)
{
case UnityEngine.AI.NavMeshPathStatus.PathComplete:
c = Color.white;
break;
case UnityEngine.AI.NavMeshPathStatus.PathInvalid:
c = Color.red;
break;
case UnityEngine.AI.NavMeshPathStatus.PathPartial:
c = Color.yellow;
break;
}
// draw the path
for (int i = 1; i < path.corners.Length; ++i)
Debug.DrawLine(path.corners[i - 1], path.corners[i], c);
}
}
public void FixedUpdate()
{
#if UNITY_EDITOR
var ShowState_Debug = IsMoving;
#endif
}
}
以前的:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class mouseNavMove : MonoBehaviour {
public GameObject clickPrefab;
private NavMeshAgent agent;
public float minDistance = 3f;
private Animation anim;
/// <summary>
/// 鼠标点击地面的点
/// </summary>
Vector3 hitPoint;
private void Awake()
{
agent = this.GetComponent<NavMeshAgent>();
anim = this.GetComponent<Animation>();
anim["walk"].wrapMode = anim["idle"].wrapMode = WrapMode.Loop;
}
// Update is called once per frame
void Update()
{
//获取鼠标点击地面的点
if (Input.GetMouseButtonDown(1))//鼠标右键瞬间按下
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "Terrain")
{
hitPoint = hit.point;
}
}
//鼠标点击地面的点 生成点击圆圈特效
float height = Terrain.activeTerrain.SampleHeight(hitPoint);//获取当前Terrain的高度
hitPoint.y = height + 0.3f;
Instantiate(clickPrefab, hitPoint, Quaternion.identity);
//开始移动
SetDestination(hitPoint);
Debug.LogWarning("角色向" + hitPoint + "移动");
}
if (agent.enabled)
{
//是否在移动中
bool isMoveing = agent.pathPending ||
agent.remainingDistance > agent.stoppingDistance ||
agent.velocity != Vector3.zero;
//没有移动,就停用导航
if (!isMoveing)
{
agent.Stop();
agent.enabled = false;
anim.CrossFade("idle",0.1f);
}
}
}
public void SetDestination(Vector3 targetPos)
{
agent.enabled = true;
anim.CrossFade("walk", 0.1f);
agent.SetDestination(targetPos);
}
}