187 lines
5.8 KiB
C#
187 lines
5.8 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
|
|
|
|
[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)")]
|
|
public float spiralRadius = 5f;
|
|
public float spiralSpeed = 1f; // Rotations per second
|
|
public float spiralHeight = 5f; // How high to move up during spiral
|
|
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 leftCenter;
|
|
private Vector3 rightCenter;
|
|
|
|
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;
|
|
|
|
void OnEnable()
|
|
{
|
|
// Store initial positions as center points
|
|
if (baseCircle != null)
|
|
baseCenter = baseCircle.position;
|
|
if (left != null)
|
|
{
|
|
leftCenter = left.position;
|
|
left.gameObject.SetActive(false); // Deactivate left
|
|
}
|
|
if (right != null)
|
|
{
|
|
rightCenter = right.position;
|
|
right.gameObject.SetActive(false); // Deactivate right
|
|
}
|
|
|
|
// 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)
|
|
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 left and right when delay is over
|
|
if (left != null)
|
|
left.gameObject.SetActive(true);
|
|
if (right != null)
|
|
right.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;
|
|
|
|
// Left - spiral movement
|
|
if (left != null)
|
|
{
|
|
float x = leftCenter.x + spiralRadius * Mathf.Cos(spiralAngle);
|
|
float z = leftCenter.z + spiralRadius * Mathf.Sin(spiralAngle);
|
|
float y = leftCenter.y + (spiralHeight * progress);
|
|
left.position = new Vector3(x, y, z);
|
|
}
|
|
|
|
// Right - spiral movement (opposite side, start at 180 degrees)
|
|
if (right != null)
|
|
{
|
|
float oppositeAngle = spiralAngle + Mathf.PI; // Add 180 degrees
|
|
float x = rightCenter.x + spiralRadius * Mathf.Cos(oppositeAngle);
|
|
float z = rightCenter.z + spiralRadius * Mathf.Sin(oppositeAngle);
|
|
float y = rightCenter.y + (spiralHeight * progress);
|
|
right.position = new Vector3(x, y, z);
|
|
}
|
|
}
|
|
}
|
|
} |