c# - 当我倒退时第三人称镜头卡顿

标签 c# unity-game-engine camera game-engine

我有第三人称代码,但是当我倒退时相机卡顿!有没有人有办法解决吗?相机与播放器分离,播放器根据相机的视角旋转。

PlayerController.cs

if (Input.GetKey (KeyCode.LeftShift)) {
    if(Input.GetKey(KeyCode.W) || Input.GetAxis ("Vertical") > 0) moveDirection += camera.forward;
    if(Input.GetKey(KeyCode.S) || Input.GetAxis ("Vertical") < 0) moveDirection += -camera.forward;
    if(Input.GetKey(KeyCode.A) || Input.GetAxis ("Horizontal") < 0) moveDirection += -camera.right;
    if(Input.GetKey(KeyCode.D) || Input.GetAxis ("Horizontal") > 0) moveDirection += camera.right;

    if (Input.GetAxis ("Horizontal") != 0 || Input.GetAxis ("Vertical") != 0) {
        //Multiply it by speed.
        moveDirection.Normalize ();
        moveDirection *= run;
        //Applying gravity to the controller
        moveDirection.y -= gravity * Time.deltaTime;
        //Making the character move
        controller.Move (moveDirection * Time.deltaTime);
    }
    moveDirection.y = 0f;
    if (moveDirection != Vector3.zero) {
        transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation (moveDirection), rotationSpeed * Time.deltaTime);
    }
}

相机.cs

// Update is called once per frame
void Update () {

    // We setup the rotation of the sticks here
    float inputX = Input.GetAxis ("RightStickHorizontal");
    float inputZ = Input.GetAxis ("RightStickVertical");
    mouseX = Input.GetAxis ("Mouse X");
    mouseY = Input.GetAxis ("Mouse Y");
    finalInputX = inputX + mouseX;
    finalInputZ = inputZ - mouseY;

    rotY += finalInputX * inputSensitivity * Time.deltaTime;
    rotX += finalInputZ * inputSensitivity * Time.deltaTime;

    rotX = Mathf.Clamp (rotX, -clampAngle, clampAngle);

    Quaternion localRotation = Quaternion.Euler (rotX, rotY, 0.0f);
    transform.rotation = localRotation;


void LateUpdate () {
    CameraUpdater ();
}

void CameraUpdater() {
    Transform target = CameraFollowObj.transform;
    // set the target object to follow

    //move towards the game object that is the target
    float step = CameraMoveSpeed * Time.fixedDeltaTime;
    transform.position = Vector3.MoveTowards (transform.position, CameraFollowObj.transform.position, step);
}

最佳答案

根据我的经验,当您的相机流畅但出现卡顿时,这是因为相机 - 一旦达到速度 - 移动速度比它所跟随的物体更快:

问题

  1. 对象移动
  2. 相机开始慢慢移向物体
  3. 它追上并停下来
  4. 相机慢慢开始移向物体
  5. 重复2-4;口吃行为

解决方案

简单; 调整相机速度变量,以确保它在物体移动时永远不会完全跟上,而是保持在后面,这样就不必每隔几帧就停止一次。



奖励阅读 Material

其他来源经常会解释他们的解决方案是错误将代码放入FixedUpdate中,以奇迹般地摆脱卡顿行为。有时,通过这样做获得正确的行为是由于一系列的原因:

  • 更新会在每个帧更新时运行,因此此处的时间步长 (Time.deltaTime) 取决于机器,但在我的机器上,平均每 0.007 秒更新一次。
  • FixedUpdate 以固定时间步长运行,标准为 0.002,但可以在设置中更改。
  • 代码:float step = CameraMoveSpeed * Time.deltaTime; 为了简单的数学计算,我们假设 CameraMoveSpeed 为 1。

通过这些数字我们可以理解,将代码放入 Update 会得到以下结果

更新

每 0.007 秒,我们用 CameraMoveSpeed * 0.007 更新相机 lerp 刻度

固定更新

每 0.02 秒,我们用 CameraMoveSpeed * 0.007 更新相机 lerp 刻度

结果

它们每次刻度移动相同的长度,但是固定更新中的刻度频率较低,这会导致相机平滑速度变慢,从而“解决”了本文的答案所描述的问题,在某种程度上,调整相机速度变量以使相机跟随速度变慢,但您没有调整变量,而是将代码移至物理更新而不是帧更新,并且您只是偶然解决了问题,而没有了解原因。

关于c# - 当我倒退时第三人称镜头卡顿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56204176/

相关文章:

android - 从ImageView中获取图片的文件路径

iPhone SDK - 获取/计算相机视野 (FOV)(增强现实)

c# - 在 ASP.NET 中拆分数据

c# - linq 按小时范围分组?

c# - 如何写入 kiwi syslog 服务器日志 c#

iOS 静态库,用于统一 App 切换问题 Facebook 集成

android - 如何在 Android 中同时在两个摄像头上录制?

c# - Linq Query toList() 为空但 foreach 有效

animation - Unity-停止动画循环

unity-game-engine - Unity抛出: Request error (error): UnityEditor. AsyncHTTPClient :Done(State, Int32)错误