如何解决Unity修复NavMeshAgent抖动动作?
问题记录
解释我的问题
我正在使用Unity3D Engine编写手机游戏。对于我的播放器移动,我正在使用NavMeshAgent,因为它对我来说是最简单,最有效的方式。但是,当我按下“播放”按钮并要求播放器移动时,这些动作非常生涩,根本看不出来。
您是否有解决此问题的主意?! 预先感谢您的回答! ^^
我的代码
这是我的代码:
Player.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.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;
}
}
解决方法
问题不是导航仪控制器,而是摄像机遵循脚本。
您可以尝试的一件事是仅使用所需的位置或使用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视频的固定评论中得到了解释
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。