JojikYT

Enemy

May 15th, 2022
4,388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6. public class Enemy : MonoBehaviour
  7. {
  8.     [SerializeField] float health = 3;
  9.     [SerializeField] GameObject hitVFX;
  10.     [SerializeField] GameObject ragdoll;
  11.  
  12.     [Header("Combat")]
  13.     [SerializeField] float attackCD = 3f;
  14.     [SerializeField] float attackRange = 1f;
  15.     [SerializeField] float aggroRange = 4f;
  16.  
  17.     GameObject player;
  18.     NavMeshAgent agent;
  19.     Animator animator;
  20.     float timePassed;
  21.     float newDestinationCD = 0.5f;
  22.  
  23.     void Start()
  24.     {
  25.         agent = GetComponent<NavMeshAgent>();
  26.         animator = GetComponent<Animator>();
  27.         player = GameObject.FindGameObjectWithTag("Player");
  28.     }
  29.  
  30.     // Update is called once per frame
  31.     void Update()
  32.     {
  33.         animator.SetFloat("speed", agent.velocity.magnitude / agent.speed);
  34.  
  35.         if (player == null)
  36.         {
  37.             return;
  38.         }
  39.  
  40.         if (timePassed >= attackCD)
  41.         {
  42.             if (Vector3.Distance(player.transform.position, transform.position) <= attackRange)
  43.             {
  44.                 animator.SetTrigger("attack");
  45.                 timePassed = 0;
  46.             }
  47.         }
  48.         timePassed += Time.deltaTime;
  49.  
  50.         if (newDestinationCD <= 0 && Vector3.Distance(player.transform.position, transform.position) <= aggroRange)
  51.         {
  52.             newDestinationCD = 0.5f;
  53.             agent.SetDestination(player.transform.position);
  54.         }
  55.         newDestinationCD -= Time.deltaTime;
  56.         transform.LookAt(player.transform);
  57.     }
  58.  
  59.     private void OnCollisionEnter(Collision collision)
  60.     {
  61.         if (collision.gameObject.CompareTag("Player"))
  62.         {
  63.             print(true);
  64.             player = collision.gameObject;
  65.         }
  66.     }
  67.  
  68.     void Die()
  69.     {
  70.         Instantiate(ragdoll, transform.position,transform.rotation);
  71.         Destroy(this.gameObject);
  72.     }
  73.  
  74.     public void TakeDamage(float damageAmount)
  75.     {
  76.         health -= damageAmount;
  77.         animator.SetTrigger("damage");
  78.         CameraShake.Instance.ShakeCamera(2f, 0.2f);
  79.  
  80.         if (health <= 0)
  81.         {
  82.             Die();
  83.         }
  84.     }
  85.     public void StartDealDamage()
  86.     {
  87.         GetComponentInChildren<EnemyDamageDealer>().StartDealDamage();
  88.     }
  89.     public void EndDealDamage()
  90.     {
  91.         GetComponentInChildren<EnemyDamageDealer>().EndDealDamage();
  92.     }
  93.  
  94.     public void HitVFX(Vector3 hitPosition)
  95.     {
  96.         GameObject hit = Instantiate(hitVFX, hitPosition, Quaternion.identity);
  97.         Destroy(hit, 3f);
  98.     }
  99.  
  100.     private void OnDrawGizmos()
  101.     {
  102.         Gizmos.color = Color.red;
  103.         Gizmos.DrawWireSphere(transform.position, attackRange);
  104.         Gizmos.color = Color.yellow;
  105.         Gizmos.DrawWireSphere(transform.position, aggroRange);
  106.     }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment