Files
Crash-Course/Assets/Scripts/Player/Block.cs

60 lines
1.5 KiB
C#
Raw Normal View History

2025-04-16 19:57:54 -04:00
using UnityEngine; using Game; using Music; using Player;
2025-02-09 13:49:39 -05:00
using UnityEngine.InputSystem;
2025-04-16 19:57:54 -04:00
namespace Player
{
2025-02-09 13:49:39 -05:00
[RequireComponent(typeof(PlayerInput))]
public class Block : MonoBehaviour
{
public bool blocking = false;
2025-02-09 17:18:51 -05:00
private InputActionAsset actions;
2025-02-26 20:17:19 -05:00
private float blockPressTime = 0f;
[SerializeField] private float parryThreshold = 0.2f; // Time for successful parry
2025-02-26 20:17:19 -05:00
private bool isParrying = false;
2025-02-09 13:49:39 -05:00
private void Start()
{
actions = GetComponent<PlayerInput>().actions;
}
private void Update() // Player blocks when "block" is pressed
2025-02-09 13:49:39 -05:00
{
2025-02-09 17:18:51 -05:00
InputAction blockAction = actions.FindAction("Block");
2025-02-09 13:49:39 -05:00
if (blockAction.ReadValue<float>() == 1f)
{
2025-02-26 20:17:19 -05:00
if (!blocking)
{
blockPressTime = Time.time; // Start parry timer
2025-02-26 20:17:19 -05:00
}
2025-02-09 17:18:51 -05:00
blocking = true;
2025-02-09 13:49:39 -05:00
}
else
{
if (blocking) // Successful parry if blocked in time
2025-02-26 20:17:19 -05:00
{
float pressDuration = Time.time - blockPressTime;
if (pressDuration <= parryThreshold)
{
Parry();
2025-02-26 20:17:19 -05:00
}
else
{
isParrying = false;
}
}
2025-02-09 17:18:51 -05:00
blocking = false;
2025-02-09 13:49:39 -05:00
}
2025-02-09 17:18:51 -05:00
GetComponent<AnimationPlayer>().block = blocking;
2025-02-09 13:49:39 -05:00
}
2025-02-26 20:17:19 -05:00
private void Parry()
{
isParrying = true;
}
public bool IsParrying()
{
return isParrying;
}
2025-02-09 13:49:39 -05:00
}
2025-04-16 19:57:54 -04:00
}