Advertisement
renderbydavid

AICntrl

Oct 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.33 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AICntrl : MonoBehaviour
  6. {
  7.     public Seek s;
  8.     public Canvas C;
  9.     public Transform PlayerTrail, Trail, Player;
  10.     [HideInInspector]
  11.     public Transform TrailStart;
  12.     public Attacking Attk;
  13.     public Rigidbody2D RB2D;
  14.     public string Name;
  15.     public Animator Anim;
  16.     public float Speed;
  17.     //[HideInInspector]
  18.     public Vector2 Move;
  19.     public bool CanMove, SpotedPlayer, PlayerisTrail;
  20.     [HideInInspector]
  21.     public bool followPlayer, canChangeDir;
  22.     int faceDir;
  23.     private void Start()
  24.     {
  25.         C.worldCamera = GameObject.Find("PlayerCam").GetComponent<Camera>();
  26.         Player = GameObject.Find("PlayerCntrl").transform;
  27.         canChangeDir = true;
  28.         StartCoroutine("DoMove");
  29.         Wait = 3;
  30.     }
  31.  
  32.     void Update()
  33.     {
  34.  
  35.         if (followPlayer)
  36.         {
  37.             CanMove = false;
  38.             transform.position = Vector2.MoveTowards(transform.position, TrailStart.position, Speed * Time.deltaTime);
  39.             if (TrailStart.transform.position.y < transform.position.y)
  40.             {
  41.                 faceDir = 0;
  42.             }
  43.             else
  44.             {
  45.                 faceDir = 1;
  46.             }
  47.             if (faceDir == 3)
  48.             {
  49.                 WalkLeft();
  50.             }
  51.             else
  52.             {
  53.                 if (faceDir == 2)
  54.                 {
  55.                     WalkRight();
  56.                 }
  57.                 else
  58.                 {
  59.                     if (faceDir == 1)
  60.                     {
  61.                         WalkUp();
  62.                     }
  63.                     else
  64.                     {
  65.                         WalkDown();
  66.                     }
  67.                 }
  68.             }
  69.  
  70.         }
  71.         else
  72.         {
  73.             if (canChangeDir)
  74.             {
  75.                 RB2D.AddForce(Move, ForceMode2D.Force);
  76.                 CanMove = true;
  77.             }
  78.  
  79.         }
  80.     }
  81.     private void OnCollisionEnter2D(Collision2D collision)
  82.     {
  83.         if (SpotedPlayer && collision.transform.tag == "wall")
  84.         {
  85.             SpotedPlayer = false;
  86.             followPlayer = false;
  87.             s.GetComponent<BoxCollider2D>().size = new Vector2(5, 5);
  88.             s.GetComponent<BoxCollider2D>().enabled = true;
  89.             Wait = 3;
  90.         }
  91.  
  92.     }
  93.  
  94.     int MoveDir, Chance;
  95.     public int Wait;
  96.  
  97.     public IEnumerator DoMove()
  98.     {
  99.         while (true)
  100.         {
  101.             yield return new WaitForSeconds(Wait);
  102.             if (CanMove && canChangeDir)
  103.             {
  104.                 Chance = Random.Range(0, 4);
  105.                 if (Chance == 2)
  106.                 {
  107.                     MoveDir = Random.Range(0, 5);
  108.                     if (MoveDir == 1)
  109.                     {
  110.                         //Move Up
  111.                         Move.y = Speed;
  112.                         WalkUp();
  113.                     }
  114.                     else
  115.                     {
  116.                         if (MoveDir == 2)
  117.                         {
  118.                             //Move Down
  119.                             Move.y = -Speed;
  120.                             WalkDown();
  121.                         }
  122.                         else
  123.                         {
  124.                             if (MoveDir == 3)
  125.                             {
  126.                                 //Move Left
  127.                                 Move.x = -Speed;
  128.                                 WalkLeft();
  129.                             }
  130.                             else
  131.                             {
  132.                                 if (MoveDir == 4)
  133.                                 {
  134.                                     //Move Right
  135.                                     Move.x = Speed;
  136.                                     WalkRight();
  137.                                 }
  138.                                 else
  139.                                 {
  140.                                     //Idle
  141.                                     Move.x = 0;
  142.                                     Move.y = 0;
  143.                                 }
  144.                             }
  145.                         }
  146.                     }
  147.                 }
  148.  
  149.             }
  150.             else
  151.             {
  152.                 if (!PlayerisTrail)
  153.                 {
  154.                     var newTrail = GameObject.Instantiate(Trail, Player.transform.position, Player.transform.rotation);
  155.                     TrailStart.transform.position = newTrail.transform.position;
  156.                 }
  157.                 else
  158.                 {
  159.                     //Player is near and I stick close to him, start attacking
  160.                     Chance = Random.Range(0, 2);
  161.                     if (Chance == 1)
  162.                     {
  163.                         if (faceDir == 1)
  164.                         {
  165.                             Attk.AttackUp();
  166.                         }
  167.                         else
  168.                         {
  169.                             Attk.AttackDown();
  170.                         }
  171.                     }
  172.  
  173.                 }
  174.  
  175.             }
  176.         }
  177.     }
  178.  
  179.     void WalkLeft()
  180.     {
  181.         if (!Attk.isAttking)
  182.         {
  183.             Anim.Play(Name + "WalkLeft");
  184.         }
  185.  
  186.     }
  187.  
  188.     void WalkRight()
  189.     {
  190.         if (!Attk.isAttking)
  191.         {
  192.             Anim.Play(Name + "WalkRight");
  193.         }
  194.  
  195.     }
  196.  
  197.     void WalkUp()
  198.     {
  199.         if (!Attk.isAttking)
  200.         {
  201.             Anim.Play(Name + "WalkUp");
  202.         }
  203.  
  204.     }
  205.  
  206.     void WalkDown()
  207.     {
  208.         if (!Attk.isAttking)
  209.         {
  210.             Anim.Play(Name + "WalkDown");
  211.         }
  212.  
  213.     }
  214.  
  215.     void IdleDown()
  216.     {
  217.         StopAttk();
  218.         if (Move.x == 0 && Move.y == 0)
  219.         {
  220.             Anim.Play(Name + "IdleDown");
  221.         }
  222.     }
  223.  
  224.     void IdleUp()
  225.     {
  226.         StopAttk();
  227.         if (Move.x == 0 && Move.y == 0)
  228.         {
  229.             Anim.Play(Name + "IdleUp");
  230.         }
  231.     }
  232.  
  233.     void IdleLeft()
  234.     {
  235.         StopAttk();
  236.         if (Move.x == 0 && Move.y == 0)
  237.         {
  238.             Anim.Play(Name + "IdleLeft");
  239.         }
  240.     }
  241.  
  242.     void IdleRight()
  243.     {
  244.         StopAttk();
  245.         if (Move.x == 0 && Move.y == 0)
  246.         {
  247.             Anim.Play(Name + "IdleRight");
  248.         }
  249.     }
  250.  
  251.     public void StopAttk()
  252.     {
  253.         Attk.isAttking = false;
  254.     }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement