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

53 lines
1.3 KiB
C#
Raw Normal View History

2022-02-23 13:47:46 +03:00
using Movement;
using UnityEngine;
namespace Platforms
{
public class MovingPlatform : MonoBehaviour, IMovement
{
2022-02-23 15:05:30 +03:00
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);
}
2022-02-23 13:47:46 +03:00
// PAUSING METHODS
2022-02-23 15:05:30 +03:00
public bool IsPaused { get; private set; }
2022-02-23 13:47:46 +03:00
public void Pause()
{
2022-02-23 15:05:30 +03:00
IsPaused = true;
_platformRigidbody.simulated = !IsPaused;
2022-02-23 13:47:46 +03:00
}
public void Resume()
{
2022-02-23 15:05:30 +03:00
IsPaused = false;
_platformRigidbody.simulated = !IsPaused;
2022-02-23 13:47:46 +03:00
}
// MOVEMENT METHODS
public float BaseSpeed { get; set; }
2022-02-23 15:05:30 +03:00
2022-02-23 13:47:46 +03:00
public void Move(float value)
{
2022-02-23 15:05:30 +03:00
var position = transform.position;
var targetPosition = new Vector3(position.x + xOffeset, position.y + yOffset, 0.0f);
2022-02-23 15:28:25 +03:00
position = Vector3.SmoothDamp(position, targetPosition, ref _velocity, 1.0f, 1.0f);
2022-02-23 15:05:30 +03:00
transform.position = position;
2022-02-23 13:47:46 +03:00
}
}
2022-02-23 15:05:30 +03:00
}