Advertisement
renderbydavid

AICntrl

Oct 22nd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.08 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AICntrl : MonoBehaviour
  6. {
  7.     //[HideInInspector]
  8.     public GameObject Player;
  9.     public Movement MyMovement;
  10.     string Move;
  11.     int MoveDir;
  12.     //[HideInInspector]
  13.     public bool isMoving, Turn, AIAttking, Chasing, OnSameAxis;
  14.     Vector2 PlayerPos;
  15.     private void Start()
  16.     {
  17.         Player = GameObject.Find("PlayerCntrl");
  18.         CheckForPlayer();
  19.         StartCoroutine("DoMove");
  20.     }
  21.  
  22.     private void Update()
  23.     {
  24.         if (isMoving)
  25.         {
  26.             MyMovement.Invoke(Move, 0);
  27.             if(Chasing)
  28.             {
  29.                 if(Mathf.Round(Player.transform.position.x) == Mathf.Round(transform.position.x))
  30.                 {
  31.                     OnSameAxis = true;
  32.                     //The player is on my X axis, either below me or above me
  33.                     if (Mathf.Round(Player.transform.position.y) > Mathf.Round(transform.position.y))
  34.                     {
  35.                         //Player is above me
  36.                         Move = "MoveUp";
  37.                         isMoving = true;
  38.                     }
  39.                     else
  40.                     {
  41.                         //Player is below me
  42.                         Move = "MoveDown";
  43.                         isMoving = true;
  44.                     }
  45.  
  46.                 }
  47.                 else
  48.                 {
  49.                    
  50.                     if (Mathf.Round(Player.transform.position.y) == Mathf.Round(transform.position.y))
  51.                     {
  52.                         OnSameAxis = true;
  53.                         //Player is either to the left of me or to the right of me
  54.                         if (Mathf.Round(Player.transform.position.x) > Mathf.Round(transform.position.x))
  55.                         {
  56.                             //Player is to the right of me
  57.                             Move = "MoveRight";
  58.                             isMoving = true;
  59.                         }
  60.                         else
  61.                         {
  62.                             //Player is to the left of me
  63.                             Move = "MoveLeft";
  64.                             isMoving = true;
  65.                         }
  66.                     }
  67.                     else
  68.                     {
  69.                         OnSameAxis = false;
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.         else
  75.         {
  76.             Vector2 Round = new Vector2(Mathf.Round(transform.position.x), Mathf.Round(transform.position.y));
  77.             transform.position = Vector2.Lerp(transform.position, Round, 4 * Time.deltaTime);
  78.         }
  79.         if (MyMovement.SomethingInTheWay)
  80.         {
  81.             if (MyMovement.TagOfObject == "Untagged" | MyMovement.TagOfObject == "Enemy")
  82.             {
  83.                 Debug.Log("SomethingInTheWay");
  84.                 OnSameAxis = false;
  85.                 Chasing = false;
  86.                 isMoving = false;
  87.                 RandomMove();
  88.             }
  89.         }
  90.  
  91.     }
  92.  
  93.     public IEnumerator DoMove()
  94.     {
  95.         while (true)
  96.         {
  97.             yield return new WaitForSeconds(2f);
  98.             MyMovement.SomethingInTheWay = false;
  99.             isMoving = false;
  100.             if (!Chasing)
  101.             {
  102.                 CheckForPlayer();
  103.             }
  104.             else
  105.             {
  106.                 if(!OnSameAxis)
  107.                 {
  108.                     Debug.Log("MovingTowardsPlayerVeryBadly");
  109.                     if (Mathf.Round(Player.transform.position.x) == Mathf.Round(transform.position.x))
  110.                     {
  111.                         Debug.Log("OnSameX");
  112.                         //The player is on my X axis, either below me or above me
  113.                         if (Mathf.Round(Player.transform.position.y) > Mathf.Round(transform.position.y))
  114.                         {
  115.                             //Player is above me
  116.                             Move = "MoveUp";
  117.                             isMoving = true;
  118.                         }
  119.                         else
  120.                         {
  121.                             //Player is below me
  122.                             Move = "MoveDown";
  123.                             isMoving = true;
  124.                         }
  125.                     }
  126.                     else
  127.                     {
  128.                         if (Mathf.Round(Player.transform.position.y) == Mathf.Round(transform.position.y))
  129.                         {
  130.                             Debug.Log("OnSameY");
  131.                             //Player is either to the left of me or to the right of me
  132.                             if (Mathf.Round(Player.transform.position.x) > Mathf.Round(transform.position.x))
  133.                             {
  134.                                 //Player is to the right of me
  135.                                 Move = "MoveRight";
  136.                                 isMoving = true;
  137.                             }
  138.                             else
  139.                             {
  140.                                 //Player is to the left of me
  141.                                 Move = "MoveLeft";
  142.                                 isMoving = true;
  143.                             }
  144.                         }
  145.                         else
  146.                         {
  147.                             //The player is not on any of my 4 axis, fuck
  148.                             if (Mathf.Round(Player.transform.position.x) > Mathf.Round(Player.transform.position.y))
  149.                             {
  150.                                 //The players X axis is greater than the y axis
  151.                                 //So move the AI along the X axis?
  152.                                 if (Mathf.Round(Player.transform.position.x) > Mathf.Round(transform.position.x))
  153.                                 {
  154.                                     Debug.Log("Move AI to the  Right?");
  155.                                     Move = "MoveRight";
  156.                                     isMoving = true;
  157.                                 }
  158.                                 else
  159.                                 {
  160.                                     Debug.Log("Move AI to the left?");
  161.                                     Move = "MoveLeft";
  162.                                     isMoving = true;
  163.                                 }
  164.                             }
  165.                             else
  166.                             {
  167.                                 //The players X axis is lesser than the Y axis
  168.                                 //So Move the AI along the Y axis?
  169.                                 if (Mathf.Round(Player.transform.position.y) > Mathf.Round(transform.position.y))
  170.                                 {
  171.                                     Debug.Log("Move AI up?");
  172.                                     Move = "MoveUp";
  173.                                     isMoving = true;
  174.                                 }
  175.                                 else
  176.                                 {
  177.                                     Debug.Log("Move AI down?");
  178.                                     Move = "MoveDown";
  179.                                     isMoving = true;
  180.                                 }
  181.                             }
  182.                         }
  183.                     }
  184.                 }
  185.                
  186.             }
  187.         }
  188.     }
  189.  
  190.     void RandomMove()
  191.     {
  192.         MoveDir = Random.Range(0, 5);
  193.         OnSameAxis = false;
  194.         Chasing = false;
  195.         if (MoveDir == 0)
  196.         {
  197.             Move = "MoveDown";
  198.             isMoving = true;
  199.         }
  200.         else
  201.         {
  202.             if (MoveDir == 1)
  203.             {
  204.                 Move = "MoveLeft";
  205.                 isMoving = true;
  206.             }
  207.             else
  208.             {
  209.                 if (MoveDir == 2)
  210.                 {
  211.                     Move = "MoveRight";
  212.                     isMoving = true;
  213.                 }
  214.                 else
  215.                 {
  216.                     if (MoveDir == 3)
  217.                     {
  218.                         Move = "MoveUp";
  219.                         isMoving = true;
  220.                     }
  221.                     else
  222.                     {
  223.                         isMoving = false;
  224.                         RandomMove();
  225.                     }
  226.                 }
  227.             }
  228.         }
  229.     }
  230.     public float Dist;
  231.     void CheckForPlayer()
  232.     {
  233.         Dist = Vector2.Distance(transform.position, Player.transform.position);
  234.         if (Dist <= 4)
  235.         {
  236.             Chasing = true;
  237.             if (Mathf.Round(Player.transform.position.x) == Mathf.Round(transform.position.x))
  238.             {
  239.                 //The player is on my X axis, either below me or above me
  240.                 if (Mathf.Round(Player.transform.position.y) > Mathf.Round(transform.position.y))
  241.                 {
  242.                     //Player is above me
  243.                     Move = "MoveUp";
  244.                     isMoving = true;
  245.                 }
  246.                 else
  247.                 {
  248.                     //Player is below me
  249.                     Move = "MoveDown";
  250.                     isMoving = true;
  251.                 }
  252.             }
  253.             else
  254.             {
  255.                 if (Mathf.Round(Player.transform.position.y) == Mathf.Round(transform.position.y))
  256.                 {
  257.                     //Player is either to the left of me or to the right of me
  258.                     if (Mathf.Round(Player.transform.position.x) > Mathf.Round(transform.position.x))
  259.                     {
  260.                         //Player is to the right of me
  261.                         Move = "MoveRight";
  262.                         isMoving = true;
  263.                     }
  264.                     else
  265.                     {
  266.                         //Player is to the left of me
  267.                         Move = "MoveLeft";
  268.                         isMoving = true;
  269.                     }
  270.                 }
  271.             }
  272.         }
  273.         else
  274.         {
  275.             //Player is not near me
  276.             Turn = !Turn;
  277.             if (Turn && !AIAttking)
  278.             {
  279.                 RandomMove();
  280.             }
  281.             else
  282.             {
  283.                 isMoving = false;
  284.             }
  285.         }
  286.     }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement