44 lines
1.2 KiB
C#
44 lines
1.2 KiB
C#
using Syntriax.Modules.Movement.ColliderCheck;
|
|
using UnityEngine;
|
|
|
|
namespace Syntriax.Modules.Movement
|
|
{
|
|
[RequireComponent(typeof(Rigidbody2D))]
|
|
public class OneDimensional2DGroundMovement : MonoBehaviour, IMovement, IState
|
|
{
|
|
private IGroundCheck groundCheck = null;
|
|
private Rigidbody2D rigid = null;
|
|
private float moveValue = 0f;
|
|
|
|
public bool IsActive => StateEnabled && groundCheck.IsCollided();
|
|
public bool StateEnabled { get; set; } = true;
|
|
public float MovementMultiplier { get; set; } = 1f;
|
|
|
|
private void Start()
|
|
{
|
|
rigid = GetComponent<Rigidbody2D>();
|
|
groundCheck = GetComponentInChildren<IGroundCheck>();
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (!IsActive)
|
|
return;
|
|
|
|
ApplyMovement();
|
|
}
|
|
|
|
private void ApplyMovement()
|
|
{
|
|
Vector2 velocity = rigid.velocity;
|
|
velocity.x = moveValue;
|
|
rigid.velocity = velocity;
|
|
}
|
|
|
|
public void Move(float x = 0, float y = 0, float z = 0)
|
|
{
|
|
moveValue = x * MovementMultiplier;
|
|
}
|
|
}
|
|
}
|