54 lines
1.8 KiB
C#
54 lines
1.8 KiB
C#
using System;
|
|
using RR.Game2D.Core;
|
|
using UnityEngine;
|
|
|
|
namespace RR.Game2D.Entities
|
|
{
|
|
public class MovingPlatform : MonoBehaviour
|
|
{
|
|
[Header("Настройки движения платформы")]
|
|
[SerializeField] private float moveSpeed = 1f;
|
|
[SerializeField] private float pathLength = 5f;
|
|
[SerializeField] private Direction2D moveDirection = Direction2D.Right;
|
|
|
|
[Header("Прикрепление игрока")]
|
|
[SerializeField] private bool attachPlayer = true;
|
|
|
|
private Vector3 _startPosition;
|
|
private Vector3 _endPosition;
|
|
|
|
private void Awake()
|
|
{
|
|
_startPosition = transform.position;
|
|
|
|
_endPosition = moveDirection switch
|
|
{
|
|
Direction2D.Down => _startPosition + Vector3.down * pathLength,
|
|
Direction2D.Left => _startPosition + Vector3.left * pathLength,
|
|
Direction2D.Right => _startPosition + Vector3.right * pathLength,
|
|
Direction2D.Up => _startPosition + Vector3.up * pathLength,
|
|
_ => _startPosition
|
|
};
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
float pingPong = Mathf.PingPong(Time.time * moveSpeed, 1f);
|
|
transform.position = Vector3.Lerp(_startPosition, _endPosition, pingPong);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |