Zoom function breaks code

I cant figure out what is happening but with I call the zoom function it just jitters back and forth between y values of 6 and 7. i even copied the full repo code, still same issue. Cant even zoom with it running

Paste in your CameraController.cs script and we’ll have a look.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

public class CameraController : MonoBehaviour
{

    private const float MAX_FOLLOW_Y_OFFSET = 2f;
    private const float MIN_FOLLOW_Y_OFFSET = 12f;

    private CinemachineTransposer cinemachineTransposer;
    private Vector3 targetFollowOffset;


    [SerializeField] private CinemachineVirtualCamera cinemachineVirtualCamera;

    private void Start()
    {
        cinemachineTransposer = cinemachineVirtualCamera.GetCinemachineComponent<CinemachineTransposer>();
        targetFollowOffset = cinemachineTransposer.m_FollowOffset;
    }


    private void Update()
    {

        HandleMovement();
        HandleRotation();
        HandleZoom();

    }

    private void HandleMovement()
    {
        Vector3 inputMoveDir = new Vector3(0, 0, 0);
        if (Input.GetKey(KeyCode.W))
        {
            inputMoveDir.z = +1f;
        }
        if (Input.GetKey(KeyCode.S))
        {
            inputMoveDir.z = -1f;
        }
        if (Input.GetKey(KeyCode.A))
        {
            inputMoveDir.x = -1f;
        }
        if (Input.GetKey(KeyCode.D))
        {
            inputMoveDir.x = +1f;
        }

        float moveSpeed = 10f;


        Vector3 moveVector = transform.forward * inputMoveDir.z + transform.right * inputMoveDir.x;
        transform.position += moveVector * moveSpeed * Time.deltaTime;
    }
    private void HandleRotation()
    {
        Vector3 rotationVector = new Vector3(0, 0, 0);

        if (Input.GetKey(KeyCode.Q))
        {
            rotationVector.y = +1f;
        }
        if (Input.GetKey(KeyCode.E))
        {
            rotationVector.y = -1f;
        }

        float rotationSpeed = 100f;

        transform.eulerAngles += rotationVector * rotationSpeed * Time.deltaTime;
    }

    private void HandleZoom()
    {
         float zoomAmount = 1f;
         if (Input.mouseScrollDelta.y > 0)
         {
             targetFollowOffset.y -= zoomAmount;
         }
         if (Input.mouseScrollDelta.y < 0)
         {
             targetFollowOffset.y += zoomAmount;
         }

         targetFollowOffset.y = Mathf.Clamp(targetFollowOffset.y, MIN_FOLLOW_Y_OFFSET, MAX_FOLLOW_Y_OFFSET);

         float zoomSpeed = 5f;
         cinemachineTransposer.m_FollowOffset =
             Vector3.Lerp(cinemachineTransposer.m_FollowOffset, targetFollowOffset, Time.deltaTime * zoomSpeed);
        
    }

}

so after some testing i think it has to do with the

targetFollowOffset.y = Mathf.Clamp(targetFollowOffset.y, MIN_FOLLOW_Y_OFFSET, MAX_FOLLOW_Y_OFFSET);

when i comment it out the shaking goes away but and it works, but i lose the capability to set a min and max zoom

It seems your min and max are swapped. Your min is 12 and your max is 2

1 Like

oh wow i cant believe i didnt see that haha it works now thanks

Things like this are easy to miss. I just read the code and didn’t spot it right away. Once I got to @bixarrio’s post, I now can’t unsee it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms