BGJ-2022.1/Assets/Scripts/Platforms/MovingPlatform.cs

53 lines
1.3 KiB
C#

using Movement;
using UnityEngine;
namespace Platforms
{
public class MovingPlatform : MonoBehaviour, IMovement
{
public float xOffeset;
public float yOffset;
private Rigidbody2D _platformRigidbody;
private Vector3 _velocity = Vector3.zero;
private void Awake()
{
_platformRigidbody = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
if (!IsPaused)
Move(BaseSpeed);
}
// PAUSING METHODS
public bool IsPaused { get; private set; }
public void Pause()
{
IsPaused = true;
_platformRigidbody.simulated = !IsPaused;
}
public void Resume()
{
IsPaused = false;
_platformRigidbody.simulated = !IsPaused;
}
// MOVEMENT METHODS
public float BaseSpeed { get; set; }
public void Move(float value)
{
var position = transform.position;
var targetPosition = new Vector3(position.x + xOffeset, position.y + yOffset, 0.0f);
position = Vector3.SmoothDamp(position, targetPosition, ref _velocity, 1.0f, 1.0f);
transform.position = position;
}
}
}