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