Advertisement
Guest User

Untitled

a guest
Nov 25th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | Software | 0 0
  1. public class Fighter : MonoBehaviour, IAction
  2.     {
  3.         [SerializeField] float weaponRange = 2f;
  4.         [SerializeField] Transform target;
  5.         [SerializeField] ParticleSystem muzzleFlash;
  6.         [SerializeField] float range;
  7.         [SerializeField] float damage;
  8.         [SerializeField] GameObject hitEffect;
  9.         [SerializeField] AudioClip shooting;
  10.         Coroutine trigger;
  11.         bool check;
  12.         public Health targetPlayer;
  13.         Health health;
  14.  
  15.         void Start()
  16.         {
  17.             health = GetComponent<Health>();
  18.             check = false;
  19.             Debug.DrawRay(this.transform.position, this.transform.forward, Color.red, 30);
  20.         }
  21.  
  22.         void Update()
  23.         {
  24.  
  25.             if (target == null) return;
  26.  
  27.             if (!GetIsInRange())
  28.             {
  29.                 GetComponent<Mover>().MoveTo(target.position);
  30.             }
  31.  
  32.             else
  33.             {
  34.                 GetComponent<Mover>().Cancel();
  35.                 if (!health.IsDead())
  36.                 {
  37.                     AttackBehaviour();
  38.                 }
  39.  
  40.             }
  41.  
  42.  
  43.         }
  44.  
  45.  
  46.         private bool GetIsInRange()
  47.         {
  48.  
  49.             return Vector3.Distance(transform.position, target.position) < weaponRange;
  50.         }
  51.  
  52.         public bool CanAttack(GameObject combatTarget)
  53.         {
  54.  
  55.             if (combatTarget == null) { return false; }
  56.             targetPlayer = combatTarget.GetComponent<Health>();
  57.             return targetPlayer != null && !targetPlayer.IsDead();
  58.         }
  59.  
  60.         private void AttackBehaviour()
  61.         {
  62.             transform.LookAt(target.transform);
  63.  
  64.             if (trigger == null)
  65.             {
  66.                 trigger = StartCoroutine(TriggerAttack());
  67.             }
  68.  
  69.  
  70.         }
  71.  
  72.         IEnumerator TriggerAttack()
  73.         {
  74.             do
  75.             {
  76.  
  77.                 yield return new WaitForSeconds(1);
  78.                 PlayMuzzleFlash();
  79.                 Process();
  80.  
  81.             } while (GetIsInRange());
  82.         }
  83.  
  84.  
  85.         private void PlayMuzzleFlash()
  86.         {
  87.             muzzleFlash.Play();
  88.             AudioSource.PlayClipAtPoint(shooting, Camera.main.transform.position);
  89.         }
  90.  
  91.         private void Process()
  92.         {
  93.             RaycastHit hit;
  94.             if (Physics.Raycast(this.transform.position, this.transform.forward, out hit, range))
  95.             {
  96.                 Debug.Log("I hit this thing: " + hit.transform.name);
  97.  
  98.                 targetPlayer = hit.transform.GetComponent<Health>();
  99.                 if (targetPlayer == null) return;
  100.                 targetPlayer.PlayerTakeDamage(damage);
  101.             }
  102.  
  103.             else
  104.  
  105.             {
  106.                 return;
  107.             }
  108.         }
  109.  
  110.  
  111.         public void Attack(GameObject combatTarget)
  112.         {
  113.             GetComponent<ActionSchedular>().StartAction(this);
  114.             target = combatTarget.transform;
  115.         }
  116.  
  117.  
  118.         public void Cancel()
  119.         {
  120.             target = null;
  121.         }
  122.  
  123.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement