Chocolade19721309

Untitled

May 28th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3.  
  4. #if ENABLE_INPUT_SYSTEM
  5. using UnityEngine.InputSystem;
  6. #endif
  7.  
  8. namespace AmazingAssets.AdvancedDissolve.ExampleScripts
  9. {
  10. public class ObjectOrbitalMove : MonoBehaviour
  11. {
  12. [Header("Target and Player References")]
  13. public Transform target;
  14. public Transform player; // Reference to the player
  15. public Collider houseArea; // Box collider representing the house area
  16.  
  17. [Header("Auto Move Settings")]
  18. public bool autoMove = false;
  19. public float autoMoveInterval = 2f;
  20. public float heightChangeInterval = 4f;
  21. public float heightChangeSpeed = 2f;
  22. public float minRandHeight = 8, maxRandHeight = 16;
  23. public enum RotateMode { Left, Right, Random };
  24. public RotateMode rotateMode = RotateMode.Random;
  25.  
  26. [Header("Random Move Settings")]
  27. public float randomMoveDirectionChangeInterval = 2f; // Interval for changing direction in Random mode
  28.  
  29. [Header("Chase Settings")]
  30. public float chaseSpeed = 5f;
  31. public float chaseRotationSpeed = 5f;
  32. public float followDistance = 2f; // Distance to maintain while following
  33. public Vector3 offset;
  34.  
  35. [Header("Rotation Settings")]
  36. public float rotateSpeed = 10;
  37. public float maxDistance = 20;
  38.  
  39. private float autoMoveTimer;
  40. private float heightChangeTimer;
  41. private float targetHeight;
  42. private float initialHeight;
  43. private int autoMoveDirection = 0;
  44. private bool isPaused = false;
  45. private float pauseTimer = 0;
  46. private bool isChasing = false;
  47. private Transform spotlight;
  48. private float randomMoveTimer;
  49.  
  50. void Start()
  51. {
  52. autoMoveTimer = autoMoveInterval;
  53. heightChangeTimer = heightChangeInterval;
  54. initialHeight = transform.position.y;
  55. targetHeight = initialHeight;
  56. randomMoveTimer = randomMoveDirectionChangeInterval;
  57.  
  58. // Cache the spotlight reference
  59. spotlight = transform.Find("Spot Light");
  60. }
  61.  
  62. void Update()
  63. {
  64. if (isPaused)
  65. {
  66. pauseTimer -= Time.deltaTime;
  67. if (pauseTimer <= 0)
  68. {
  69. isPaused = false;
  70. autoMoveTimer = autoMoveInterval;
  71. }
  72. return;
  73. }
  74.  
  75. // Check if the player is inside the house area
  76. if (houseArea.bounds.Contains(player.position))
  77. {
  78. isChasing = false;
  79. autoMove = true;
  80. }
  81. else
  82. {
  83. isChasing = true;
  84. autoMove = false;
  85. }
  86.  
  87. if (isChasing)
  88. {
  89. ChasePlayer();
  90. }
  91. else if (autoMove)
  92. {
  93. AutoMove();
  94. }
  95.  
  96. // Smoothly transition to target height
  97. Vector3 position = transform.position;
  98. position.y = Mathf.Lerp(position.y, targetHeight, Time.deltaTime * heightChangeSpeed);
  99. transform.position = position;
  100. }
  101.  
  102. void AutoMove()
  103. {
  104. autoMoveTimer -= Time.deltaTime;
  105. heightChangeTimer -= Time.deltaTime;
  106.  
  107. if (autoMoveTimer <= 0)
  108. {
  109. if (rotateMode == RotateMode.Random)
  110. {
  111. randomMoveTimer -= Time.deltaTime;
  112. if (randomMoveTimer <= 0)
  113. {
  114. autoMoveDirection = Random.Range(0, 2); // Random direction: 0 = Left, 1 = Right
  115. randomMoveTimer = randomMoveDirectionChangeInterval;
  116. }
  117. }
  118. else
  119. {
  120. autoMoveDirection = (rotateMode == RotateMode.Left) ? 0 : 1; // 0 = Left, 1 = Right
  121. }
  122. autoMoveTimer = autoMoveInterval;
  123. }
  124.  
  125. RotateAroundTarget(autoMoveDirection == 0);
  126.  
  127. if (heightChangeTimer <= 0)
  128. {
  129. // Correctly calculate the new height within the defined range
  130. targetHeight = Random.Range(minRandHeight, maxRandHeight);
  131. heightChangeTimer = heightChangeInterval;
  132. }
  133.  
  134. MoveForwardBackward();
  135. }
  136.  
  137. void RotateAroundTarget(bool rotateLeft)
  138. {
  139. float fOrbitCircumference = 2F * maxDistance * Mathf.PI;
  140. float fDistanceRadians = (rotateSpeed / fOrbitCircumference) * 2 * Mathf.PI;
  141. float rotationAngle = fDistanceRadians * Mathf.Rad2Deg;
  142.  
  143. if (rotateLeft)
  144. {
  145. transform.RotateAround(target.position, Vector3.up, -rotationAngle);
  146. }
  147. else
  148. {
  149. transform.RotateAround(target.position, Vector3.up, rotationAngle);
  150. }
  151. }
  152.  
  153. void MoveForwardBackward()
  154. {
  155. float t = Vector3.Distance(transform.position, target.position) / maxDistance;
  156. t = Mathf.Clamp(t, 0.35f, 1);
  157.  
  158. Vector3 mVector = (transform.position - target.position).normalized;
  159. transform.position = target.position + mVector * t * maxDistance;
  160. }
  161.  
  162. void ChasePlayer()
  163. {
  164. // Smoothly rotate the drone to look at the player
  165. Vector3 direction = (player.position - transform.position).normalized;
  166. Quaternion lookRotation = Quaternion.LookRotation(direction);
  167. transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * chaseRotationSpeed);
  168.  
  169. // Move towards the player with a specified follow distance
  170. Vector3 targetPosition = player.position - direction * followDistance;
  171. transform.position = Vector3.MoveTowards(transform.position, targetPosition, chaseSpeed * Time.deltaTime);
  172.  
  173. // Smoothly rotate the spotlight to look at the player
  174. if (spotlight != null)
  175. {
  176. Vector3 lightDirection = player.position - spotlight.position;
  177. Quaternion lightRotation = Quaternion.LookRotation(lightDirection);
  178. spotlight.rotation = Quaternion.Slerp(spotlight.rotation, lightRotation, Time.deltaTime * chaseRotationSpeed);
  179. }
  180. }
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment