NavMashAgent和Animation

a839398 2017-12-04 07:45:35
我用Animation设置了角色动画的切换,键盘监听角色的移动,角色的移动、转向和动画切换都正常
然后我用NavMashAgent设置角色的自动寻路的时候,角色可以移动到目标地点。
但是自动寻路的过程中动画切换出了问题。角色有两个动作stand和run。如果没有障碍物角色一直站着移动到终点,如果有障碍物,角色就跑着绕过障碍物,之后又是站着到终点。
...全文
444 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
a839398 2017-12-07
  • 打赏
  • 举报
回复
谢谢你,这个问题困扰了我几天了。以为要很久才有回复,没想到这么快。我再写一下代码先。
ttod_qzstudio 2017-12-06
  • 打赏
  • 举报
回复
如果你判断一个角色是否正在移动,似乎没必要使用rigidbody,判断相邻两帧位置的距离(建议使用距离的平方,这样计算量小一点儿)是否接近0就可以了,rigidbody组件容易添加额外的干扰。 总的来说这个事情应该可以把逻辑写的简单些,我觉得你的意思好像是这样吧: 角色控制分两种,一种是通过键盘操作,一种是自动寻路,通过键盘上的某个键可以切换控制状态。 角色动作的播放和控制是没有关系的,只和移动速度有关,接近0就是站立,否则就是跑动。 你上面的代码有一点点复杂了,再缕一下思路吧。
a839398 2017-12-05
  • 打赏
  • 举报
回复
我昨天没在自己的电脑上的网,家里面没网络,就没有发代码
a839398 2017-12-05
  • 打赏
  • 举报
回复
using UnityEngine; using System.Collections; public class BoyAnimationVillage : MonoBehaviour { private Animator anim; void Start() { anim = transform.GetComponent<Animator>(); } // Update is called once per frame void Update () { if (transform.GetComponent<Rigidbody>().velocity.magnitude >0.1f)//判断刚体Rigidbody的速度是否大于0.01 { //transform.GetComponent<Rigidbody>().freezeRotation = false; anim.SetBool("Move", true);//设置动画切换 } else { anim.SetBool("Move", false);//设置动画切换 transform.GetComponent<Rigidbody>().freezeRotation = true; } } } //================================================================================== using UnityEngine; using System.Collections; public class PlayerVillageMove : MonoBehaviour { public float velocity = 5; private NavMeshAgent agent; void Start() { agent = this.GetComponent<NavMeshAgent>(); } void Update () { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); Vector3 vel = rigidbody.velocity; if (Mathf.Abs(h) > 0.05f || Mathf.Abs(v) > 0.05f) { rigidbody.velocity = new Vector3(-h * velocity, vel.y, -v * velocity); transform.rotation = Quaternion.LookRotation(new Vector3(-h, 0, -v)); } else { if (agent.enabled == false) { rigidbody.velocity = Vector3.zero; } } if (agent.enabled) { transform.rotation = Quaternion.LookRotation ( agent.velocity ); } } } //========================================================================================= using UnityEngine; using System.Collections; public class PlayerAutoMove : MonoBehaviour { private NavMeshAgent agent; public float minDistance = 3; void Start () { agent = this.GetComponent<NavMeshAgent>(); } void Update () { if (agent.enabled) { if (agent.remainingDistance < minDistance &&agent.remainingDistance!=0 ) { agent.Stop(); agent.enabled = false; TaskManager._instance.OnArriveDestination(); } } float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); if (Mathf.Abs(h) > 0.05f || Mathf.Abs(v) > 0.05f) { StopAuto();//如果在寻路过程中 有按下移动控制键,那么就停止寻路 } } public void SetDestination( Vector3 targetPos ) { agent.enabled = true; agent.SetDestination(targetPos); } public void StopAuto() { if (agent.enabled) { agent.Stop(); agent.enabled = false; } } }

2,537

社区成员

发帖
与我相关
我的任务
社区描述
Unity3D相关内容讨论专区
游戏unity 技术论坛(原bbs)
社区管理员
  • Unity3D
  • 芝麻粒儿
  • 「已注销」
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

Unity3D社区公告:

  1. 社区致力于解决各种Unity3D相关的“疑难杂症”。
  2. 社区不允许发布与Unity3D或相关技术无关内容。
  3. 社区版主邀请各位一道为打造优秀社区不懈努力。

试试用AI创作助手写篇文章吧