微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Unity - 如何使用 NavMeshAgent 跳转并单击以移动逻辑

如何解决Unity - 如何使用 NavMeshAgent 跳转并单击以移动逻辑

我正在构建一个游戏,可以使用鼠标输入控制玩家,通过导航网格代理使用点击移动逻辑。

为了让玩家跳跃,我也开始使用 CharacterController 来帮助管理玩家。我的问题是我不知道把跳转逻辑放在哪里。我发现的所有参考资料都与使用没有导航网格代理的角色控制器相关。

如果需要,我可以去掉 CharacterController,但必须保留 NavMeshAgent。

这是一个允许行走的工作代码。你能帮我跳逻辑吗?

  private NavMeshAgent _agent;
  private CharacterController _characterController;
  private Vector3 _desveLocity;

  private void Update()
  {
     if (Input.GetMouseButtonDown(0))
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray.origin,ray.direction,out RaycastHit hitInfo))
         {
             _agent.destination = hitInfo.point;
         }
     }
     var currMovementDirection = _desveLocity.normalized * currentSpeed;
     if (_agent.remainingdistance > _agent.stoppingdistance)
     {
         _desveLocity = _agent.desiredVeLocity;
         _characterController.Move(currMovementDirection * Time.deltaTime);
     }
 }

解决方法

跳转逻辑应该在 Update() 方法中,因为我们希望每帧都计算高度。

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray.origin,ray.direction,out RaycastHit hitInfo))
        {
            _agent.destination = hitInfo.point;
        }
    }
    var currMovementDirection = _desVelocity.normalized * currentSpeed;

    groundedPlayer = _characterController.isGrounded;
    if (groundedPlayer && currMovementDirection.y < 0)
    {
        currMovementDirection.y = 0f;
    }



    // Changes the height position of the player..
    if (Input.GetButtonDown("Jump") && groundedPlayer)
    {
        currMovementDirection.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
    }

    currMovementDirection.y += gravityValue * Time.deltaTime;
    if (_agent.remainingDistance > _agent.stoppingDistance)
    {
        _desVelocity = _agent.desiredVelocity;
        _characterController.Move(currMovementDirection * Time.deltaTime);
    }
}

请参阅官方文档 here

,

您可以使用 const [logIndex,setLogIndex] = useState(0); const [logs,setLogs] = useState([]); let interval = useRef(setTimeout(() => {},0); useEffect(() => { interval.current = setTimeout(addNextLogMessage,2000); return(() => { clearTimeout(interval.current); // cleanup } },[]); // run when page first loads const sourceLogs = [ <p>Message 1</p>,<p>Message 2</p>,<p>Message 3</p>,<p>Message 4</p>,<p>Message 5</p>,<p>Message 6</p> ]; const timeBetweenMessages = Math.ceil((1000 * 60 * 3) / sourceLogs.length); // three minutes (1000ms * 60sec * 3min) divided into number of log messages,rounded up const addNextLogMessage () => { setLogIndex(logIndex => logIndex + 1); clearTimeout(interval.current); if(logIndex < sourceLogs.length) { setLogs(logs => [...logs,{...sourceLogs[logIndex],timeStamp: new Date().toString()}]); interval.current = setTimeout(addNextLogMessage,Math.random() * timeBetweenMessages + 500); } } return ( <> { logs.map(item => item); } </> ); 而不是 Rigidbody 来实现这一点。诀窍是您需要禁用 CharacterController 才能跳转。

或者,您可以将目的地设置为跳转时的位置,以便代理在跳转发生时不会继续模拟。

使用碰撞检测,您可以在着陆后再次打开 NavMeshAgent

NavMeshAgent

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。