Moving platform script
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using Movement;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -5,27 +6,39 @@ namespace Platforms
|
||||
{
|
||||
public class MovingPlatform : MonoBehaviour, IMovement
|
||||
{
|
||||
public float xOffeset;
|
||||
public float xOffset;
|
||||
public float yOffset;
|
||||
|
||||
private Vector3 _originalPos;
|
||||
private Vector3 _futurePos;
|
||||
private const float VerificationOffset = 0.04f;
|
||||
|
||||
private bool _goingToFuturePos;
|
||||
|
||||
private Rigidbody2D _platformRigidbody;
|
||||
|
||||
private Vector3 _velocity = Vector3.zero;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_platformRigidbody = GetComponent<Rigidbody2D>();
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
private void Start()
|
||||
{
|
||||
_originalPos = transform.position;
|
||||
_futurePos = new Vector3(_originalPos.x + xOffset, _originalPos.y + yOffset, _originalPos.z);
|
||||
_goingToFuturePos = true;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!IsPaused)
|
||||
Move(BaseSpeed);
|
||||
}
|
||||
|
||||
// PAUSING METHODS
|
||||
|
||||
|
||||
public bool IsPaused { get; private set; }
|
||||
|
||||
public void Pause()
|
||||
{
|
||||
IsPaused = true;
|
||||
@@ -37,17 +50,28 @@ namespace Platforms
|
||||
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;
|
||||
switch (_goingToFuturePos)
|
||||
{
|
||||
case true:
|
||||
transform.position += new Vector3(0.05f, 0, 0);
|
||||
if (Math.Abs(_futurePos.x - transform.position.x) < VerificationOffset &&
|
||||
Math.Abs(_futurePos.y - transform.position.y) < VerificationOffset)
|
||||
_goingToFuturePos = false;
|
||||
break;
|
||||
case false:
|
||||
transform.position += new Vector3(-0.05f, 0, 0);
|
||||
if (Math.Abs(_originalPos.x - transform.position.x) < VerificationOffset &&
|
||||
Math.Abs(_originalPos.y - transform.position.y) < VerificationOffset)
|
||||
_goingToFuturePos = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user