可以看到navmeshAgent能进行寻路,但是人物不动
脚本里是用animation来控制的运动,禁用了Nav的控制
navAgent.updatePosition = false;
navAgent.updateRotation = false;
如此一来,就出现了上面的现象,尝试添加了一句nav.nextPosition = transform.position;,能解决上述现象,但是人物只是停留在原地,并没有移动,该怎么 让人物进行寻路呢
EnemyAI:
EnemyAnimation:
同学你好,就是在update里边添加上navmeshagent.nextPosition = transform.position就可以了,老师这边测试是没有问题的,可以看下,是2018.3版本的,但是代码跟工程实现是一样的:
链接:https://pan.baidu.com/s/1cF2f6n5yWx-L7u0zxAluDA
提取码:85oy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Guard : MonoBehaviour
{
public GameObject[] patrolPath; //巡逻路径
private NavMeshAgent agent;
private Animator anim;
private int targetDex = 0;
// Start is called before the first frame update
void Start()
{
agent = GetComponent<NavMeshAgent>();
anim = GetComponent<Animator>();
agent.updatePosition = false;
agent.updateRotation = false;
}
// Update is called once per frame
void Update()
{
GameObject targetObj = patrolPath[targetDex];
if (targetObj != null && MoveTo(targetObj.transform.position))
{
targetDex++;
targetDex %= patrolPath.Length;
}
if (targetObj == null)
anim.SetFloat("speedZ", 0f, 0.3f, Time.deltaTime);
}
bool MoveTo(Vector3 targetPos)
{
agent.destination = targetPos;
if (!agent.pathPending && agent.remainingDistance < agent.radius + 0.2)
{
return true;
}
Vector3 nextPos = agent.nextPosition;
nextPos.y = transform.position.y;
Vector3 P = nextPos - transform.position;
float signAngle = Vector3.SignedAngle(P, transform.forward, Vector3.up);
float angleAbs = Mathf.Abs(signAngle);
if (signAngle > 0)
transform.Rotate(new Vector3(0, -Time.deltaTime * 120, 0));//turn right
else
transform.Rotate(new Vector3(0, Time.deltaTime * 120, 0)); //turn left
if (angleAbs < 90)
anim.SetFloat("speedZ", 1.5f * Mathf.Cos(angleAbs), 2f, Time.deltaTime);
else
anim.SetFloat("speedZ", 0.6f, 0.2f, Time.deltaTime);
if (P.magnitude > agent.radius)
agent.nextPosition = (agent.nextPosition - 0.1f * P);
return false;
}
}
机器人导航,加进Rigidbody后,动画怎么会跑不动的