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 StandInScript : MonoBehaviour
- {
- /// <summary>
- /// 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!!!
- /// This script roams randomly every x seconds you set inside of the inspector and then when you get in range
- /// of the enemy, it'll chase the player.
- /// </summary>
- public enum Phase
- {
- FREEROAM,
- ATTACK
- }
- public Phase currentPhase;
- public Rigidbody2D rb;
- private GameObject player;
- [HideInInspector] public bool playerInRange;
- //Free Roam Variables
- public float freeRoamSpeed;
- public float timeBetweenMove;
- public float timeToMove;
- private float timeBetweenMoveCounter;
- private float timeToMoveCounter;
- private bool canFreeRoam;
- private Vector3 moveDirection;
- //Atac Variables
- public GameObject target;
- public float combatMoveSpeed;
- // Start is called before the first frame update
- void Start()
- {
- if(!GameObject.Find("Player"))
- {
- Debug.LogWarning("No Game Objects Named 'Player' In The Scene!");
- }
- if(GameObject.Find("Player"))
- player = GameObject.Find("Player");
- if(gameObject.GetComponent<Rigidbody2D>() == null)
- {
- Debug.LogWarning("Game Object Doesn't Have a Rigidbody2D!");
- }
- if(gameObject.GetComponent<Rigidbody2D>() != null)
- {
- rb = gameObject.GetComponent<Rigidbody2D>();
- }
- }
- // Update is called once per frame
- void Update()
- {
- switch(currentPhase)
- {
- case Phase.ATTACK:
- HandleAttack();
- break;
- case Phase.FREEROAM:
- HandleFreeroam();
- break;
- }
- }
- void HandleAttack()
- {
- if (target != null)
- {
- transform.rotation = Quaternion.LookRotation(Vector3.forward, target.transform.position - transform.position);
- transform.position = Vector2.MoveTowards(transform.position, target.transform.position, combatMoveSpeed);
- }
- }
- void HandleFreeroam()
- {
- //If the enemy can free roam...
- if (canFreeRoam)
- {
- //Start the counter...
- timeToMoveCounter -= Time.deltaTime;
- //Apply movement to the rigidbody [X]
- rb.velocity = moveDirection;
- //If the time to move has been spent then...
- if (timeToMoveCounter <= 0f)
- {
- //Enemy can't free roam anymore
- canFreeRoam = false;
- timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeToMove * 1.25f);
- }
- }
- if (!canFreeRoam)
- {
- rb.velocity = Vector3.zero;
- timeBetweenMoveCounter -= Time.deltaTime;
- if (timeBetweenMoveCounter <= 0f)
- {
- //Enemy can roam
- canFreeRoam = true;
- //Reset the time between move counter
- timeBetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
- //Apply movement to move direction [X]
- moveDirection = new Vector3(Random.Range(-1f, 1f) * freeRoamSpeed, Random.Range(-1f, 1f) * freeRoamSpeed, 0);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement