
本文共 3724 字,大约阅读时间需要 12 分钟。
如何判断NavMeshAgent到达目的地?
-----------------------------使用NavMeshAgent的destination 和nextPosition这两个 自带的属性变量
用到的的是 NavMeshAgent的destination 和nextPosition这两个 自带的属性变量。
if ((destination.x - agent.nextPosition.x <= 0.05f) && (destination.y - agent.nextPosition.y <= 0.05f) && (destination.z - agent.nextPosition.z <= 0.05f) ) { Debug.Log(" onPos "); }
destination是NavMeshAgent组件的 目的地
nextPosition是NavMeshAgent组件的 及时的位置信息
因为 两个 向量 坐标 的精度通常是(17.00001,12.000001,46.0000001),所以无法 进行 == 运算比较。
这样就只能相减,
destination.x - agent.nextPosition.x <= 0.05f
这里存在一个BUG:鼠标点击有的地方,会一直调用并显示
Debug.Log(" onPos ");
如果 目的地的点击特效在 这里进行判断,就会 显示不出来。出现BUG。(相关资料5)
用 NavMeshAgent的nav.remainingDistance参数
用 NavMeshAgent的nav.remainingDistance参数作为判断 是否到达目的地的参数。
if (Mathf.Abs(nav.remainingDistance) < 0.01)
{ animator.SetBool("Move", false);}else{ animator.SetBool("Move", true); }
NavMeshAgent的nav.remainingDistance参数有个局限,不能用于 自动烘培。
只能使用 Unity自带的静态烘培。
...if(agent.remainingDistance < 0.5){ //到达目的地}...
(2019.9.8更新)
判断 角色是否到达目的地的其他方法
1.碰撞体Collider
在目的地中设置碰撞体,BoxCollider,当角色的碰撞体 进入 目的地碰撞体,返回一个 “已经到达目的地”
目的地的碰撞体 的 代码(目的地的碰撞体BoxCollider的IsTrigger为True)
public void OnTriggerEnter(Collider other) { if ( (other.tag == "Player") && other.GetCompent().isMoving == true ) { other.GetCompent ().isDestination = true;//已经到达目的地 } }public void OnTriggerExit(Collider other) { if ( (other.tag == "Player") && other.GetCompent ().isMoving == true ) { other.GetCompent ().isDestination = false;//离开 目的地 } }
玩家状态PlayerStatus的代码(玩家的碰撞体BoxCollider的IsTrigger为false)
...public class PlayerStatus : MonoBehaviour {...public bool isMoving;//角色 是否正在 移动public bool isDestination;//是否 到达目的地...}
2.判断 角色 的坐标 和目的地的坐标
2.1两者(角色 当前的坐标Position_1 和目的地的坐标Position_2 ) 坐标的差值
和最上面的方法差不多,但是 有偏差。 把 角色 当前的坐标Position_1 和目的地的坐标Position_2 进行 比较,两者相差的值,在一个浮动的范围内。
Vector3 pos_ = Positon_1 - Positon_2if((pos_.x < 1)&&(pos_.y < 1)&&(pos_.z < 1)){//判断为到达目的地...}
注意 :值要取绝对值
System.Math.Abs(value);//using System;
2.2 两者(角色 当前的坐标Position_1 和目的地的坐标Position_2 ) 坐标的 矢量值
参考资料7、8
Vector3 pos_ = Positon_1 - Positon_2float f = pos_.magnitude ;if(f < 1.0f)//2个浮点变量进行比较 {//判断为到达目的地...}
2019.11.25更新
NavMeshAgent组件的 路径 path 变量长度
player_go.transform.GetComponent().path.corners.Length
获得 角色 的 NavMeshAgent组件的 路径 path 变量 用于存储 路径点的 三维坐标 的 数组的长度。
当这个 长度 等于 1的时候,表明 角色到达了目的地,当其大于1 的时候,表明寻路组件 正在计算路径的 坐标点。
参考资料9
通过speedPercent来判断是否到达目的地
20200501更新
...using UnityEngine.AI;... NavMeshAgent agent;//Reference to the NavMeshAgent float speedPercent = 0f;...start(){... agent = GetComponent();...}...
update(){... speedPercent = agent.velocity.magnitude / agent.speed;//在Update函数里面进行调用...}
通常speedPercent是控制角色动画,跑步动画、走路动画、待命动画三种不同动画之间的切换设置的条件。
当speedPercent为0,或者是接近于0的时候,说明到达了目的地。
注意:角色在转弯的时候,有可能speedPercent也为0,需要添加一些特定的条件,才能判断是否到达目的地。
例如 判断 坐标,在本文的第2章节中有提到。
... update(){... speedPercent = agent.velocity.magnitude / agent.speed;//在Update函数里面进行调用 Vector3 pos_ = Positon_1 - Positon_2; float x,y,z; x = pos_.x; x= System.Math.Abs(x);//using System; y = pos_.y; y = System.Math.Abs(y);//using System; z = pos_.z; z = System.Math.Abs(z);//using System; if( (x < 1) &&(y < 1) &&(z < 1) && speedPercent == 0 ) {//判断为到达目的地 ... }...}...
注意 :值要取绝对值
System.Math.Abs(value);//using System;
Vector3.Distance也可以直接获得 两点之间的 浮点变量的值。20200524
float distance_f = Vector3.Distance(lastPos,nextPos);
相关文章:
1.
2.
.nextPosition
3.
4.
5.
6.
7..
8.
9..
10.
11.
12.
转载地址:https://blog.csdn.net/BuladeMian/article/details/79423364 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关于作者
