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
- {
- public Seek s;
- public Canvas C;
- public Transform PlayerTrail, Trail, Player;
- [HideInInspector]
- public Transform TrailStart;
- public Attacking Attk;
- public Rigidbody2D RB2D;
- public string Name;
- public Animator Anim;
- public float Speed;
- //[HideInInspector]
- public Vector2 Move;
- public bool CanMove, SpotedPlayer, PlayerisTrail;
- [HideInInspector]
- public bool followPlayer, canChangeDir;
- int faceDir;
- private void Start()
- {
- C.worldCamera = GameObject.Find("PlayerCam").GetComponent<Camera>();
- Player = GameObject.Find("PlayerCntrl").transform;
- canChangeDir = true;
- StartCoroutine("DoMove");
- Wait = 3;
- }
- void Update()
- {
- if (followPlayer)
- {
- CanMove = false;
- transform.position = Vector2.MoveTowards(transform.position, TrailStart.position, Speed * Time.deltaTime);
- if (TrailStart.transform.position.y < transform.position.y)
- {
- faceDir = 0;
- }
- else
- {
- faceDir = 1;
- }
- if (faceDir == 3)
- {
- WalkLeft();
- }
- else
- {
- if (faceDir == 2)
- {
- WalkRight();
- }
- else
- {
- if (faceDir == 1)
- {
- WalkUp();
- }
- else
- {
- WalkDown();
- }
- }
- }
- }
- else
- {
- if (canChangeDir)
- {
- RB2D.AddForce(Move, ForceMode2D.Force);
- CanMove = true;
- }
- }
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- if (SpotedPlayer && collision.transform.tag == "wall")
- {
- SpotedPlayer = false;
- followPlayer = false;
- s.GetComponent<BoxCollider2D>().size = new Vector2(5, 5);
- s.GetComponent<BoxCollider2D>().enabled = true;
- Wait = 3;
- }
- }
- int MoveDir, Chance;
- public int Wait;
- public IEnumerator DoMove()
- {
- while (true)
- {
- yield return new WaitForSeconds(Wait);
- if (CanMove && canChangeDir)
- {
- Chance = Random.Range(0, 4);
- if (Chance == 2)
- {
- MoveDir = Random.Range(0, 5);
- if (MoveDir == 1)
- {
- //Move Up
- Move.y = Speed;
- WalkUp();
- }
- else
- {
- if (MoveDir == 2)
- {
- //Move Down
- Move.y = -Speed;
- WalkDown();
- }
- else
- {
- if (MoveDir == 3)
- {
- //Move Left
- Move.x = -Speed;
- WalkLeft();
- }
- else
- {
- if (MoveDir == 4)
- {
- //Move Right
- Move.x = Speed;
- WalkRight();
- }
- else
- {
- //Idle
- Move.x = 0;
- Move.y = 0;
- }
- }
- }
- }
- }
- }
- else
- {
- if (!PlayerisTrail)
- {
- var newTrail = GameObject.Instantiate(Trail, Player.transform.position, Player.transform.rotation);
- TrailStart.transform.position = newTrail.transform.position;
- }
- else
- {
- //Player is near and I stick close to him, start attacking
- Chance = Random.Range(0, 2);
- if (Chance == 1)
- {
- if (faceDir == 1)
- {
- Attk.AttackUp();
- }
- else
- {
- Attk.AttackDown();
- }
- }
- }
- }
- }
- }
- void WalkLeft()
- {
- if (!Attk.isAttking)
- {
- Anim.Play(Name + "WalkLeft");
- }
- }
- void WalkRight()
- {
- if (!Attk.isAttking)
- {
- Anim.Play(Name + "WalkRight");
- }
- }
- void WalkUp()
- {
- if (!Attk.isAttking)
- {
- Anim.Play(Name + "WalkUp");
- }
- }
- void WalkDown()
- {
- if (!Attk.isAttking)
- {
- Anim.Play(Name + "WalkDown");
- }
- }
- void IdleDown()
- {
- StopAttk();
- if (Move.x == 0 && Move.y == 0)
- {
- Anim.Play(Name + "IdleDown");
- }
- }
- void IdleUp()
- {
- StopAttk();
- if (Move.x == 0 && Move.y == 0)
- {
- Anim.Play(Name + "IdleUp");
- }
- }
- void IdleLeft()
- {
- StopAttk();
- if (Move.x == 0 && Move.y == 0)
- {
- Anim.Play(Name + "IdleLeft");
- }
- }
- void IdleRight()
- {
- StopAttk();
- if (Move.x == 0 && Move.y == 0)
- {
- Anim.Play(Name + "IdleRight");
- }
- }
- public void StopAttk()
- {
- Attk.isAttking = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement