Zoom Camera is buggy

So I added in some code to also zoom on the z-axis by duplicating the code we made for the y-axis and changed everything accordingly. It works great, except now I am getting a strange graphical glitch where every 10-15 seconds the camera view will jump out and then snap back to where it was. My z-position and x-rotation in the inspector are not stable and will constantly jump around, and the border of the Game View flickers a bit.

Is there a way to implement something like the stoppingDistance code from the unit movement lecture to force the camera to settle? Or is there a better way to implement code to zoom on multiple axes, rather than running two sets of the same code?

Here’s my code. About the only thing I haven’t tried is splitting up the " if(Input.mouseScrollDelta.y >0)" into separate Y and Z checks, which I guess is next.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class CameraController : MonoBehaviour

{
private const float MIN_FOLLOW_Y_OFFSET =2f;

private const float MAX_FOLLOW_Y_OFFSET = 12f;

private const float MIN_FOLLOW_Z_OFFSET = 0f;

private const float MAX_FOLLOW_Z_OFFSET = -12f;

[SerializeField] private CinemachineVirtualCamera cinemachineVirtualCamera;



private CinemachineTransposer cinemachineTransposer;

private Vector3 targetFollowOffset;

 private void Start()

{

    cinemachineTransposer = cinemachineVirtualCamera.GetCinemachineComponent<CinemachineTransposer>();

    targetFollowOffset = cinemachineTransposer.m_FollowOffset;

}



void Update()

{

    HandleMovement();

    HandleRotation();

    HandleZoom();

}

//move and rotate removed for brevity

private void HandleZoom()

{

    float zoomAmountY = 1f;

    float zoomAmountZ = 1f;

   // float zoomStoppingDistance = .1f;   //this does nothing yet, ignore me

    if(Input.mouseScrollDelta.y >0)

    {

        targetFollowOffset.y -= zoomAmountY;

        targetFollowOffset.z += zoomAmountZ;

    }

    if(Input.mouseScrollDelta.y <0)

    {

        targetFollowOffset.y += zoomAmountY;

        targetFollowOffset.z -= zoomAmountZ;

    }

    float zoomSpeed = 5f;

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

    cinemachineTransposer.m_FollowOffset =

        Vector3.Lerp(cinemachineTransposer.m_FollowOffset, targetFollowOffset, Time.deltaTime * zoomSpeed);

   

    targetFollowOffset.z = Mathf.Clamp(targetFollowOffset.z, MIN_FOLLOW_Z_OFFSET, MAX_FOLLOW_Z_OFFSET);

    cinemachineTransposer.m_FollowOffset =

        Vector3.Lerp(cinemachineTransposer.m_FollowOffset, targetFollowOffset, Time.deltaTime * zoomSpeed);  
}

}

TIL: I’ve been trying to modify a Vector2 with a third Vector.

Well, at least I figured out what the problem was…? Time to move on.

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

Privacy & Terms