JojikYT

DamageDealer

Mar 14th, 2022 (edited)
2,292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. public class DamageDealer : MonoBehaviour
  5. {
  6.     bool canDealDamage;
  7.     List<GameObject> hasDealtDamage;
  8.  
  9.     [SerializeField] float weaponLength;
  10.     [SerializeField] float weaponDamage;
  11.     void Start()
  12.     {
  13.         canDealDamage = false;
  14.         hasDealtDamage = new List<GameObject>();
  15.     }
  16.  
  17.     void Update()
  18.     {
  19.         if (canDealDamage)
  20.         {
  21.             RaycastHit hit;
  22.  
  23.             int layerMask = 1 << 9;
  24.             if (Physics.Raycast(transform.position, -transform.up, out hit, weaponLength, layerMask))
  25.             {
  26.                 if (hit.transform.TryGetComponent(out Enemy enemy) && !hasDealtDamage.Contains(hit.transform.gameObject))
  27.                 {
  28.                     enemy.TakeDamage(weaponDamage);
  29.                     enemy.HitVFX(hit.point);
  30.                     hasDealtDamage.Add(hit.transform.gameObject);
  31.                 }
  32.             }
  33.         }
  34.     }
  35.     public void StartDealDamage()
  36.     {
  37.         canDealDamage = true;
  38.         hasDealtDamage.Clear();
  39.     }
  40.     public void EndDealDamage()
  41.     {
  42.         canDealDamage = false;
  43.     }
  44.  
  45.     private void OnDrawGizmos()
  46.     {
  47.         Gizmos.color = Color.yellow;
  48.         Gizmos.DrawLine(transform.position, transform.position - transform.up * weaponLength);
  49.     }
  50. }
  51.  
Add Comment
Please, Sign In to add comment