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 AICntrl : MonoBehaviour
- {
- //[HideInInspector]
- public GameObject Player;
- public Movement MyMovement;
- string Move;
- int MoveDir;
- //[HideInInspector]
- public bool isMoving, Turn, AIAttking, Chasing, OnSameAxis;
- Vector2 PlayerPos;
- private void Start()
- {
- Player = GameObject.Find("PlayerCntrl");
- CheckForPlayer();
- StartCoroutine("DoMove");
- }
- private void Update()
- {
- if (isMoving)
- {
- MyMovement.Invoke(Move, 0);
- if(Chasing)
- {
- if(Mathf.Round(Player.transform.position.x) == Mathf.Round(transform.position.x))
- {
- OnSameAxis = true;
- //The player is on my X axis, either below me or above me
- if (Mathf.Round(Player.transform.position.y) > Mathf.Round(transform.position.y))
- {
- //Player is above me
- Move = "MoveUp";
- isMoving = true;
- }
- else
- {
- //Player is below me
- Move = "MoveDown";
- isMoving = true;
- }
- }
- else
- {
- if (Mathf.Round(Player.transform.position.y) == Mathf.Round(transform.position.y))
- {
- OnSameAxis = true;
- //Player is either to the left of me or to the right of me
- if (Mathf.Round(Player.transform.position.x) > Mathf.Round(transform.position.x))
- {
- //Player is to the right of me
- Move = "MoveRight";
- isMoving = true;
- }
- else
- {
- //Player is to the left of me
- Move = "MoveLeft";
- isMoving = true;
- }
- }
- else
- {
- OnSameAxis = false;
- }
- }
- }
- }
- else
- {
- Vector2 Round = new Vector2(Mathf.Round(transform.position.x), Mathf.Round(transform.position.y));
- transform.position = Vector2.Lerp(transform.position, Round, 4 * Time.deltaTime);
- }
- if (MyMovement.SomethingInTheWay)
- {
- if (MyMovement.TagOfObject == "Untagged" | MyMovement.TagOfObject == "Enemy")
- {
- Debug.Log("SomethingInTheWay");
- OnSameAxis = false;
- Chasing = false;
- isMoving = false;
- RandomMove();
- }
- }
- }
- public IEnumerator DoMove()
- {
- while (true)
- {
- yield return new WaitForSeconds(2f);
- MyMovement.SomethingInTheWay = false;
- isMoving = false;
- if (!Chasing)
- {
- CheckForPlayer();
- }
- else
- {
- if(!OnSameAxis)
- {
- Debug.Log("MovingTowardsPlayerVeryBadly");
- if (Mathf.Round(Player.transform.position.x) == Mathf.Round(transform.position.x))
- {
- Debug.Log("OnSameX");
- //The player is on my X axis, either below me or above me
- if (Mathf.Round(Player.transform.position.y) > Mathf.Round(transform.position.y))
- {
- //Player is above me
- Move = "MoveUp";
- isMoving = true;
- }
- else
- {
- //Player is below me
- Move = "MoveDown";
- isMoving = true;
- }
- }
- else
- {
- if (Mathf.Round(Player.transform.position.y) == Mathf.Round(transform.position.y))
- {
- Debug.Log("OnSameY");
- //Player is either to the left of me or to the right of me
- if (Mathf.Round(Player.transform.position.x) > Mathf.Round(transform.position.x))
- {
- //Player is to the right of me
- Move = "MoveRight";
- isMoving = true;
- }
- else
- {
- //Player is to the left of me
- Move = "MoveLeft";
- isMoving = true;
- }
- }
- else
- {
- //The player is not on any of my 4 axis, fuck
- if (Mathf.Round(Player.transform.position.x) > Mathf.Round(Player.transform.position.y))
- {
- //The players X axis is greater than the y axis
- //So move the AI along the X axis?
- if (Mathf.Round(Player.transform.position.x) > Mathf.Round(transform.position.x))
- {
- Debug.Log("Move AI to the Right?");
- Move = "MoveRight";
- isMoving = true;
- }
- else
- {
- Debug.Log("Move AI to the left?");
- Move = "MoveLeft";
- isMoving = true;
- }
- }
- else
- {
- //The players X axis is lesser than the Y axis
- //So Move the AI along the Y axis?
- if (Mathf.Round(Player.transform.position.y) > Mathf.Round(transform.position.y))
- {
- Debug.Log("Move AI up?");
- Move = "MoveUp";
- isMoving = true;
- }
- else
- {
- Debug.Log("Move AI down?");
- Move = "MoveDown";
- isMoving = true;
- }
- }
- }
- }
- }
- }
- }
- }
- void RandomMove()
- {
- MoveDir = Random.Range(0, 5);
- OnSameAxis = false;
- Chasing = false;
- if (MoveDir == 0)
- {
- Move = "MoveDown";
- isMoving = true;
- }
- else
- {
- if (MoveDir == 1)
- {
- Move = "MoveLeft";
- isMoving = true;
- }
- else
- {
- if (MoveDir == 2)
- {
- Move = "MoveRight";
- isMoving = true;
- }
- else
- {
- if (MoveDir == 3)
- {
- Move = "MoveUp";
- isMoving = true;
- }
- else
- {
- isMoving = false;
- RandomMove();
- }
- }
- }
- }
- }
- public float Dist;
- void CheckForPlayer()
- {
- Dist = Vector2.Distance(transform.position, Player.transform.position);
- if (Dist <= 4)
- {
- Chasing = true;
- if (Mathf.Round(Player.transform.position.x) == Mathf.Round(transform.position.x))
- {
- //The player is on my X axis, either below me or above me
- if (Mathf.Round(Player.transform.position.y) > Mathf.Round(transform.position.y))
- {
- //Player is above me
- Move = "MoveUp";
- isMoving = true;
- }
- else
- {
- //Player is below me
- Move = "MoveDown";
- isMoving = true;
- }
- }
- else
- {
- if (Mathf.Round(Player.transform.position.y) == Mathf.Round(transform.position.y))
- {
- //Player is either to the left of me or to the right of me
- if (Mathf.Round(Player.transform.position.x) > Mathf.Round(transform.position.x))
- {
- //Player is to the right of me
- Move = "MoveRight";
- isMoving = true;
- }
- else
- {
- //Player is to the left of me
- Move = "MoveLeft";
- isMoving = true;
- }
- }
- }
- }
- else
- {
- //Player is not near me
- Turn = !Turn;
- if (Turn && !AIAttking)
- {
- RandomMove();
- }
- else
- {
- isMoving = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement