Movement/Implementations/AirMovement1D.cs

50 lines
1.4 KiB
C#
Raw Normal View History

using Syntriax.Modules.Trigger;
using UnityEngine;
namespace Syntriax.Modules.Movement.Implementations
{
[RequireComponent(typeof(Rigidbody2D))]
public class AirMovement1D : MovementBase1D
{
protected override float moveValue { get; set; } = 0f;
protected IGroundTrigger groundTrigger = null;
protected Rigidbody2D rigid = null;
protected override void Start()
{
base.Start();
rigid = GetComponent<Rigidbody2D>();
groundTrigger = GetComponentInChildren<IGroundTrigger>();
2022-11-15 16:26:26 +03:00
if (groundTrigger != null)
{
groundTrigger.OnTriggered += OnGroundTrigger;
CanTakeOver = false;
}
}
private void OnGroundTrigger(bool isGrounded)
=> CanTakeOver = !isGrounded;
public override 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;
}
protected override void OnDeactivated() { }
protected override void OnActivated() { }
}
}