Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- #if ENABLE_INPUT_SYSTEM
- using UnityEngine.InputSystem;
- #endif
- namespace AmazingAssets.AdvancedDissolve.ExampleScripts
- {
- public class ObjectOrbitalMove : MonoBehaviour
- {
- [Header("Target and Player References")]
- public Transform target;
- public Transform player; // Reference to the player
- public Collider houseArea; // Box collider representing the house area
- [Header("Auto Move Settings")]
- public bool autoMove = false;
- public float autoMoveInterval = 2f;
- public float heightChangeInterval = 4f;
- public float heightChangeSpeed = 2f;
- public float minRandHeight = 8, maxRandHeight = 16;
- public enum RotateMode { Left, Right, Random };
- public RotateMode rotateMode = RotateMode.Random;
- [Header("Random Move Settings")]
- public float randomMoveDirectionChangeInterval = 2f; // Interval for changing direction in Random mode
- [Header("Chase Settings")]
- public float chaseSpeed = 5f;
- public float chaseRotationSpeed = 5f;
- public float followDistance = 2f; // Distance to maintain while following
- public Vector3 offset;
- [Header("Rotation Settings")]
- public float rotateSpeed = 10;
- public float maxDistance = 20;
- private float autoMoveTimer;
- private float heightChangeTimer;
- private float targetHeight;
- private float initialHeight;
- private int autoMoveDirection = 0;
- private bool isPaused = false;
- private float pauseTimer = 0;
- private bool isChasing = false;
- private Transform spotlight;
- private float randomMoveTimer;
- void Start()
- {
- autoMoveTimer = autoMoveInterval;
- heightChangeTimer = heightChangeInterval;
- initialHeight = transform.position.y;
- targetHeight = initialHeight;
- randomMoveTimer = randomMoveDirectionChangeInterval;
- // Cache the spotlight reference
- spotlight = transform.Find("Spot Light");
- }
- void Update()
- {
- if (isPaused)
- {
- pauseTimer -= Time.deltaTime;
- if (pauseTimer <= 0)
- {
- isPaused = false;
- autoMoveTimer = autoMoveInterval;
- }
- return;
- }
- // Check if the player is inside the house area
- if (houseArea.bounds.Contains(player.position))
- {
- isChasing = false;
- autoMove = true;
- }
- else
- {
- isChasing = true;
- autoMove = false;
- }
- if (isChasing)
- {
- ChasePlayer();
- }
- else if (autoMove)
- {
- AutoMove();
- }
- // Smoothly transition to target height
- Vector3 position = transform.position;
- position.y = Mathf.Lerp(position.y, targetHeight, Time.deltaTime * heightChangeSpeed);
- transform.position = position;
- }
- void AutoMove()
- {
- autoMoveTimer -= Time.deltaTime;
- heightChangeTimer -= Time.deltaTime;
- if (autoMoveTimer <= 0)
- {
- if (rotateMode == RotateMode.Random)
- {
- randomMoveTimer -= Time.deltaTime;
- if (randomMoveTimer <= 0)
- {
- autoMoveDirection = Random.Range(0, 2); // Random direction: 0 = Left, 1 = Right
- randomMoveTimer = randomMoveDirectionChangeInterval;
- }
- }
- else
- {
- autoMoveDirection = (rotateMode == RotateMode.Left) ? 0 : 1; // 0 = Left, 1 = Right
- }
- autoMoveTimer = autoMoveInterval;
- }
- RotateAroundTarget(autoMoveDirection == 0);
- if (heightChangeTimer <= 0)
- {
- // Correctly calculate the new height within the defined range
- targetHeight = Random.Range(minRandHeight, maxRandHeight);
- heightChangeTimer = heightChangeInterval;
- }
- MoveForwardBackward();
- }
- void RotateAroundTarget(bool rotateLeft)
- {
- float fOrbitCircumference = 2F * maxDistance * Mathf.PI;
- float fDistanceRadians = (rotateSpeed / fOrbitCircumference) * 2 * Mathf.PI;
- float rotationAngle = fDistanceRadians * Mathf.Rad2Deg;
- if (rotateLeft)
- {
- transform.RotateAround(target.position, Vector3.up, -rotationAngle);
- }
- else
- {
- transform.RotateAround(target.position, Vector3.up, rotationAngle);
- }
- }
- void MoveForwardBackward()
- {
- float t = Vector3.Distance(transform.position, target.position) / maxDistance;
- t = Mathf.Clamp(t, 0.35f, 1);
- Vector3 mVector = (transform.position - target.position).normalized;
- transform.position = target.position + mVector * t * maxDistance;
- }
- void ChasePlayer()
- {
- // Smoothly rotate the drone to look at the player
- Vector3 direction = (player.position - transform.position).normalized;
- Quaternion lookRotation = Quaternion.LookRotation(direction);
- transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * chaseRotationSpeed);
- // Move towards the player with a specified follow distance
- Vector3 targetPosition = player.position - direction * followDistance;
- transform.position = Vector3.MoveTowards(transform.position, targetPosition, chaseSpeed * Time.deltaTime);
- // Smoothly rotate the spotlight to look at the player
- if (spotlight != null)
- {
- Vector3 lightDirection = player.position - spotlight.position;
- Quaternion lightRotation = Quaternion.LookRotation(lightDirection);
- spotlight.rotation = Quaternion.Slerp(spotlight.rotation, lightRotation, Time.deltaTime * chaseRotationSpeed);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment