简体   繁体   中英

Clamping the camera isn't so smooth when moving the camera around. Is there a way to make the clamping smoother?

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);
    }
}

I can look around up down left right using Cinemachine CinemachineFreeLook but when using the clamping in this script and then when moving the camera around with the mouse it looks like the camera is a bit stuttering/jittering.

Without the clamping, in the LateUpdate the camera will work fine but then I can't move the camera up down left right only to the left and right.

The reason I'm trying to do the clamping on my own is that in the FreeLookCamera in the Axis Control > Y Axis if I set the speed to 100 or 300 when I move the camera up and down it will also zoom in/out on the player and I don't want that zooming but there is no option to remove/disable the zooming.

Screenshot: https://i.stack.imgur.com/K0lll.jpg

Look like you forgot to multiply your speed by Time.deltaTime. Try adding

pitch *= Time.deltaTime;

after

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

This should give you a smoother movement. You might have to increase the speed.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM