using UnityEngine;
using Game;
using Music;
using Player;
namespace Game
{
///
/// This class controls a platform that moves between specified points in a loop.
///
public class MovingPlatform : MonoBehaviour
{
///
/// The platform object that will move.
///
public Transform platform;
///
/// The index of the starting point for the platform.
///
public int startPoint;
///
/// An array of points that the platform will move between.
///
public Transform[] points;
///
/// The speed at which the platform moves.
///
public float speed;
///
/// The current target point index.
///
private int i;
///
/// Sets the initial position of the platform to the starting point.
///
private void Start()
{
transform.position = points[startPoint].position;
}
///
/// Moves the platform between points in a loop.
///
private void FixedUpdate()
{
// If the platform is close to the target point, update to the next point
if (Vector2.Distance(transform.position, points[i].position) < 0.02f)
{
i++;
if (i == points.Length)
{
i = 0; // Loop back to the first point
}
}
// Move the platform towards the current target point
GetComponent().MovePosition(
Vector2.MoveTowards(transform.position, points[i].position, speed * Time.fixedDeltaTime)
);
}
}
}