using UnityEngine; public class BatSwarmMovement : MonoBehaviour { [Header("Movement Settings")] [SerializeField] private float radius = 2f; // Radius of the circular movement [SerializeField] private float verticalAmount = 1.5f; // Amount of vertical movement [SerializeField] private float rotationSpeed = 2f; // Base rotation speed [SerializeField] private float bobSpeed = 1.5f; // Speed of up/down movement [Header("Chaos Settings")] [SerializeField] private float chaosAmount = 0.5f; // Amount of random movement [SerializeField] private float timeVariance = 0.5f; // Variance in timing private Vector3 startPosition; private float individualOffset; private float verticalOffset; private float rotationOffset; private void Start() { // Store the initial position startPosition = transform.position; // Generate random offsets for this specific bat individualOffset = Random.Range(0f, 2f * Mathf.PI); verticalOffset = Random.Range(0f, 2f * Mathf.PI); rotationOffset = Random.Range(-0.5f, 0.5f); } private void Update() { float time = Time.time; // Calculate base circular movement float adjustedTime = time * rotationSpeed + individualOffset; float x = Mathf.Cos(adjustedTime) * radius; float z = Mathf.Sin(adjustedTime) * radius; // Add vertical movement using a different frequency float verticalMovement = Mathf.Sin(time * bobSpeed + verticalOffset) * verticalAmount; // Add chaos to the movement float chaosX = Mathf.PerlinNoise(time + individualOffset, 0f) * chaosAmount; float chaosY = Mathf.PerlinNoise(0f, time + individualOffset) * chaosAmount; float chaosZ = Mathf.PerlinNoise(time + verticalOffset, time + individualOffset) * chaosAmount; // Combine all movements Vector3 newPosition = startPosition + new Vector3( x + chaosX, verticalMovement + chaosY, z + chaosZ ); // Update position transform.position = newPosition; // Calculate forward direction for rotation Vector3 direction = (newPosition - transform.position).normalized; if (direction != Vector3.zero) { // Add some wobble to the rotation float rotationWobble = Mathf.Sin(time * (bobSpeed + rotationOffset)) * 15f; Quaternion targetRotation = Quaternion.LookRotation(direction); targetRotation *= Quaternion.Euler(0f, 0f, rotationWobble); // Smoothly rotate towards the direction of movement transform.rotation = Quaternion.Slerp( transform.rotation, targetRotation, Time.deltaTime * 5f ); } transform.LookAt(transform.parent); } // Optional: Visualize the movement radius in the editor private void OnDrawGizmosSelected() { Gizmos.color = Color.yellow; Gizmos.DrawWireSphere(transform.position, radius); } }