【问题标题】:Unity fix NavMeshAgent jerky movements?Unity 修复 NavMeshAgent 生涩的动作?
【发布时间】:2021-01-31 08:59:50
【问题描述】:

问题记录

https://youtu.be/BpzHQkVQz5A

解释我的问题

我正在使用 Unity3D 引擎编写手机游戏。对于我的玩家移动,我使用的是 NavMeshAgent,因为它对我来说是最简单和最有效的方式。但是当我点击播放按钮并要求我的玩家移动时,移动是生涩的,而且看起来一点也不愉快。

你有什么办法解决这个问题吗?! 预先感谢您的回答! ^^

我的代码

这是我的代码:

播放器.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Player : MonoBehaviour
{
    NavMeshAgent agent;
    Touch touch;
    RaycastHit hit;
    Ray ray;

    // START FUNCTION
    private void Start()
    {
        agent = GetComponent<NavMeshAgent>();
    }

    // UPDATE FUNCTION
    private void Update()
    {
        // TOUCH DETECTION
        if (Input.touchCount > 0)
        {
            touch = Input.GetTouch(0);

            // A FINGER TOUCHED THE SCREEN
            if (touch.phase == TouchPhase.Began)
            {
                // RETURN X, Y AND Z WORLD POS OF THE TOUCH SCREEN POS
                ray = Camera.main.ScreenPointToRay(touch.position);

                if (Physics.Raycast(ray, out hit))
                {
                    if (hit.collider != null)
                    {
                        // MOVING PLAYER TO THE HIT POS
                        Vector3 hitVec = new Vector3(hit.point.x, hit.point.y + (GetComponent<Collider>().bounds.size.y / 2), hit.point.z);
                        agent.SetDestination(hitVec);
                    }
                }
            }
        }

        // SAME CODE USING MOUSE BUTTON
#if UNITY_EDITOR

        if (Input.GetMouseButton(0))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider != null)
                {
                    Vector3 hitVec = new Vector3(hit.point.x, hit.point.y + (GetComponent<Collider>().bounds.size.y / 2), hit.point.z);
                    agent.SetDestination(hitVec);
                }
            }
        }

#endif
    }
}

CameraFollow.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// BRACKEYS CAMERA FOLLOW SCRIPT WITHOUT THE LOOKAT METHODE
public class CameraFollow : MonoBehaviour
{
    public Transform target;

    public float smoothSpeed = 0.2f;
    public Vector3 offset;

    void FixedUpdate()
    {
        Vector3 desiredPosition = target.position + offset;
        Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
        transform.position = smoothedPosition;
    }
}

【问题讨论】:

    标签: c# unity3d render game-physics


    【解决方案1】:

    问题不在于导航网格控制器,而是相机跟随脚本。

    您可以尝试的一件事是仅使用 desiredPosition 或使用 Vector3.SmoothDamp 移动相机位置:

    private Vector3 velocity;
    
    void LateUpdate(){
    
    ...
    
            Vector3 desiredPosition = target.position + offset;
            Vector3 smoothedPosition = Vector3.SmoothDamp(transform.position, desiredPosition, ref velocity, smoothSpeed);
            transform.position = smoothedPosition;
    }
    

    这在 Brackeys 视频的置顶评论中也有解释

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-29
      • 2012-12-30
      • 1970-01-01
      相关资源
      最近更新 更多