mikdog

Adventure Creator Camera Zoom v2

Nov 5th, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class CameraZoom : MonoBehaviour
  4. {
  5. public float zoomSpeed = 1.0f;
  6.  
  7. // Add these two public fields
  8. public float minZoom = 2.0f; // Most zoomed-in (smallest view)
  9. public float maxZoom = 15.0f; // Most zoomed-out (largest view)
  10.  
  11. // NEW: Smooth zoom settings
  12. public float smoothSpeed = 5.0f; // Higher = snappier, Lower = smoother
  13.  
  14. // NEW: Variables for smooth interpolation
  15. private Camera cam;
  16. private float targetSize;
  17.  
  18. void Start()
  19. {
  20. cam = GetComponent<Camera>();
  21. targetSize = cam.orthographicSize;
  22. }
  23.  
  24. void Update()
  25. {
  26. float scrollWheelInput = Input.GetAxis("Mouse ScrollWheel");
  27.  
  28. if (scrollWheelInput != 0f)
  29. {
  30. // Calculate new target size (your original zoom logic)
  31. targetSize = cam.orthographicSize - scrollWheelInput * zoomSpeed;
  32. targetSize = Mathf.Clamp(targetSize, minZoom, maxZoom);
  33. }
  34.  
  35. // Smoothly interpolate current size toward target
  36. cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, targetSize, smoothSpeed * Time.deltaTime);
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment