Advertisement
VynxDev

Enemy script.

Feb 22nd, 2021
2,325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6. [RequireComponent(typeof(NavMeshAgent))]
  7. public class Enemy : LivingEntity {
  8.  
  9.     public enum state { idle, chase, attack};
  10.  
  11.     private state currentState;
  12.     private NavMeshAgent pathFinder;
  13.     private Transform target;
  14.     private float attackThreshhold = .5f;
  15.     private float timeBetweenAttacks = 1;
  16.     private float attackTimer;
  17.     private float myCollisionRadius;
  18.     private float targetCollisionRadius;
  19.    
  20.  
  21.     // Start is called before the first frame update
  22.     protected override void Start() {
  23.        
  24.         base.Start();
  25.         currentState = state.chase;
  26.         pathFinder = GetComponent<NavMeshAgent>();
  27.         target = GameObject.FindGameObjectWithTag("Player").transform;
  28.         myCollisionRadius = GetComponent<CapsuleCollider>().radius;
  29.         targetCollisionRadius = GetComponent<CapsuleCollider>().radius;
  30.  
  31.  
  32.  
  33.         StartCoroutine(updatePath());
  34.     }
  35.  
  36.     // Update is called once per frame
  37.     void Update() {
  38.        
  39.         if(Time.time > attackTimer)
  40.         {
  41.             float sqrDistanceToTarget = (target.position - transform.position).sqrMagnitude;
  42.             if (sqrDistanceToTarget < Mathf.Pow(attackThreshhold + myCollisionRadius + targetCollisionRadius, 2)) {
  43.                 attackTimer = Time.time + timeBetweenAttacks;
  44.                 StartCoroutine(attack());
  45.             }
  46.         }
  47.     }
  48.  
  49.     IEnumerator attack() {
  50.  
  51.         currentState = state.attack;
  52.         pathFinder.enabled = false;
  53.  
  54.         Vector3 origin = transform.position;
  55.         Vector3 attackPos = target.position;
  56.         float percent = 0;
  57.         float attackSpeed = 3;
  58.  
  59.         while(percent <= 1) {
  60.  
  61.             percent += Time.deltaTime * attackSpeed;
  62.             float interpolation = (-Mathf.Pow(percent, 2) + percent) * 4;
  63.             transform.position = Vector3.Lerp(origin, attackPos, interpolation);
  64.  
  65.             yield return null;
  66.         }
  67.         currentState = state.chase;
  68.         pathFinder.enabled = true;
  69.     }
  70.  
  71.     IEnumerator updatePath() {
  72.        
  73.         float refreshRate = .25f;
  74.        
  75.         while(target != null) {
  76.             if(currentState == state.chase) {
  77.                 Vector3 dirToTarget = (target.position - transform.position).normalized;
  78.                 Vector3 targetPosition = target.position - dirToTarget * (myCollisionRadius + targetCollisionRadius/2);
  79.                 if (!dead) pathFinder.SetDestination(targetPosition);
  80.                 yield return new WaitForSeconds(refreshRate);
  81.             }
  82.  
  83.         }
  84.  
  85.     }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement