JojikYT

EnemyDamageDealer

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