cameraRPG.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using UnityEngine;
  2. using DG.Tweening;
  3. public class cameraRPG : MonoBehaviour
  4. {
  5. [Header("Core Settings")]
  6. public static cameraRPG instance;
  7. public Transform focus;
  8. public Vector3 offset = new Vector3(0, 0, -10);
  9. [Header("Movement")]
  10. public float followSpeed = 2f;
  11. public bool useSmoothing = true;
  12. [Header("Deadzone (Optional)")]
  13. public bool useDeadzone = false;
  14. public Vector2 deadzoneSize = new Vector2(2f, 1f);
  15. [Header("Look Ahead")]
  16. public bool useLookAhead = false;
  17. public float lookAheadDistance = 2f;
  18. public float lookAheadSpeed = 1f;
  19. [Header("Shake")]
  20. public float shakeDuration = 0.5f;
  21. public float shakeStrength = 1f;
  22. // Private variables
  23. private Vector3 targetPosition;
  24. private Vector3 lookAheadOffset;
  25. private bool isPaused = false;
  26. private Tween currentMoveTween;
  27. private Camera cam;
  28. void Awake()
  29. {
  30. if (instance == null)
  31. {
  32. instance = this;
  33. }
  34. else
  35. {
  36. Destroy(gameObject);
  37. return;
  38. }
  39. cam = GetComponent<Camera>();
  40. }
  41. void Start()
  42. {
  43. if (focus != null)
  44. {
  45. transform.position = focus.position + offset;
  46. }
  47. }
  48. public void SetTarget(Transform target)
  49. {
  50. focus = target;
  51. }
  52. public void SetPaused(bool paused)
  53. {
  54. isPaused = paused;
  55. }
  56. void LateUpdate()
  57. {
  58. if (focus == null || isPaused) return;
  59. CalculateTargetPosition();
  60. MoveCamera();
  61. }
  62. void CalculateTargetPosition()
  63. {
  64. targetPosition = focus.position + offset;
  65. // Look ahead based on movement
  66. if (useLookAhead)
  67. {
  68. Vector3 targetLookAhead = Vector3.zero;
  69. // You might want to get velocity from your player controller instead
  70. Rigidbody2D focusRb = focus.GetComponent<Rigidbody2D>();
  71. if (focusRb != null)
  72. {
  73. Vector3 velocity = focusRb.velocity;
  74. targetLookAhead = velocity.normalized * lookAheadDistance;
  75. }
  76. lookAheadOffset = Vector3.Lerp(lookAheadOffset, targetLookAhead, Time.deltaTime * lookAheadSpeed);
  77. targetPosition += lookAheadOffset;
  78. }
  79. // Apply deadzone
  80. if (useDeadzone)
  81. {
  82. Vector3 currentPos = transform.position;
  83. Vector3 difference = targetPosition - currentPos;
  84. // Only move if outside deadzone
  85. if (Mathf.Abs(difference.x) > deadzoneSize.x / 2f)
  86. {
  87. targetPosition.x = currentPos.x + (difference.x - Mathf.Sign(difference.x) * deadzoneSize.x / 2f);
  88. }
  89. else
  90. {
  91. targetPosition.x = currentPos.x;
  92. }
  93. if (Mathf.Abs(difference.y) > deadzoneSize.y / 2f)
  94. {
  95. targetPosition.y = currentPos.y + (difference.y - Mathf.Sign(difference.y) * deadzoneSize.y / 2f);
  96. }
  97. else
  98. {
  99. targetPosition.y = currentPos.y;
  100. }
  101. }
  102. }
  103. void MoveCamera()
  104. {
  105. if (useSmoothing)
  106. {
  107. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * followSpeed);
  108. }
  109. else
  110. {
  111. transform.position = targetPosition;
  112. }
  113. }
  114. // DOTween enhanced methods
  115. public void TeleportTo(Vector3 newPosition)
  116. {
  117. currentMoveTween?.Kill();
  118. transform.position = newPosition + offset;
  119. }
  120. public void SmoothTeleportTo(Vector3 newPosition, float duration = 1f, Ease easeType = Ease.OutCubic)
  121. {
  122. currentMoveTween?.Kill();
  123. Vector3 targetPos = newPosition + offset;
  124. currentMoveTween = transform.DOMove(targetPos, duration).SetEase(easeType);
  125. }
  126. public void Shake(float duration = -1, float strength = -1)
  127. {
  128. if (duration < 0) duration = shakeDuration;
  129. if (strength < 0) strength = shakeStrength;
  130. transform.DOShakePosition(duration, strength);
  131. }
  132. public void ZoomTo(float targetSize, float duration = 1f, Ease easeType = Ease.OutCubic)
  133. {
  134. if (cam != null)
  135. {
  136. cam.DOOrthoSize(targetSize, duration).SetEase(easeType);
  137. }
  138. }
  139. public void PunchScale(float strength = 0.1f, float duration = 0.3f)
  140. {
  141. transform.DOPunchScale(Vector3.one * strength, duration);
  142. }
  143. // Utility methods
  144. public Vector3 GetTargetPosition()
  145. {
  146. return targetPosition;
  147. }
  148. public bool IsMoving()
  149. {
  150. return Vector3.Distance(transform.position, targetPosition) > 0.01f;
  151. }
  152. void OnDrawGizmosSelected()
  153. {
  154. // Draw deadzone
  155. if (useDeadzone)
  156. {
  157. Gizmos.color = Color.yellow;
  158. Gizmos.DrawWireCube(transform.position, new Vector3(deadzoneSize.x, deadzoneSize.y, 0));
  159. }
  160. }
  161. }