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 AIMovement : MonoBehaviour {
- public float MoveSpeed;
- public static bool IsDead;
- [Header("Walls")]
- public GameObject WallPrefab;
- public float wallWidth;
- public float wallDistance;
- private Collider wall;
- private Vector3 lastWallEnd;
- [Header("Difficulty")]
- public int Difficutly = 1;
- [Header("Decisions")]
- public bool canDecide;
- public bool hasDecided;
- public float delayNextDecision;
- private float _delayNextDecision;
- //Directions
- [Header("Col detection")]
- public LayerMask layerMask; //mask wich should get detected by rays
- private float VectorLengthForward, VectorLengthLeft, VectorLengthRight;
- private bool left, right, up, down, random;//facing directions
- private Vector3 forwardDirection;
- private Vector3 leftDirection;
- private Vector3 rightDirection;
- private Vector3 TransformEulerAngle;
- // Use this for initialization
- void Start() {
- //can decide at start
- canDecide = true;
- hasDecided = false;
- //AI is not dead yet
- IsDead = false;
- //Vectors to check if something is nearby the AI
- forwardDirection = transform.TransformDirection(Vector3.forward) * VectorLengthForward;
- leftDirection = transform.TransformDirection(Vector3.left) * VectorLengthLeft;
- rightDirection = transform.TransformDirection(Vector3.right) * VectorLengthRight;
- //get difficulty
- CheckDifficulty();
- //get direction
- GetDirection();
- //Counter for decision delay
- _delayNextDecision = delayNextDecision;
- //Start wall spawning
- SpawnWall();
- }
- // Update is called once per frame
- void Update() {
- if (!IsDead) {
- //allways get the direction
- GetDirection();
- delayNextDecision -= Time.deltaTime;
- if (delayNextDecision <= 0) {
- if (!random) {
- Decide();
- delayNextDecision = _delayNextDecision;
- } else {
- GoRandom();
- delayNextDecision = _delayNextDecision;
- }
- }
- //Basic forward movement
- transform.Translate(transform.forward * MoveSpeed * Time.smoothDeltaTime, Space.World);
- }
- }
- private void Decide() {
- //Spawn walls
- if (wall != null) {
- FitColliderBetweenWalls(wall, lastWallEnd, transform.position);
- canDecide = false;
- hasDecided = true;
- }
- //Debug.Log("I'm deciding..");
- RaycastHit hitFoward;
- //if facing left
- if (left) {
- //Debug.Log("Facing left");
- //if something is in front of AI while facing left
- if (Physics.Raycast(transform.position, forwardDirection, out hitFoward, VectorLengthForward, layerMask)) {
- if (hitFoward.collider.transform.name.StartsWith("Wall") || hitFoward.collider.transform.tag == "Border") {
- //Debug.Log("something in front go up or down");
- GoUpOrDown();
- } else {
- //if nothing is nearby move randomly
- random = true;
- }
- }
- }
- if (right) {
- //Debug.Log("Facing right");
- //if something is in front of AI while facing right
- if (Physics.Raycast(transform.position, forwardDirection, out hitFoward, VectorLengthForward, layerMask)) {
- if (hitFoward.collider.transform.name.StartsWith("Wall") || hitFoward.collider.transform.tag == "Border") {
- //Debug.Log("something in front go up or down");
- GoUpOrDown();
- } else {
- //if nothing is nearby move randomly
- random = true;
- }
- }
- }
- if (up) {
- // Debug.Log("Facing up");
- //if something is in front of AI while facing up
- if (Physics.Raycast(transform.position, forwardDirection, out hitFoward, VectorLengthForward, layerMask)) {
- if (hitFoward.collider.transform.name.StartsWith("Wall") || hitFoward.collider.transform.tag == "Border") {
- // Debug.Log("something in front go left up or right");
- GoLeftOrRight();
- } else {
- //if nothing is nearby move randomly
- random = true;
- }
- }
- }
- if (down) {
- //Debug.Log("Facing down");
- //if something is in front of AI while facing down
- if (Physics.Raycast(transform.position, forwardDirection, out hitFoward, VectorLengthForward, layerMask)) {
- if (hitFoward.collider.transform.name.StartsWith("Wall") || hitFoward.collider.transform.tag == "Border") {
- //Debug.Log("something in front go left or right");
- GoLeftOrRight();
- } else {
- //if nothing is nearby move randomly
- random = true;
- }
- }
- }
- }
- /*============================================================================================
- * DIRECTIONS
- * ============================================================================================*/
- // AI can move wherever it wants to, if nothing is in its range
- private void GoRandom() {
- int rand;
- //4 possible directions
- rand = Random.Range(0, 3);
- switch (rand) {
- case 0: GoLeft(); break;
- case 1: GoRight(); break;
- case 2: GoUp(); break;
- case 3: GoDown(); break;
- default: break;
- }
- random = false;
- }
- // Checks in which direction AI is facing
- private void GetDirection() {
- TransformEulerAngle = transform.eulerAngles;
- //Left
- if (TransformEulerAngle == new Vector3(0, -90, 0)) {
- right = false;
- up = false;
- down = false;
- left = true;
- }
- //Right
- if (TransformEulerAngle == new Vector3(0, 90, 0)) {
- down = false;
- up = false;
- left = false;
- right = true;
- }
- //Up
- if (TransformEulerAngle == new Vector3(0, 0, 0)) {
- down = false;
- left = false;
- right = false;
- up = true;
- }
- //Down
- if (TransformEulerAngle == new Vector3(0, 180, 0)) {
- up = false;
- left = false;
- right = false;
- down = true;
- }
- }
- // When AI is facing left or right
- private void GoUpOrDown() {
- //check if AI can go left or right
- RaycastHit hitLeft;
- RaycastHit hitRight;
- //if AI facing left or right
- //if something is on his left side
- //then go up
- if (Physics.Raycast(transform.position, leftDirection, out hitLeft, VectorLengthLeft, layerMask)) {
- if (hitLeft.collider.transform.name.StartsWith("Wall") || hitLeft.collider.transform.tag == "Border") {
- GoUp();
- }
- }
- //if AI facing left or right
- //if something is on his right side
- //then go down
- if (Physics.Raycast(transform.position, rightDirection, out hitRight, VectorLengthRight, layerMask)) {
- if (hitRight.collider.transform.name.StartsWith("Wall") || hitRight.collider.transform.tag == "Border") {
- GoDown();
- }
- }
- }
- // When AI is facing up or down
- private void GoLeftOrRight() {
- RaycastHit hitLeft;
- RaycastHit hitRight;
- //if something is on AIs left side
- if (Physics.Raycast(transform.position, leftDirection, out hitLeft, VectorLengthLeft, layerMask)) {
- if (hitLeft.collider.transform.name.StartsWith("Wall") || hitLeft.collider.transform.tag == "Border") {
- //turn right
- GoRight();
- }
- }
- //if something is on AIs right side
- if (Physics.Raycast(transform.position, rightDirection, out hitRight, VectorLengthRight, layerMask)) {
- if (hitRight.collider.transform.name.StartsWith("Wall") || hitLeft.collider.transform.tag == "Border") {
- //turn left
- GoLeft();
- }
- }
- }
- //All directions
- private void GoLeft() {
- transform.eulerAngles = new Vector3(0, -90, 0);
- SpawnWall();
- }
- private void GoRight() {
- transform.eulerAngles = new Vector3(0, 90, 0);
- SpawnWall();
- }
- private void GoUp() {
- transform.eulerAngles = new Vector3(0, 0, 0);
- SpawnWall();
- }
- private void GoDown() {
- transform.eulerAngles = new Vector3(0, 180, 0);
- SpawnWall();
- }
- /*============================================================================================
- * Walls
- * ============================================================================================*/
- private void FitColliderBetweenWalls(Collider _collider, Vector3 lastWallPos, Vector3 AIpos) {
- //center position
- _collider.transform.position = lastWallPos + (AIpos - lastWallPos) * 0.5f;
- //Scale it horizontal or vertical
- float dist = Vector3.Distance(lastWallPos, AIpos);
- if (lastWallPos.x != AIpos.x) {
- _collider.transform.localScale = new Vector3(dist + wallDistance, 1, wallWidth);
- } else {
- _collider.transform.localScale = new Vector3(wallWidth, 1, dist + wallDistance);
- }
- }
- private void SpawnWall() {
- //save last wall
- lastWallEnd = transform.position;
- // Spawn a new Lightwall
- GameObject g = (GameObject) Instantiate(WallPrefab, transform.position, Quaternion.identity);
- g.name = "WallAI";
- wall = g.GetComponent<Collider>();
- }
- //Show Vectors in unity
- void OnDrawGizmos() {
- CheckDifficulty();
- forwardDirection = transform.TransformDirection(Vector3.forward) * VectorLengthForward;
- leftDirection = transform.TransformDirection(Vector3.left) * VectorLengthLeft;
- rightDirection = transform.TransformDirection(Vector3.right) * VectorLengthRight;
- Gizmos.color = Color.yellow;
- Gizmos.DrawRay(transform.position, forwardDirection);
- Gizmos.DrawRay(transform.position, leftDirection);
- Gizmos.DrawRay(transform.position, rightDirection);
- }
- //Checks the AIs difficulty level
- private void CheckDifficulty() {
- switch (Difficutly) {
- case 1:
- VectorLengthForward = 10;
- VectorLengthLeft = 7;
- VectorLengthRight = 7;
- delayNextDecision = 0.3f;
- break;
- case 2:
- VectorLengthForward = 15;
- VectorLengthLeft = 10;
- VectorLengthRight = 10;
- delayNextDecision = 0.2f;
- break;
- case 3:
- VectorLengthForward = 20;
- VectorLengthLeft = 12;
- VectorLengthRight = 12;
- delayNextDecision = 0.1f;
- break;
- case 4:
- VectorLengthForward = 25;
- VectorLengthLeft = 14;
- VectorLengthRight = 14;
- delayNextDecision = 0.2f;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement