【问题标题】:Clamping the camera isn't so smooth when moving the camera around. Is there a way to make the clamping smoother?移动相机时,夹住相机不是很顺畅。有没有办法让夹紧更顺畅?
【发布时间】:2021-05-23 03:03:28
【问题描述】:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

public class CamerasManager : MonoBehaviour
{
    public CinemachineFreeLook playerCameraCloseLook;
    public CinemachineFreeLook playerCameraGameplayLook;
    public float timeBeforeSwitching;
    public float clampMin;
    public float clampMax;
    public float pitch;
    public float speed;

    private float brainBlendTime;

    // Start is called before the first frame update
    void Start()
    {
        brainBlendTime = Camera.main.GetComponent<CinemachineBrain>().m_DefaultBlend.m_Time;

        StartCoroutine(SwitchCameras(timeBeforeSwitching));
    }

    // Update is called once per frame
    void LateUpdate()
    {
        if (playerCameraGameplayLook != null)
        {
            pitch += speed * Input.GetAxis("Mouse Y");
            pitch = Mathf.Clamp(pitch, clampMin, clampMax);
            playerCameraGameplayLook.GetRig(1).GetCinemachineComponent<CinemachineComposer>().m_TrackedObjectOffset.y = pitch;
        }
    }

    IEnumerator SwitchCameras(float TimeBeforeSwitching)
    {
        yield return new WaitForSeconds(TimeBeforeSwitching);

        playerCameraCloseLook.enabled = false;
        playerCameraGameplayLook.enabled = true;

        PlayerLockManager.PlayerLockState(false);

        StartCoroutine(BlendTime());
    }

    IEnumerator BlendTime()
    {
        yield return new WaitForSeconds(brainBlendTime);

        PlayerLockManager.PlayerLockState(true);
    }
}

我可以使用 Cinemachine CinemachineFreeLook 左右上下左右查看,但是当在此脚本中使用钳位时,然后用鼠标四处移动相机时,看起来相机有点卡顿/抖动。

如果没有夹紧,在 LateUpdate 中,相机可以正常工作,但我无法将相机从左到右上下移动,只能左右移动。

我尝试自己进行夹紧的原因是,在 FreeLookCamera 中的 Axis Control > Y Axis 中,如果我在上下移动相机时将速度设置为 100 或 300,它也会放大/out 在播放器上,我不想缩放,但没有删除/禁用缩放的选项。

截图:https://i.stack.imgur.com/K0lll.jpg

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    看起来您忘记将速度乘以 Time.deltaTime。 尝试添加

    pitch *= Time.deltaTime;
    

    之后

    pitch += speed * Input.GetAxis("Mouse Y");
    

    这应该会让你的动作更顺畅。 您可能需要提高速度。

    https://docs.unity3d.com/ScriptReference/Input.GetAxis.html

    【讨论】:

    • 当我添加这条线间距 *= Time.deltaTime;然后我不能用鼠标上下移动相机,只能左右两侧。夹紧不工作。请问大家还有什么想法吗?
    • 再乘以更大的数字,例如 beetween 2/10(鼠标灵敏度)。但是再次查看您的代码,抖动可能是因为您没有缓存 GetCinemachineComponent。缓存它并查看文档以使用 deltaTime,并在 Update 而不是 LateUpdate 中尝试
    猜你喜欢
    • 2018-01-08
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多