Movement/2D/OneDimensional2DAirMovement.cs

55 lines
1.6 KiB
C#
Raw Normal View History

2021-12-23 12:41:01 +03:00
using Syntriax.Modules.Movement.ColliderCheck;
using UnityEngine;
namespace Syntriax.Modules.Movement
{
[RequireComponent(typeof(Rigidbody2D))]
public class OneDimensional2DAirMovement : 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;
2022-02-19 16:02:38 +03:00
public float BaseSpeed { get; set; } = 1f;
2021-12-23 13:41:32 +03:00
public float MovementMultiplier { get; set; } = 1f;
2021-12-23 12:41:01 +03:00
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 * Time.fixedDeltaTime;
if (moveValue != 0f)
{
if (Mathf.Abs(velocity.x) > Mathf.Abs(moveValue))
velocity.x = moveValue;
else if (Mathf.Abs(velocity.x - moveValue) > Mathf.Abs(moveValue))
velocity.x += moveValue * Time.fixedDeltaTime;
}
rigid.velocity = velocity;
}
public void Move(float x = 0, float y = 0, float z = 0)
{
2022-02-19 16:46:24 +03:00
moveValue = x * BaseSpeed * MovementMultiplier;
2021-12-23 12:41:01 +03:00
}
}
}