Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class EnemyBase : MonoBehaviour
  6. {
  7.     public Animator animator;
  8.     public Transform shoot;
  9.     public ParticleSystem muzzleFlash;
  10.  
  11.     public float health = 100f;
  12.     public float damage = 10f;
  13.     public float range = 100f;
  14.     public float fireRate = 15f;
  15.  
  16.     private bool isDead = false;
  17.     private float nextTimeToFire = 0f;
  18.  
  19.     private void Update()
  20.     {
  21.         EShoot();
  22.     }
  23.     public void TakeDamgae(float amount)
  24.     {
  25.         if (health > 0)
  26.         {
  27.             health -= amount;
  28.         }
  29.         if (health <= 0)
  30.         {
  31.             GetComponent<Collider>().enabled = !GetComponent<Collider>().enabled;
  32.             isDead = true;
  33.             Anims();
  34.         }
  35.     }
  36.  
  37.     void Die()
  38.     {
  39.         Destroy(gameObject);
  40.     }
  41.  
  42.     void EShoot()
  43.     {
  44.         //Checks the time between shots and if the ai is dead
  45.         if (Time.time >= nextTimeToFire && !isDead)
  46.         {
  47.             nextTimeToFire = Time.time + 1f / fireRate;
  48.             RaycastHit hit;
  49.             Debug.DrawRay(transform.position, transform.forward * range, Color.green);
  50.             if (Physics.Raycast(shoot.transform.position, shoot.transform.forward, out hit, range))
  51.             {
  52.                 Debug.Log(hit.transform.name);
  53.  
  54.                 PlayerBase target = hit.transform.GetComponent<PlayerBase>();
  55.                 //if raycast hits player call TakeDamage function
  56.                 if (target != null)
  57.                 {
  58.                     muzzleFlash.Play();
  59.                     target.TakeDamage(damage);
  60.                 }
  61.             }
  62.         }
  63.     }
  64.  
  65.     void Anims()
  66.     {
  67.         if (isDead == true)
  68.         {
  69.             animator.SetTrigger("death");
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement