daily pastebin goal
46%
SHARE
TWEET

MeleeEnemyAI

Salasander Mar 25th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AlbatrossController : MonoBehaviour
  6. {
  7.     public enum Phase
  8.     {
  9.         FREEROAM,
  10.         ATTACK
  11.     }
  12.  
  13.     //General Variables
  14.     public Phase phase;
  15.     public bool canMove;
  16.     private Animator anim;
  17.     private Rigidbody2D myRigidbody;
  18.     private GameObject player;
  19.     [HideInInspector] public bool playerInRange;
  20.  
  21.     //Free Roam Variables
  22.     public float freeRoamSpeed;
  23.     public float timeBetweenMove;
  24.     public float timeToMove;
  25.     private float timeBetweenMoveCounter;
  26.     private float timeToMoveCounter;
  27.     private bool canFreeRoam;
  28.     private Vector3 moveDirection;
  29.  
  30.     //Atac Variables
  31.     public GameObject target;
  32.     public float combatMoveSpeed;
  33.  
  34.     private void Start()
  35.     {
  36.         anim = GetComponent<Animator>();
  37.         canFreeRoam = true;
  38.         myRigidbody = gameObject.GetComponent<Rigidbody2D>();
  39.         player = GameObject.Find("Player");
  40.         target = player;
  41.  
  42.         timeBetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
  43.         timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeToMove * 1.25f);
  44.     }
  45.  
  46.     private void Update()
  47.     {
  48.         switch(phase)
  49.         {
  50.             case Phase.FREEROAM:
  51.                 HandleFreeRoam();
  52.                 break;
  53.  
  54.             case Phase.ATTACK:
  55.                 HandleAttack();
  56.                 break;
  57.         }
  58.  
  59.         if(playerInRange)
  60.         {
  61.             phase = Phase.ATTACK;
  62.         }
  63.     }
  64.  
  65.     void HandleFreeRoam()
  66.     {
  67.         //If the enemy can free roam...
  68.         if (canFreeRoam)
  69.         {
  70.             //Start the counter...
  71.             timeToMoveCounter -= Time.deltaTime;
  72.             //Apply movement to the rigidbody [X]
  73.             myRigidbody.velocity = moveDirection;
  74.  
  75.             //If the time to move has been spent then...
  76.             if (timeToMoveCounter <= 0f)
  77.             {
  78.                 //Enemy can't free roam anymore
  79.                 canFreeRoam = false;
  80.  
  81.                 timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeToMove * 1.25f);
  82.             }
  83.         }
  84.  
  85.         if (!canFreeRoam)
  86.         {
  87.             myRigidbody.velocity = Vector3.zero;
  88.             timeBetweenMoveCounter -= Time.deltaTime;
  89.             if (timeBetweenMoveCounter <= 0f)
  90.             {
  91.                 //Enemy can roam
  92.                 canFreeRoam = true;
  93.                 //Reset the time between move counter
  94.                 timeBetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
  95.  
  96.                 //Apply movement to move direction [X]
  97.                 moveDirection = new Vector3(Random.Range(-1f, 1f) * freeRoamSpeed, Random.Range(-1f, 1f) * freeRoamSpeed, 0);
  98.             }
  99.         }
  100.     }
  101.  
  102.     void HandleAttack()
  103.     {
  104.         if (target != null)
  105.         {
  106.             SwordSwing();
  107.             transform.rotation = Quaternion.LookRotation(Vector3.forward, target.transform.position - transform.position);
  108.             if (canMove)
  109.             {
  110.                 transform.position = Vector2.MoveTowards(transform.position, target.transform.position, combatMoveSpeed);
  111.             }
  112.         }
  113.     }
  114.  
  115.     public void SwordSwing()
  116.     {
  117.         anim.SetBool("Swinging", true);
  118.     }
  119.  
  120.     public void StopSwing()
  121.     {
  122.         anim.SetBool("Swinging", false);
  123.     }
  124. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top