maxhacker11

PlayerMelee.cs

Nov 11th, 2023
935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMelee : MonoBehaviour
  6. {
  7.     public Transform attackOrigin;
  8.     public float attackRadius = 1f;
  9.     public LayerMask enemyMask;
  10.  
  11.     public float cooldownTime = .5f;
  12.     private float cooldownTimer = 0f;
  13.  
  14.     public int attackDamage = 25;
  15.  
  16.     public Animator animator;
  17.  
  18.     private void Update()
  19.     {
  20.         if (cooldownTimer <= 0)
  21.         {
  22.             if (Input.GetKey(KeyCode.K))
  23.             {
  24.                 // Example of playing attack animation
  25.                 animator.SetTrigger("Melee");
  26.  
  27.                 Collider2D[] enemiesInRange = Physics2D.OverlapCircleAll(attackOrigin.position, attackRadius, enemyMask);
  28.                 foreach (var enemy in enemiesInRange)
  29.                 {
  30.                     enemy.GetComponent<HealthManager>().TakeDamage(attackDamage, transform.position);
  31.                 }
  32.  
  33.                 cooldownTimer = cooldownTime;
  34.             }
  35.         }
  36.         else
  37.         {
  38.             cooldownTimer -= Time.deltaTime;
  39.         }
  40.     }
  41.  
  42.     private void OnDrawGizmos()
  43.     {
  44.         Gizmos.DrawWireSphere(attackOrigin.position, attackRadius);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment