76 lines
2.0 KiB
C#
76 lines
2.0 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class PlayerCombat : MonoBehaviour
|
|
{
|
|
[Header("Attack Parameters")]
|
|
[SerializeField] private float attackCooldown;
|
|
[SerializeField] private float range;
|
|
[SerializeField] private int damage;
|
|
|
|
[Header("Collider Parameters")]
|
|
[SerializeField] private float colliderDistance;
|
|
[SerializeField] private BoxCollider2D boxCollider;
|
|
|
|
[Header("Enemy Layer")]
|
|
[SerializeField] private LayerMask enemyLayer;
|
|
|
|
private float cooldownTimer = 0f;
|
|
|
|
//References
|
|
private Animator anim;
|
|
|
|
void Start()
|
|
{
|
|
anim = GetComponent<Animator>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
cooldownTimer += Time.deltaTime;
|
|
}
|
|
|
|
public void Melee(InputAction.CallbackContext callback)
|
|
{
|
|
if (callback.performed && cooldownTimer >= attackCooldown)
|
|
{
|
|
Debug.Log("Melee Attack");
|
|
|
|
RaycastHit2D hit =
|
|
Physics2D.BoxCast(
|
|
boxCollider.bounds.center + transform.right * range * transform.localScale.x * colliderDistance,
|
|
new Vector3(boxCollider.bounds.size.x * range, boxCollider.bounds.size.y, boxCollider.bounds.size.z),
|
|
0,
|
|
Vector2.left,
|
|
0,
|
|
enemyLayer);
|
|
|
|
anim.SetTrigger("meleeAttack");
|
|
cooldownTimer = 0f;
|
|
|
|
if (hit.collider != null)
|
|
{
|
|
Debug.Log("Hit");
|
|
|
|
EnemyHealth enemyHealth =
|
|
hit.transform.GetComponent<EnemyHealth>();
|
|
|
|
if (enemyHealth != null)
|
|
{
|
|
enemyHealth.TakeDamage(damage);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnDrawGizmos()
|
|
{
|
|
Gizmos.color = Color.red;
|
|
|
|
Gizmos.DrawWireCube(
|
|
boxCollider.bounds.center + transform.right * range * transform.localScale.x * colliderDistance,
|
|
new Vector3(boxCollider.bounds.size.x * range,
|
|
boxCollider.bounds.size.y,
|
|
boxCollider.bounds.size.z));
|
|
}
|
|
} |