30 lines
922 B
C#
30 lines
922 B
C#
using UnityEngine;
|
|
|
|
public class MoveInCircle : MonoBehaviour
|
|
{
|
|
[SerializeField] private float radius = 2f; // Radius of the circular movement
|
|
[SerializeField] private float speed = 2f; // Speed of circling
|
|
[SerializeField] private bool clockwise = true; // Direction toggle
|
|
private float angle = 0f; // Angle for movement
|
|
|
|
private Vector3 startLocalPosition;
|
|
|
|
void Start()
|
|
{
|
|
startLocalPosition = transform.localPosition; // Store the initial local position
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
// Increment angle based on speed and direction
|
|
angle += (clockwise ? -1 : 1) * speed * Time.deltaTime;
|
|
|
|
// Calculate new local position
|
|
float x = Mathf.Cos(angle) * radius;
|
|
float z = Mathf.Sin(angle) * radius;
|
|
|
|
// Apply the new position relative to the parent
|
|
transform.localPosition = startLocalPosition + new Vector3(x, 0, z);
|
|
}
|
|
}
|