ParticlePlexusDemo_CameraRig.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ParticlePlexusDemo_CameraRig : MonoBehaviour
  5. {
  6. public Transform pivot;
  7. Vector3 targetRotation;
  8. [Range(0.0f, 90.0f)]
  9. public float rotationLimit = 90.0f;
  10. public float rotationSpeed = 2.0f;
  11. public float rotationLerpSpeed = 4.0f;
  12. Vector3 startRotation;
  13. void Start()
  14. {
  15. startRotation = pivot.localEulerAngles;
  16. targetRotation = startRotation;
  17. }
  18. void Update()
  19. {
  20. float horizontal = Input.GetAxis("Horizontal");
  21. float vertical = Input.GetAxis("Vertical");
  22. if (Input.GetKeyDown(KeyCode.R))
  23. {
  24. targetRotation = startRotation;
  25. }
  26. horizontal *= rotationSpeed;
  27. vertical *= rotationSpeed;
  28. targetRotation.y += horizontal;
  29. targetRotation.x += vertical;
  30. targetRotation.x = Mathf.Clamp(targetRotation.x, -rotationLimit, rotationLimit);
  31. targetRotation.y = Mathf.Clamp(targetRotation.y, -rotationLimit, rotationLimit);
  32. Vector3 currentRotation = pivot.localEulerAngles;
  33. currentRotation.x = Mathf.LerpAngle(currentRotation.x, targetRotation.x, Time.deltaTime * rotationLerpSpeed);
  34. currentRotation.y = Mathf.LerpAngle(currentRotation.y, targetRotation.y, Time.deltaTime * rotationLerpSpeed);
  35. pivot.localEulerAngles = currentRotation;
  36. }
  37. }