62 lines
2.3 KiB
C#
62 lines
2.3 KiB
C#
using UnityEngine;
|
|
|
|
namespace RR.Game2D.Entities.Platforms
|
|
{
|
|
public class TrajectoryPlatform : MonoBehaviour
|
|
{
|
|
[Header("Настройки движения платформы")]
|
|
[SerializeField] private float moveSpeed = 1f;
|
|
[SerializeField] private Vector3[] coordinates;
|
|
[SerializeField] private bool loop = true;
|
|
|
|
[Header("Прикрепление игрока")]
|
|
[SerializeField] private bool attachPlayer = true;
|
|
|
|
private int _currentTargetIndex;
|
|
private void FixedUpdate()
|
|
{
|
|
if (coordinates == null || coordinates.Length == 0) return;
|
|
|
|
// Определяем целевую точку
|
|
Vector3 targetPoint = coordinates[_currentTargetIndex];
|
|
|
|
// Двигаем платформу к целевой точке
|
|
transform.position = Vector3.MoveTowards(transform.position, targetPoint, moveSpeed * Time.fixedDeltaTime);
|
|
|
|
// Проверяем, достигли ли целевой точки
|
|
if (!(Vector3.Distance(transform.position, targetPoint) < 0.01f)) return;
|
|
if (loop)
|
|
{
|
|
// Режим зацикливания: переходим к следующей точке по кругу
|
|
_currentTargetIndex = (_currentTargetIndex + 1) % (coordinates.Length + 1);
|
|
|
|
// Если вернулись к началу, корректируем индекс
|
|
if (_currentTargetIndex == coordinates.Length)
|
|
{
|
|
_currentTargetIndex = 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_currentTargetIndex++;
|
|
if (_currentTargetIndex < coordinates.Length) return;
|
|
transform.position = coordinates[0];
|
|
_currentTargetIndex = 0;
|
|
}
|
|
}
|
|
|
|
private void OnCollisionEnter2D(Collision2D other)
|
|
{
|
|
if (!attachPlayer) return;
|
|
if (!other.gameObject.CompareTag("Player")) return;
|
|
other.transform.SetParent(transform);
|
|
}
|
|
|
|
private void OnCollisionExit2D(Collision2D other)
|
|
{
|
|
if (!attachPlayer) return;
|
|
if (!other.gameObject.CompareTag("Player")) return;
|
|
other.transform.SetParent(null);
|
|
}
|
|
}
|
|
} |