Files

67 lines
1.9 KiB
C#
Raw Permalink Normal View History

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