Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class EnemyBase : MonoBehaviour
- {
- public Animator animator;
- public Transform shoot;
- public ParticleSystem muzzleFlash;
- public float health = 100f;
- public float damage = 10f;
- public float range = 100f;
- public float fireRate = 15f;
- private bool isDead = false;
- private float nextTimeToFire = 0f;
- private void Update()
- {
- EShoot();
- }
- public void TakeDamgae(float amount)
- {
- if (health > 0)
- {
- health -= amount;
- }
- if (health <= 0)
- {
- GetComponent<Collider>().enabled = !GetComponent<Collider>().enabled;
- isDead = true;
- Anims();
- }
- }
- void Die()
- {
- Destroy(gameObject);
- }
- void EShoot()
- {
- //Checks the time between shots and if the ai is dead
- if (Time.time >= nextTimeToFire && !isDead)
- {
- nextTimeToFire = Time.time + 1f / fireRate;
- RaycastHit hit;
- Debug.DrawRay(transform.position, transform.forward * range, Color.green);
- if (Physics.Raycast(shoot.transform.position, shoot.transform.forward, out hit, range))
- {
- Debug.Log(hit.transform.name);
- PlayerBase target = hit.transform.GetComponent<PlayerBase>();
- //if raycast hits player call TakeDamage function
- if (target != null)
- {
- muzzleFlash.Play();
- target.TakeDamage(damage);
- }
- }
- }
- }
- void Anims()
- {
- if (isDead == true)
- {
- animator.SetTrigger("death");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement