rotationCamera.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class rotationCamera : MonoBehaviour {
  5. Vector3 startRotation;
  6. [Header("Down bound angle X")]
  7. [Range(-180, 180)]
  8. public float MinX = 0f;
  9. [Header("Upper bound angle X")]
  10. [Range(-180, 180)]
  11. public float MaxX = 80f;
  12. float rotX;
  13. float rotY;
  14. public float Speed = 1;
  15. void Start()
  16. {
  17. startRotation = transform.eulerAngles;
  18. rotX = startRotation.y;
  19. rotY = startRotation.x;
  20. }
  21. void Update()
  22. {
  23. if (Input.GetMouseButton(0))
  24. {
  25. rotX += Input.GetAxis("Mouse X") * Speed;
  26. if(rotY > MinX && rotY < MaxX)
  27. {
  28. rotY += Input.GetAxis("Mouse Y") * -Speed;
  29. }
  30. else
  31. {
  32. if(rotY <= MinX)
  33. {
  34. if(Input.GetAxis("Mouse Y") < 0)
  35. {
  36. rotY += Input.GetAxis("Mouse Y") * -Speed;
  37. }
  38. }
  39. if (rotY >= MaxX)
  40. {
  41. if (Input.GetAxis("Mouse Y") > 0)
  42. {
  43. rotY += Input.GetAxis("Mouse Y") * -Speed;
  44. }
  45. }
  46. }
  47. }
  48. transform.eulerAngles = new Vector3(rotY, rotX, 0);
  49. }
  50. }