Newer
Older
HierarchicalFSM-Unity3D / Assets / Scripts / CameraBehaviour.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CameraBehaviour : MonoBehaviour
  5. {
  6. [SerializeField] private Transform followTarget;
  7. [SerializeField] private float rotationSpeed = 10f;
  8. [SerializeField] private float bottomClamp = -40f;
  9. [SerializeField] private float topClamp = 70f;
  10. private float cinemachineTargetPitch;
  11. private float cinemachineTargetYaw;
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. }
  16. // Update is called once per frame
  17. void LateUpdate()
  18. {
  19. CameraRotation();
  20. }
  21. private float GetMouseInput(string axis) => Input.GetAxis(axis) * rotationSpeed * Time.deltaTime;
  22. private void CameraRotation()
  23. {
  24. float mouseX = GetMouseInput("Mouse X");
  25. float mouseY = GetMouseInput("Mouse Y");
  26. cinemachineTargetPitch = UpdateRotation(cinemachineTargetPitch, mouseY, bottomClamp, topClamp, true);
  27. cinemachineTargetYaw = UpdateRotation(cinemachineTargetYaw, mouseX, float.MinValue, float.MaxValue, false);
  28. ApplyRotations(cinemachineTargetPitch, cinemachineTargetYaw);
  29. }
  30. private void ApplyRotations(float pitch, float yaw) => followTarget.rotation = Quaternion.Euler(pitch, yaw, followTarget.eulerAngles.x);
  31. private float UpdateRotation(float currentRotation, float input, float min, float max, bool isXAxis)
  32. {
  33. currentRotation += isXAxis ? -input : input;
  34. return Mathf.Clamp(currentRotation, min, max);
  35. }
  36. }