77 lines
2.4 KiB
C#
77 lines
2.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using RR.Game2D.Core;
|
|
using UnityEngine;
|
|
|
|
public class ShiftingPlatform : MonoBehaviour
|
|
{
|
|
[Header("Настройки смещения платформы")]
|
|
[SerializeField] private Vector3 shiftAmount = new Vector3(0f, 2f, 0f);
|
|
[SerializeField] private float shiftDuration = 1f;
|
|
[SerializeField] private Direction2D shiftDirection = Direction2D.Down;
|
|
|
|
[Header("Прикрепление игрока")]
|
|
[SerializeField] private bool attachPlayer = true;
|
|
|
|
private Vector3 _startPosition;
|
|
|
|
private void Awake()
|
|
{
|
|
_startPosition = transform.position;
|
|
}
|
|
|
|
private void OnCollisionEnter2D(Collision2D other)
|
|
{
|
|
if (!other.gameObject.CompareTag("Player")) return;
|
|
|
|
if (attachPlayer) other.transform.SetParent(transform);
|
|
|
|
Vector3 shiftVector = shiftDirection switch
|
|
{
|
|
Direction2D.Down => Vector3.down * shiftAmount.y,
|
|
Direction2D.Left => Vector3.left * shiftAmount.x,
|
|
Direction2D.Right => Vector3.right * shiftAmount.x,
|
|
Direction2D.Up => Vector3.up * shiftAmount.y,
|
|
_ => Vector3.zero
|
|
};
|
|
|
|
StopAllCoroutines();
|
|
StartCoroutine(ShiftPlatform(shiftVector));
|
|
}
|
|
|
|
private void OnCollisionExit2D(Collision2D other)
|
|
{
|
|
if (!other.gameObject.CompareTag("Player")) return;
|
|
|
|
if (attachPlayer) other.transform.SetParent(null);
|
|
|
|
Vector3 shiftVector = shiftDirection switch
|
|
{
|
|
Direction2D.Down => Vector3.up * shiftAmount.y,
|
|
Direction2D.Left => Vector3.right * shiftAmount.x,
|
|
Direction2D.Right => Vector3.left * shiftAmount.x,
|
|
Direction2D.Up => Vector3.down * shiftAmount.y,
|
|
_ => Vector3.zero
|
|
};
|
|
|
|
StopAllCoroutines();
|
|
StartCoroutine(ShiftPlatform(shiftVector));
|
|
}
|
|
|
|
private IEnumerator ShiftPlatform(Vector3 shiftVector)
|
|
{
|
|
Vector3 initialPosition = transform.position;
|
|
Vector3 finalPosition = initialPosition + shiftVector;
|
|
float elapsedTime = 0f;
|
|
|
|
while (elapsedTime < shiftDuration)
|
|
{
|
|
transform.position = Vector3.Lerp(initialPosition, finalPosition, (elapsedTime / shiftDuration));
|
|
elapsedTime += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
transform.position = finalPosition;
|
|
}
|
|
}
|