I am a beginner in C# and would like to enable the player to zoom the camera in/out (orthographic) within a certain range (2-7). My code works in terms of zooming in and out, however when the level of zoom reaches 2 or 7 it will not allow any input after.
I apologize in advance if my code is a mess or if the solution is simple, as I have only been at it for a few days.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class CameraZoom : MonoBehaviour
{
public CinemachineVirtualCamera cvc;
public float scrollSpeed = 10;
void Start()
{
cvc = GetComponent<CinemachineVirtualCamera>();
}
void Update()
{
if (Input.GetAxis("Mouse ScrollWheel") != 0f && cvc.m_Lens.OrthographicSize >= 2 && cvc.m_Lens.OrthographicSize <= 7)
{
cvc.m_Lens.OrthographicSize -= Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
}
else
{
Debug.Log("Cannot zoom any further.");
}
}
}
You will want to separate the logic in three steps
This could e.g. look like
What you are looking for is
Mathf.Clampyou could simply do the entire thing in a single line:
making sure that input is always handled but the size guaranteed to stay between
2and7.