Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class CameraZoom : MonoBehaviour
- {
- public float zoomSpeed = 1.0f;
- // Add these two public fields
- public float minZoom = 2.0f; // Most zoomed-in (smallest view)
- public float maxZoom = 15.0f; // Most zoomed-out (largest view)
- // NEW: Smooth zoom settings
- public float smoothSpeed = 5.0f; // Higher = snappier, Lower = smoother
- // NEW: Variables for smooth interpolation
- private Camera cam;
- private float targetSize;
- void Start()
- {
- cam = GetComponent<Camera>();
- targetSize = cam.orthographicSize;
- }
- void Update()
- {
- float scrollWheelInput = Input.GetAxis("Mouse ScrollWheel");
- if (scrollWheelInput != 0f)
- {
- // Calculate new target size (your original zoom logic)
- targetSize = cam.orthographicSize - scrollWheelInput * zoomSpeed;
- targetSize = Mathf.Clamp(targetSize, minZoom, maxZoom);
- }
- // Smoothly interpolate current size toward target
- cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, targetSize, smoothSpeed * Time.deltaTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment