Eonirr

Untitled

Dec 23rd, 2025
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class TopDownPlayerController : MonoBehaviour
  4. {
  5. public Transform camTransform;
  6.  
  7. [Header("ROTATE ON MOVE")]
  8. public bool rotatePlayerOnMove;
  9.  
  10. [Header("MOVEMENT")]
  11. public float moveSpeed;
  12.  
  13. [Header("ROTATION")]
  14. public float playerRotateSpeed;
  15. public float camRotateSpeed;
  16.  
  17. [Header("CAMERA")]
  18. public Vector3 cameraOffset;
  19. public float camFollowSpeed;
  20. public float camTiltAngle;
  21.  
  22. Vector3 moveInput;
  23. float camYRotation;
  24. float lastCamYRotation;
  25. bool cameraHasRotated;
  26.  
  27. void Start()
  28. {
  29. camYRotation = camTransform.eulerAngles.y;
  30.  
  31. lastCamYRotation = camYRotation;
  32. }
  33.  
  34. void Update()
  35. {
  36. HandleInput();
  37.  
  38. MovePlayer();
  39.  
  40. UpdateCameraPosition();
  41. HandleCameraRotation();
  42.  
  43. cameraHasRotated = Mathf.Abs(camYRotation - lastCamYRotation) > 0.01f;
  44. lastCamYRotation = camYRotation;
  45. }
  46.  
  47. void HandleInput()
  48. {
  49. moveInput = Vector3.zero;
  50. if (Input.GetKey(KeyCode.W)) moveInput += Vector3.forward;
  51. if (Input.GetKey(KeyCode.S)) moveInput += Vector3.back;
  52. if (Input.GetKey(KeyCode.A)) moveInput += Vector3.left;
  53. if (Input.GetKey(KeyCode.D)) moveInput += Vector3.right;
  54. }
  55.  
  56. void MovePlayer()
  57. {
  58. if (moveInput.sqrMagnitude < 0.01f) return;
  59.  
  60. Vector3 camForward = Quaternion.Euler(0, camYRotation, 0) * Vector3.forward;
  61. Vector3 camRight = Quaternion.Euler(0, camYRotation, 0) * Vector3.right;
  62.  
  63. Vector3 worldMove = (camForward * moveInput.z + camRight * moveInput.x).normalized;
  64.  
  65. transform.position += worldMove * moveSpeed * Time.deltaTime;
  66. worldMove.y = 0;
  67.  
  68. bool isMovingForward = moveInput.z > 0.1f;
  69.  
  70. bool shouldRotate = rotatePlayerOnMove || cameraHasRotated || isMovingForward;
  71.  
  72. if (!shouldRotate) return;
  73.  
  74. Quaternion targetRot = Quaternion.LookRotation(worldMove);
  75. transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, playerRotateSpeed * Time.deltaTime);
  76. }
  77.  
  78. void HandleCameraRotation()
  79. {
  80. float camInput = 0f;
  81. if (Input.GetKey(KeyCode.Q)) camInput = -1f;
  82. if (Input.GetKey(KeyCode.E)) camInput = 1f;
  83.  
  84. if (camInput != 0f) camYRotation += camInput * camRotateSpeed * Time.deltaTime;
  85. }
  86.  
  87. void UpdateCameraPosition()
  88. {
  89. Quaternion camYRot = Quaternion.Euler(0f, camYRotation, 0f);
  90. Vector3 targetPos = transform.position + camYRot * cameraOffset;
  91.  
  92. camTransform.position = Vector3.Lerp(camTransform.position, targetPos, camFollowSpeed * Time.deltaTime);
  93.  
  94. Vector3 lookTarget = transform.position + Vector3.up * 1.5f;
  95. camTransform.rotation = Quaternion.Euler(camTiltAngle, camYRotation, 0f);
  96. camTransform.LookAt(lookTarget);
  97. }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment