255 lines
8.3 KiB
C#
255 lines
8.3 KiB
C#
using UnityEngine;
|
|
|
|
public class CircleMovement : MonoBehaviour
|
|
{
|
|
[Header("Movement Targets")]
|
|
public Transform baseCircle; // Assign "base circle" child here
|
|
public Transform left; // Assign "left" child here
|
|
public Transform right; // Assign "right" child here
|
|
public Transform front; // Assign "front" child here
|
|
public Transform back; // Assign "back" child here
|
|
|
|
[Header("Circle Settings (Base)")]
|
|
public float circleRadius = 5f;
|
|
public float circleSpeed = 1f; // Rotations per second
|
|
public float circleDuration = 5f; // How long to move before stopping (seconds)
|
|
public float circleDelay = 2f; // Delay before starting movement (seconds)
|
|
|
|
[Header("Spiral Settings (Left/Right/Front/Back)")]
|
|
public float spiralStartRadius = 5f; // Starting radius
|
|
public float spiralEndRadius = 4f; // Ending radius
|
|
public float spiralSpeed = 1f; // Rotations per second
|
|
public float spiralHeight = 5f; // How much to move vertically (positive = up, negative = down)
|
|
public float spiralDuration = 5f; // How long to move before stopping (seconds)
|
|
public float spiralDelay = 2f; // Delay before starting movement (seconds)
|
|
|
|
private Vector3 baseCenter;
|
|
private Vector3 spiralCenter;
|
|
private float spiralStartY; // Store the starting Y position for spiral objects
|
|
|
|
private float circleAngle = 0f;
|
|
private float spiralAngle = 0f;
|
|
|
|
private bool circleMoving = false;
|
|
private bool circleWaiting = false;
|
|
private float circleElapsedTime = 0f;
|
|
private float circleDelayTimer = 0f;
|
|
|
|
private bool spiralMoving = false;
|
|
private bool spiralWaiting = false;
|
|
private float spiralElapsedTime = 0f;
|
|
private float spiralDelayTimer = 0f;
|
|
|
|
private bool initialized = false;
|
|
|
|
void Awake()
|
|
{
|
|
// Store the initial Y position of spiral objects before anything happens
|
|
// We'll use the first available spiral object's Y position
|
|
if (left != null)
|
|
spiralStartY = left.position.y;
|
|
else if (right != null)
|
|
spiralStartY = right.position.y;
|
|
else if (front != null)
|
|
spiralStartY = front.position.y;
|
|
else if (back != null)
|
|
spiralStartY = back.position.y;
|
|
else
|
|
spiralStartY = transform.position.y; // Fallback to parent position
|
|
|
|
initialized = true;
|
|
}
|
|
|
|
void OnEnable()
|
|
{
|
|
// Make sure Awake has run
|
|
if (!initialized)
|
|
{
|
|
if (left != null)
|
|
spiralStartY = left.position.y;
|
|
else if (right != null)
|
|
spiralStartY = right.position.y;
|
|
else if (front != null)
|
|
spiralStartY = front.position.y;
|
|
else if (back != null)
|
|
spiralStartY = back.position.y;
|
|
else
|
|
spiralStartY = transform.position.y;
|
|
}
|
|
|
|
// Store initial positions as center points
|
|
if (baseCircle != null)
|
|
baseCenter = baseCircle.position;
|
|
|
|
// Use parent's position as the shared center for X-Z plane
|
|
spiralCenter = transform.position;
|
|
|
|
// Deactivate all spiral objects AFTER storing their positions
|
|
if (left != null)
|
|
left.gameObject.SetActive(false);
|
|
if (right != null)
|
|
right.gameObject.SetActive(false);
|
|
if (front != null)
|
|
front.gameObject.SetActive(false);
|
|
if (back != null)
|
|
back.gameObject.SetActive(false);
|
|
|
|
// Initialize circle movement
|
|
circleMoving = false;
|
|
circleWaiting = true;
|
|
circleAngle = 0f;
|
|
circleElapsedTime = 0f;
|
|
circleDelayTimer = 0f;
|
|
|
|
// Initialize spiral movement
|
|
spiralMoving = false;
|
|
spiralWaiting = true;
|
|
spiralAngle = 0f;
|
|
spiralElapsedTime = 0f;
|
|
spiralDelayTimer = 0f;
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
circleMoving = false;
|
|
circleWaiting = false;
|
|
spiralMoving = false;
|
|
spiralWaiting = false;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
// Handle circle movement (base)
|
|
HandleCircleMovement();
|
|
|
|
// Handle spiral movement (left/right/front/back)
|
|
HandleSpiralMovement();
|
|
}
|
|
|
|
void HandleCircleMovement()
|
|
{
|
|
// Handle delay
|
|
if (circleWaiting)
|
|
{
|
|
circleDelayTimer += Time.deltaTime;
|
|
|
|
if (circleDelayTimer >= circleDelay)
|
|
{
|
|
circleWaiting = false;
|
|
circleMoving = true;
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Handle movement
|
|
if (circleMoving)
|
|
{
|
|
circleElapsedTime += Time.deltaTime;
|
|
|
|
// Stop after duration
|
|
if (circleElapsedTime >= circleDuration)
|
|
{
|
|
circleMoving = false;
|
|
return;
|
|
}
|
|
|
|
// Update angle
|
|
circleAngle += circleSpeed * Time.deltaTime * Mathf.PI * 2f;
|
|
|
|
// Base circle - normal circle movement on X-Z plane
|
|
if (baseCircle != null)
|
|
{
|
|
float x = baseCenter.x + circleRadius * Mathf.Cos(circleAngle);
|
|
float z = baseCenter.z + circleRadius * Mathf.Sin(circleAngle);
|
|
baseCircle.position = new Vector3(x, baseCenter.y, z);
|
|
}
|
|
}
|
|
}
|
|
|
|
void HandleSpiralMovement()
|
|
{
|
|
// Handle delay
|
|
if (spiralWaiting)
|
|
{
|
|
spiralDelayTimer += Time.deltaTime;
|
|
|
|
if (spiralDelayTimer >= spiralDelay)
|
|
{
|
|
spiralWaiting = false;
|
|
spiralMoving = true;
|
|
|
|
// Activate all spiral objects at the same time
|
|
if (left != null)
|
|
left.gameObject.SetActive(true);
|
|
if (right != null)
|
|
right.gameObject.SetActive(true);
|
|
if (front != null)
|
|
front.gameObject.SetActive(true);
|
|
if (back != null)
|
|
back.gameObject.SetActive(true);
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Handle movement
|
|
if (spiralMoving)
|
|
{
|
|
spiralElapsedTime += Time.deltaTime;
|
|
|
|
// Stop after duration
|
|
if (spiralElapsedTime >= spiralDuration)
|
|
{
|
|
spiralMoving = false;
|
|
return;
|
|
}
|
|
|
|
// Update angle
|
|
spiralAngle += spiralSpeed * Time.deltaTime * Mathf.PI * 2f;
|
|
|
|
// Calculate progress (0 to 1)
|
|
float progress = spiralElapsedTime / spiralDuration;
|
|
|
|
// Lerp radius from start to end based on progress
|
|
float currentRadius = Mathf.Lerp(spiralStartRadius, spiralEndRadius, progress);
|
|
|
|
// Calculate Y position - starts at spiralStartY and moves by spiralHeight
|
|
// Works for both positive (up) and negative (down) heights
|
|
float y = spiralStartY + (spiralHeight * progress);
|
|
|
|
// Left - spiral movement (0 degrees)
|
|
if (left != null)
|
|
{
|
|
float x = spiralCenter.x + currentRadius * Mathf.Cos(spiralAngle);
|
|
float z = spiralCenter.z + currentRadius * Mathf.Sin(spiralAngle);
|
|
left.position = new Vector3(x, y, z);
|
|
}
|
|
|
|
// Right - spiral movement (180 degrees opposite)
|
|
if (right != null)
|
|
{
|
|
float angle = spiralAngle + Mathf.PI; // 180 degrees
|
|
float x = spiralCenter.x + currentRadius * Mathf.Cos(angle);
|
|
float z = spiralCenter.z + currentRadius * Mathf.Sin(angle);
|
|
right.position = new Vector3(x, y, z);
|
|
}
|
|
|
|
// Front - spiral movement (90 degrees)
|
|
if (front != null)
|
|
{
|
|
float angle = spiralAngle + Mathf.PI * 0.5f; // 90 degrees
|
|
float x = spiralCenter.x + currentRadius * Mathf.Cos(angle);
|
|
float z = spiralCenter.z + currentRadius * Mathf.Sin(angle);
|
|
front.position = new Vector3(x, y, z);
|
|
}
|
|
|
|
// Back - spiral movement (270 degrees)
|
|
if (back != null)
|
|
{
|
|
float angle = spiralAngle + Mathf.PI * 1.5f; // 270 degrees
|
|
float x = spiralCenter.x + currentRadius * Mathf.Cos(angle);
|
|
float z = spiralCenter.z + currentRadius * Mathf.Sin(angle);
|
|
back.position = new Vector3(x, y, z);
|
|
}
|
|
}
|
|
}
|
|
} |