Advertisement
Salasander

MeleeEnemyAI

May 24th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class StandInScript : MonoBehaviour
  6. {
  7.  
  8.     /// <summary>
  9.     /// THERE ARE TWO PARTS FOR THIS SCRIPT. You need to have another gameobject with the script "PlayerCheck" that has a collider to your choice of size. MAKE SURE THE COLLIDER IS A TRIGGER AND 2D. MAKE SURE THE PLAYER IS TAGGED AS PLAYER AS WELL!!!
  10.     /// This script roams randomly every x seconds you set inside of the inspector and then when you get in range
  11.     /// of the enemy, it'll chase the player.
  12.     /// </summary>
  13.  
  14.     public enum Phase
  15.     {
  16.         FREEROAM,
  17.         ATTACK
  18.     }
  19.  
  20.     public Phase currentPhase;
  21.     public Rigidbody2D rb;
  22.     private GameObject player;
  23.     [HideInInspector] public bool playerInRange;
  24.  
  25.     //Free Roam Variables
  26.     public float freeRoamSpeed;
  27.     public float timeBetweenMove;
  28.     public float timeToMove;
  29.     private float timeBetweenMoveCounter;
  30.     private float timeToMoveCounter;
  31.     private bool canFreeRoam;
  32.     private Vector3 moveDirection;
  33.  
  34.     //Atac Variables
  35.     public GameObject target;
  36.     public float combatMoveSpeed;
  37.  
  38.     // Start is called before the first frame update
  39.     void Start()
  40.     {
  41.         if(!GameObject.Find("Player"))
  42.         {
  43.             Debug.LogWarning("No Game Objects Named 'Player' In The Scene!");
  44.         }
  45.  
  46.         if(GameObject.Find("Player"))
  47.             player = GameObject.Find("Player");
  48.  
  49.         if(gameObject.GetComponent<Rigidbody2D>() == null)
  50.         {
  51.             Debug.LogWarning("Game Object Doesn't Have a Rigidbody2D!");
  52.         }
  53.  
  54.         if(gameObject.GetComponent<Rigidbody2D>() != null)
  55.         {
  56.             rb = gameObject.GetComponent<Rigidbody2D>();
  57.         }
  58.     }
  59.  
  60.     // Update is called once per frame
  61.     void Update()
  62.     {
  63.         switch(currentPhase)
  64.         {
  65.             case Phase.ATTACK:
  66.                 HandleAttack();
  67.                 break;
  68.  
  69.             case Phase.FREEROAM:
  70.                 HandleFreeroam();
  71.                 break;
  72.         }
  73.     }
  74.  
  75.     void HandleAttack()
  76.     {
  77.         if (target != null)
  78.         {
  79.             transform.rotation = Quaternion.LookRotation(Vector3.forward, target.transform.position - transform.position);
  80.             transform.position = Vector2.MoveTowards(transform.position, target.transform.position, combatMoveSpeed);
  81.         }
  82.     }
  83.  
  84.     void HandleFreeroam()
  85.     {
  86.         //If the enemy can free roam...
  87.         if (canFreeRoam)
  88.         {
  89.             //Start the counter...
  90.             timeToMoveCounter -= Time.deltaTime;
  91.  
  92.             //Apply movement to the rigidbody [X]
  93.             rb.velocity = moveDirection;
  94.  
  95.             //If the time to move has been spent then...
  96.             if (timeToMoveCounter <= 0f)
  97.             {
  98.                 //Enemy can't free roam anymore
  99.                 canFreeRoam = false;
  100.  
  101.                 timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeToMove * 1.25f);
  102.             }
  103.         }
  104.  
  105.         if (!canFreeRoam)
  106.         {
  107.             rb.velocity = Vector3.zero;
  108.             timeBetweenMoveCounter -= Time.deltaTime;
  109.             if (timeBetweenMoveCounter <= 0f)
  110.             {
  111.                 //Enemy can roam
  112.                 canFreeRoam = true;
  113.                 //Reset the time between move counter
  114.                 timeBetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
  115.  
  116.                 //Apply movement to move direction [X]
  117.                 moveDirection = new Vector3(Random.Range(-1f, 1f) * freeRoamSpeed, Random.Range(-1f, 1f) * freeRoamSpeed, 0);
  118.             }
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement