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 BulletEnemy : MonoBehaviour
- {
- TroopsHealth currentHealth;
- TroopsHealth realDamage;
- private Transform target;
- public float speed = 70f;
- public int damage = 50;
- public float armor = 1;
- public float explosionRadius = 0f;
- public GameObject impactEffect;
- public void Seek(Transform _target)
- {
- target = _target;
- }
- void Update()
- {
- if (target == null)
- {
- Destroy(gameObject);
- return;
- }
- Vector3 dir = target.position - transform.position;
- float distanceThisFrame = speed * Time.deltaTime;
- if (dir.magnitude <= distanceThisFrame)
- {
- HitTarget();
- return;
- }
- transform.Translate(dir.normalized * distanceThisFrame, Space.World);
- transform.LookAt(target);
- }
- void HitTarget()
- {
- GameObject effectIns = (GameObject)Instantiate(impactEffect, transform.position, transform.rotation);
- Destroy(effectIns, 2f);
- if (explosionRadius > 0f)
- {
- Explode();
- }
- else
- {
- Damage(target);
- }
- Destroy(gameObject);
- }
- void Explode()
- {
- Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius);
- foreach (Collider collider in colliders)
- {
- if (collider.tag == "Troops")
- {
- Damage(collider.transform);
- }
- }
- }
- void Damage(Transform troops)
- {
- TroopsHealth e = troops.GetComponent<TroopsHealth>();
- if (e != null)
- {
- e.TakeDamage(damage);
- }
- }
- public void TakeDamage(float amount)
- {
- var realDamage = amount - amount * .03f * armor;
- realDamage = Mathf.Max(1, realDamage);
- currentHealth -= realDamage;
- }
- void OnDrawGizmosSelected()
- {
- Gizmos.color = Color.red;
- Gizmos.DrawWireSphere(transform.position, explosionRadius);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement