Advertisement
Guest User

Untitled

a guest
Nov 25th, 2018
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.71 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AIMovement : MonoBehaviour {
  6.  
  7.     public float MoveSpeed;
  8.     public static bool IsDead;
  9.     [Header("Walls")]
  10.     public GameObject WallPrefab;
  11.     public float wallWidth;
  12.     public float wallDistance;
  13.     private Collider wall;
  14.     private Vector3 lastWallEnd;
  15.     [Header("Difficulty")]
  16.     public int Difficutly = 1;
  17.     [Header("Decisions")]
  18.     public bool canDecide;
  19.     public bool hasDecided;
  20.     public float delayNextDecision;
  21.     private float _delayNextDecision;
  22.     //Directions
  23.     [Header("Col detection")]
  24.     public LayerMask layerMask; //mask wich should get detected by rays
  25.     private float VectorLengthForward, VectorLengthLeft, VectorLengthRight;
  26.     private bool left, right, up, down, random;//facing directions
  27.     private Vector3 forwardDirection;
  28.     private Vector3 leftDirection;
  29.     private Vector3 rightDirection;
  30.     private Vector3 TransformEulerAngle;
  31.  
  32.     // Use this for initialization
  33.     void Start() {
  34.         //can decide at start
  35.         canDecide = true;
  36.         hasDecided = false;
  37.         //AI is not dead yet
  38.         IsDead = false;
  39.         //Vectors to check if something is nearby the AI
  40.         forwardDirection = transform.TransformDirection(Vector3.forward) * VectorLengthForward;
  41.         leftDirection = transform.TransformDirection(Vector3.left) * VectorLengthLeft;
  42.         rightDirection = transform.TransformDirection(Vector3.right) * VectorLengthRight;
  43.  
  44.         //get difficulty
  45.         CheckDifficulty();
  46.         //get direction
  47.         GetDirection();
  48.         //Counter for decision delay
  49.         _delayNextDecision = delayNextDecision;
  50.         //Start wall spawning
  51.         SpawnWall();
  52.  
  53.     }
  54.  
  55.     // Update is called once per frame
  56.     void Update() {
  57.  
  58.         if (!IsDead) {
  59.             //allways get the direction
  60.             GetDirection();
  61.             delayNextDecision -= Time.deltaTime;
  62.             if (delayNextDecision <= 0) {
  63.                 if (!random) {
  64.                     Decide();
  65.                     delayNextDecision = _delayNextDecision;
  66.  
  67.                 } else {
  68.                     GoRandom();
  69.                     delayNextDecision = _delayNextDecision;
  70.                 }
  71.             }
  72.             //Basic forward movement
  73.             transform.Translate(transform.forward * MoveSpeed * Time.smoothDeltaTime, Space.World);
  74.         }
  75.     }
  76.  
  77.     private void Decide() {
  78.  
  79.         //Spawn walls
  80.         if (wall != null) {
  81.             FitColliderBetweenWalls(wall, lastWallEnd, transform.position);
  82.             canDecide = false;
  83.             hasDecided = true;
  84.         }
  85.         //Debug.Log("I'm deciding..");
  86.  
  87.         RaycastHit hitFoward;
  88.         //if facing left
  89.         if (left) {
  90.             //Debug.Log("Facing left");
  91.             //if something is in front of AI while facing left
  92.             if (Physics.Raycast(transform.position, forwardDirection, out hitFoward, VectorLengthForward, layerMask)) {
  93.                 if (hitFoward.collider.transform.name.StartsWith("Wall") || hitFoward.collider.transform.tag == "Border") {
  94.                     //Debug.Log("something in front go up or down");
  95.                     GoUpOrDown();
  96.                 } else {
  97.                     //if nothing is nearby move randomly
  98.                     random = true;
  99.                 }
  100.             }
  101.         }
  102.         if (right) {
  103.             //Debug.Log("Facing right");
  104.             //if something is in front of AI while facing right
  105.             if (Physics.Raycast(transform.position, forwardDirection, out hitFoward, VectorLengthForward, layerMask)) {
  106.                 if (hitFoward.collider.transform.name.StartsWith("Wall") || hitFoward.collider.transform.tag == "Border") {
  107.                     //Debug.Log("something in front go up or down");
  108.                     GoUpOrDown();
  109.                 } else {
  110.                     //if nothing is nearby move randomly
  111.                     random = true;
  112.                 }
  113.             }
  114.         }
  115.         if (up) {
  116.             //  Debug.Log("Facing up");
  117.             //if something is in front of AI while facing up
  118.             if (Physics.Raycast(transform.position, forwardDirection, out hitFoward, VectorLengthForward, layerMask)) {
  119.                 if (hitFoward.collider.transform.name.StartsWith("Wall") || hitFoward.collider.transform.tag == "Border") {
  120.                     //  Debug.Log("something in front go left up or right");
  121.                     GoLeftOrRight();
  122.                 } else {
  123.                     //if nothing is nearby move randomly
  124.                     random = true;
  125.                 }
  126.             }
  127.         }
  128.         if (down) {
  129.             //Debug.Log("Facing down");
  130.             //if something is in front of AI while facing down
  131.             if (Physics.Raycast(transform.position, forwardDirection, out hitFoward, VectorLengthForward, layerMask)) {
  132.                 if (hitFoward.collider.transform.name.StartsWith("Wall") || hitFoward.collider.transform.tag == "Border") {
  133.                     //Debug.Log("something in front go left or right");
  134.                     GoLeftOrRight();
  135.                 } else {
  136.                     //if nothing is nearby move randomly
  137.                     random = true;
  138.                 }
  139.             }
  140.         }
  141.     }
  142.     /*============================================================================================
  143.      * DIRECTIONS
  144.      * ============================================================================================*/
  145.  
  146.     // AI can move wherever it wants to, if nothing is in its range
  147.     private void GoRandom() {
  148.         int rand;
  149.         //4 possible directions
  150.         rand = Random.Range(0, 3);
  151.         switch (rand) {
  152.             case 0: GoLeft(); break;
  153.             case 1: GoRight(); break;
  154.             case 2: GoUp(); break;
  155.             case 3: GoDown(); break;
  156.             default: break;
  157.         }
  158.         random = false;
  159.     }
  160.     // Checks in which direction AI is facing
  161.     private void GetDirection() {
  162.         TransformEulerAngle = transform.eulerAngles;
  163.         //Left
  164.         if (TransformEulerAngle == new Vector3(0, -90, 0)) {
  165.             right = false;
  166.             up = false;
  167.             down = false;
  168.             left = true;
  169.         }
  170.         //Right
  171.         if (TransformEulerAngle == new Vector3(0, 90, 0)) {
  172.             down = false;
  173.             up = false;
  174.             left = false;
  175.             right = true;
  176.         }
  177.         //Up
  178.         if (TransformEulerAngle == new Vector3(0, 0, 0)) {
  179.             down = false;
  180.             left = false;
  181.             right = false;
  182.             up = true;
  183.         }
  184.         //Down
  185.         if (TransformEulerAngle == new Vector3(0, 180, 0)) {
  186.             up = false;
  187.             left = false;
  188.             right = false;
  189.             down = true;
  190.         }
  191.     }
  192.  
  193.     // When AI is facing left or right
  194.     private void GoUpOrDown() {
  195.         //check if AI can go left or right
  196.         RaycastHit hitLeft;
  197.         RaycastHit hitRight;
  198.  
  199.         //if AI facing left or right
  200.         //if something is on his left side
  201.         //then go up
  202.         if (Physics.Raycast(transform.position, leftDirection, out hitLeft, VectorLengthLeft, layerMask)) {
  203.             if (hitLeft.collider.transform.name.StartsWith("Wall") || hitLeft.collider.transform.tag == "Border") {
  204.  
  205.                 GoUp();
  206.             }
  207.         }
  208.         //if AI facing left or right
  209.         //if something is on his right side
  210.         //then go down
  211.         if (Physics.Raycast(transform.position, rightDirection, out hitRight, VectorLengthRight, layerMask)) {
  212.             if (hitRight.collider.transform.name.StartsWith("Wall") || hitRight.collider.transform.tag == "Border") {
  213.                 GoDown();
  214.             }
  215.         }
  216.     }
  217.  
  218.     // When AI is facing up or down
  219.     private void GoLeftOrRight() {
  220.  
  221.         RaycastHit hitLeft;
  222.         RaycastHit hitRight;
  223.  
  224.         //if something is on AIs left side
  225.         if (Physics.Raycast(transform.position, leftDirection, out hitLeft, VectorLengthLeft, layerMask)) {
  226.             if (hitLeft.collider.transform.name.StartsWith("Wall") || hitLeft.collider.transform.tag == "Border") {
  227.                 //turn right
  228.                 GoRight();
  229.             }
  230.         }
  231.         //if something is on AIs right side
  232.         if (Physics.Raycast(transform.position, rightDirection, out hitRight, VectorLengthRight, layerMask)) {
  233.             if (hitRight.collider.transform.name.StartsWith("Wall") || hitLeft.collider.transform.tag == "Border") {
  234.                 //turn left
  235.                 GoLeft();
  236.             }
  237.         }
  238.     }
  239.     //All directions
  240.     private void GoLeft() {
  241.         transform.eulerAngles = new Vector3(0, -90, 0);
  242.         SpawnWall();
  243.     }
  244.     private void GoRight() {
  245.         transform.eulerAngles = new Vector3(0, 90, 0);
  246.         SpawnWall();
  247.     }
  248.     private void GoUp() {
  249.  
  250.         transform.eulerAngles = new Vector3(0, 0, 0);
  251.         SpawnWall();
  252.     }
  253.     private void GoDown() {
  254.         transform.eulerAngles = new Vector3(0, 180, 0);
  255.         SpawnWall();
  256.  
  257.     }
  258.     /*============================================================================================
  259.      * Walls
  260.      * ============================================================================================*/
  261.     private void FitColliderBetweenWalls(Collider _collider, Vector3 lastWallPos, Vector3 AIpos) {
  262.         //center position
  263.         _collider.transform.position = lastWallPos + (AIpos - lastWallPos) * 0.5f;
  264.  
  265.         //Scale it horizontal or vertical
  266.         float dist = Vector3.Distance(lastWallPos, AIpos);
  267.         if (lastWallPos.x != AIpos.x) {
  268.             _collider.transform.localScale = new Vector3(dist + wallDistance, 1, wallWidth);
  269.         } else {
  270.             _collider.transform.localScale = new Vector3(wallWidth, 1, dist + wallDistance);
  271.         }
  272.     }
  273.     private void SpawnWall() {
  274.  
  275.         //save last wall
  276.         lastWallEnd = transform.position;
  277.  
  278.         // Spawn a new Lightwall
  279.         GameObject g = (GameObject) Instantiate(WallPrefab, transform.position, Quaternion.identity);
  280.         g.name = "WallAI";
  281.         wall = g.GetComponent<Collider>();
  282.     }
  283.  
  284.     //Show Vectors in unity
  285.     void OnDrawGizmos() {
  286.         CheckDifficulty();
  287.  
  288.         forwardDirection = transform.TransformDirection(Vector3.forward) * VectorLengthForward;
  289.         leftDirection = transform.TransformDirection(Vector3.left) * VectorLengthLeft;
  290.         rightDirection = transform.TransformDirection(Vector3.right) * VectorLengthRight;
  291.  
  292.         Gizmos.color = Color.yellow;
  293.  
  294.         Gizmos.DrawRay(transform.position, forwardDirection);
  295.         Gizmos.DrawRay(transform.position, leftDirection);
  296.         Gizmos.DrawRay(transform.position, rightDirection);
  297.     }
  298.     //Checks the AIs difficulty level
  299.     private void CheckDifficulty() {
  300.         switch (Difficutly) {
  301.             case 1:
  302.                 VectorLengthForward = 10;
  303.                 VectorLengthLeft = 7;
  304.                 VectorLengthRight = 7;
  305.                 delayNextDecision = 0.3f;
  306.                 break;
  307.             case 2:
  308.                 VectorLengthForward = 15;
  309.                 VectorLengthLeft = 10;
  310.                 VectorLengthRight = 10;
  311.                 delayNextDecision = 0.2f;
  312.                 break;
  313.             case 3:
  314.                 VectorLengthForward = 20;
  315.                 VectorLengthLeft = 12;
  316.                 VectorLengthRight = 12;
  317.                 delayNextDecision = 0.1f;
  318.                 break;
  319.             case 4:
  320.                 VectorLengthForward = 25;
  321.                 VectorLengthLeft = 14;
  322.                 VectorLengthRight = 14;
  323.                 delayNextDecision = 0.2f;
  324.                 break;
  325.         }
  326.     }
  327.  
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement