Ограничение приближения камеры
Я написал скрипт, который позволяет приближать и отдалять камеру с помощью колёсика мышки, но проблема в том, что у этого приближения и отдаления нет ограничений (я могу приближать и отдалять сколько захочу). Не знаю как выставить ограничения по высоте. Что мне нужно добавить в мой код, чтобы камера приближалась и отдалялась на определённый уровень? p.s метод в котором происходит приближение камеры называется HandleMouseInput().
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraContol : MonoBehaviour
{
public Transform cameraTransform;
private float movementSpeed = 0.4f;
public float movementTime;
public Vector3 zoomAmount;
public float minY; // 11
public float maxY; // 40
public float minZ; // 9
public float maxZ; // -21
public Vector3 newPosition;
public Quaternion newRotation;
public Vector3 newZoom;
public Vector3 dragStartPosition;
public Vector3 dragCurrentPosition;
public Vector3 rotateStartPosition;
public Vector3 rotateCurrentPosition;
// Start is called before the first frame update
void Start()
{
newPosition = transform.position;
newRotation = transform.rotation;
newZoom = cameraTransform.localPosition;
}
// Update is called once per frame
void Update()
{
HadleMovementInput();
HandleMouseInput();
}
void HandleMouseInput()
{
if (Input.mouseScrollDelta.y != 0)
{
newZoom += Input.mouseScrollDelta.y * zoomAmount;
}
if (Input.GetMouseButtonDown(1))
{
Plane plane = new Plane(Vector3.up, Vector3.zero);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float entry;
if(plane.Raycast(ray, out entry))
{
dragStartPosition = ray.GetPoint(entry);
}
}
if (Input.GetMouseButton(1))
{
Plane plane = new Plane(Vector3.up, Vector3.zero);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float entry;
if (plane.Raycast(ray, out entry))
{
dragCurrentPosition = ray.GetPoint(entry);
newPosition = transform.position + dragStartPosition - dragCurrentPosition;
}
}
if (Input.GetMouseButtonDown(2))
{
rotateStartPosition = Input.mousePosition;
}
if (Input.GetMouseButton(2))
{
rotateCurrentPosition = Input.mousePosition;
Vector3 difference = rotateStartPosition - rotateCurrentPosition;
rotateStartPosition = rotateCurrentPosition;
newRotation *= Quaternion.Euler(Vector3.up * (-difference.x / 5f));
}
}
void HadleMovementInput()
{
if (Input.GetKey("w"))
{
newPosition += (transform.forward * movementSpeed);
}
if (Input.GetKey("s"))
{
newPosition += (transform.forward * -movementSpeed);
}
if (Input.GetKey("d"))
{
newPosition += (transform.right * movementSpeed);
}
if (Input.GetKey("a"))
{
newPosition += (transform.right * -movementSpeed);
}
transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * movementTime);
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, 1);
cameraTransform.localPosition = Vector3.Lerp(cameraTransform.localPosition, newZoom, Time.deltaTime * movementTime);
}
}