我现在正在试着让相机顺畅地跟随玩家。该脚本运行良好,但问题是该脚本会导致播放器在某个时间点出现卡顿。例如,如果玩家在X:3,玩家将结巴,但如果玩家在X:-6,玩家将停止结巴。我100%确定这个脚本就是问题所在,因为如果我删除这个脚本,播放器就会停止卡顿。
以下是相机跟随脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollowing : MonoBehaviour
{
[SerializeField]
private Transform target;
[SerializeField]
private Vector3 cameraOffset;
[SerializeField]
private float followSpeed = 10f;
[SerializeField]
private float xMin = 0f;
private Vector3 velocity = Vector3.zero;
private void FixedUpdate()
{
Vector3 targetPos = target.position + cameraOffset;
Vector3 clampedPos = new Vector3(Mathf.Clamp(targetPos.x, xMin, float.MaxValue), targetPos.y, targetPos.z);
Vector3 smoothPos = Vector3.SmoothDamp(transform.position, clampedPos, ref velocity, followSpeed * Time.fixedDeltaTime);
transform.position = smoothPos;
}
}如果你知道答案或可能的原因,请告诉我,我试图在今年年底发布,出版等,这个游戏。谢谢!:D
发布于 2021-05-12 23:24:03
我想你说的结巴是指摇晃或颤动(不熟悉结巴这个词)。我会试着适应docs的例子。使用void Update()或LateUpdate()而不是FixedUpdate()。
如果你想让你的游戏更准确,你可能想要使用void LateUpdate()。此方法将在检测到输入后调用,因此它将做出更好的反应。我认为这是更好的选择,因为它会比Update()更准确。
我还会跟踪场景中摇晃开始的位置,如果它是从一个确定的点开始的,并检查在不想要的摇晃开始的位置附近的一些错位的碰撞器。
https://stackoverflow.com/questions/67506284
复制相似问题