We can get same result with less code

using Cinemachine;
using UnityEngine;

using static UnityEngine.Mathf;

public class CameraController : MonoBehaviour
{
    [Header("Params")]
    [SerializeField, Range(0.1f, 10)] private float _movementSpeed;
    [SerializeField, Range(0.1f, 180)] private float _rotationSpeed;
    [SerializeField, Range(0.01f, 1)] private float _zoomSpeed;

    [Header("Ancors")]
    [SerializeField] private Vector3 _upAncor;
    [SerializeField] private Vector3 _downAncor;

    [SerializeField] private CinemachineVirtualCamera _virtualCamera;

    private CinemachineTransposer _cinemachineTransposer;
    private float _zoomLerp = 1;

    private void Start()
    {
        _cinemachineTransposer = _virtualCamera.GetCinemachineComponent<CinemachineTransposer>();
    }

    private void Update()
    {
        Vector3 moveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        float rotation = Input.GetKey(KeyCode.Q)? _rotationSpeed : Input.GetKey(KeyCode.E)? -_rotationSpeed : 0;
        _zoomLerp += -Input.mouseScrollDelta.y * _zoomSpeed;
        _zoomLerp = Clamp(_zoomLerp, 0, 1);

        if (moveDir == Vector3.zero && rotation == 0 && Input.mouseScrollDelta.y == 0)
            return;

        transform.Translate(moveDir * Time.deltaTime * _movementSpeed);
        transform.Rotate(Vector3.up * rotation * Time.deltaTime);
        _cinemachineTransposer.m_FollowOffset = Vector3.Lerp(_downAncor, _upAncor, _zoomLerp);
    }
}

Your goal should never be to make the most compact code possible but rather to make it easily readable and understandable.

Yes that code works but it’s harder to read. You define the moveDir but then only use it 7 lines after that. You have movement and rotation and zoom logic all mixed together.
Right now it might seem understandable, but if you get back to that code in 1 week it will be very tricky to understand what all of it is doing.

Also you can use the constants Vector3.up and Vector3.down instead of having manually defined anchors

6 Likes

Privacy & Terms