Movement/Samples/2D/Scripts/GroundMovement1D.cs

44 lines
1.2 KiB
C#
Raw Permalink Normal View History

using Syntriax.Modules.Trigger;
using UnityEngine;
namespace Syntriax.Modules.Movement.Samples
{
2023-03-21 16:06:14 +03:00
[System.Serializable]
[RequireComponent(typeof(Rigidbody2D))]
public class GroundMovement1D : MovementBase1D
{
protected override float moveValue { get; set; } = 0f;
protected IGroundTrigger groundTrigger = null;
protected Rigidbody2D rigid = null;
2022-11-15 16:26:26 +03:00
private void Awake() => CanTakeOver = true;
protected override void Start()
{
base.Start();
rigid = GetComponentInParent<Rigidbody2D>();
groundTrigger = GetComponentInChildren<IGroundTrigger>();
2022-11-15 14:30:08 +03:00
if (groundTrigger != null)
2022-11-15 16:26:26 +03:00
{
2022-12-17 11:59:08 +03:00
groundTrigger.OnTriggerStateChanged += OnGroundTrigger;
2022-11-15 16:26:26 +03:00
CanTakeOver = false;
}
}
private void OnGroundTrigger(bool isGrounded)
=> CanTakeOver = isGrounded;
public override void ApplyMovement()
{
Vector2 velocity = rigid.velocity;
velocity.x = moveValue;
rigid.velocity = velocity;
}
protected override void OnDeactivated() { }
protected override void OnActivated() { }
}
}