Files
Crash-Course/Assets/Scripts/Punch.cs

62 lines
1.3 KiB
C#
Raw Permalink Normal View History

using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(PlayerInput))]
[RequireComponent(typeof(AnimationPlayer))]
public class Punch : MonoBehaviour
{
public bool cancelable = true;
public static event System.Action<GameObject> OnPlayerPunched;
[SerializeField] private BoxCollider2D hurtbox;
InputActionAsset actions;
private void Start()
{
actions = GetComponent<PlayerInput>().actions;
}
private void Update()
{
2025-02-08 18:54:21 -05:00
if (actions.FindAction("Punch").WasPressedThisFrame())
{
if (!cancelable) return;
2025-02-08 18:54:21 -05:00
ExecutePunch();
}
}
2025-02-08 18:54:21 -05:00
private void ExecutePunch()
{
GetComponent<AnimationPlayer>().Punch();
DisableCancellation();
GetComponent<PlayerMovement>().maxSpeedOverride = 1f;
OnPlayerPunched?.Invoke(gameObject);
2025-02-08 18:54:21 -05:00
}
public void EnableHurtbox()
{
2025-02-16 16:47:45 -05:00
if (hurtbox != null) hurtbox.enabled = true;
}
public void DisableHurtbox()
{
2025-02-16 16:47:45 -05:00
if (hurtbox != null) hurtbox.enabled = false;
}
public void DisableCancellation()
{
cancelable = false;
}
public void EnableCancellation()
{
cancelable = true;
}
2025-02-08 18:54:21 -05:00
public void ReturnToMaxSpeed()
{
GetComponent<PlayerMovement>().maxSpeedOverride = GetComponent<PlayerMovement>().maxSpeed;
}
}