总体思路为,
//首先查看期望速度,期望速度是否为0,若为0,设置移动速度和转向速度都为0;
//再看在期望速度不为0时,
//为保证动画流畅,在期望速度与自身朝向大于90度时将移动速度设置为0,只改变转向速度。
//而在正常情况下的,小于90度时,两个速度均改变。
//改变移动速度大小使用期望速度在自身速度的投影。
//改变转向速度大小使用角度大小的弧度制,来根据相差角度大小对应设置需要转向速度的大小。
nav = this.GetComponent<NavMeshAgent>();
ani = this.GetComponent<Animator>();
void Update () {
if(nav.desiredVelocity==Vector3.zero)
{
ani.SetFloat("robotspeed",0);
ani.SetFloat("robotturn", 0);
}
else
{
float angle = Vector3.Angle(transform.forward, nav.desiredVelocity);
float anglerad = 0f;
Vector3 projection=Vector3.zero;
Vector3 crossres = Vector3.Cross(transform.forward, nav.desiredVelocity);
if (angle>90)
{
ani.SetFloat("robotspeed" , 0/*,0.5f, Time.deltaTime*/);
}
else
{
projection = Vector3.Project(nav.desiredVelocity, transform.forward);
ani.SetFloat("robotspeed", projection.magnitude/*, 1f, Time.deltaTime*/);
}
anglerad = angle * Mathf.Deg2Rad;
if (crossres.y < 0)
{
anglerad = -anglerad;
}
ani.SetFloat("robotturn", anglerad* 2f, 1f, Time.deltaTime);
}
}


3102

被折叠的 条评论
为什么被折叠?



