Facebook
From Ardit Pranvoku, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 500
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Control : MonoBehaviour
  6. {
  7.     public bool ReadyToExecute;
  8.     public bool wasToRight;
  9.     public bool done;
  10.     // Start is called before the first frame update
  11.     void Start()
  12.     {
  13.         done = false;
  14.         ReadyToExecute = true;
  15.         wasToRight = false;
  16.     }
  17.  
  18.     // Update is called once per frame
  19.     void Update()
  20.     {
  21.         if(!done)
  22.         {
  23.             Main();
  24.         }
  25.        
  26.     }
  27.  
  28.     public void Main()
  29.     {
  30.         if(ReadyToExecute)
  31.         {
  32.             CheckSensors();
  33.             Debug.Log("Checking Sensors...");
  34.             ReadyToExecute = false;
  35.         }
  36.     }
  37.     public IEnumerator MoveForward()
  38.     {
  39.         Debug.Log("Moving forward...");
  40.         for (int i = 0; i < 180; i++)
  41.         {
  42.             this.transform.position += (this.transform.forward / 180);
  43.             yield return null;
  44.         }
  45.         SetReady();
  46.     }
  47.  
  48.     public IEnumerator TurnCCW()
  49.     {
  50.         Debug.Log("Turning...");
  51.         for (int i = 0; i < 180; i++)
  52.         {
  53.             this.transform.Rotate(new Vector3(0f, -0.5f, 0f));
  54.             yield return null;
  55.         }
  56.         SetReady();
  57.     }
  58.  
  59.     public IEnumerator TurnCW()
  60.     {
  61.         Debug.Log("Turning...");
  62.         for (int i = 0; i < 180; i++)
  63.         {
  64.             this.transform.Rotate(new Vector3(0f, 0.5f, 0f));
  65.             yield return null;
  66.         }
  67.         SetReady();
  68.     }
  69.  
  70.     public void SetReady()
  71.     {
  72.         if (!ReadyToExecute)
  73.             ReadyToExecute = true;
  74.     }
  75.  
  76.     public void CheckSensors()
  77.     {
  78.         bool inFront = false;
  79.         bool toRight = false;
  80.  
  81.         RaycastHit hit;
  82.         if (Physics.Raycast(transform.position, this.transform.forward, out hit, 0.7f))
  83.         {
  84.             Debug.Log("There is something in front of me!");
  85.             inFront = true;
  86.         }
  87.         if (Physics.Raycast(transform.position, this.transform.right, out hit, 0.7f))
  88.         {
  89.             Debug.Log("There is something to the right of me!");
  90.             toRight = true;
  91.         }
  92.         if(inFront == false && toRight == false) //No walls around
  93.         {
  94.             if (wasToRight == false)
  95.             {
  96.                 StartCoroutine(MoveForward());
  97.                 Debug.Log("No walls to left or right sensed.");
  98.             }
  99.             else
  100.             {
  101.                 StartCoroutine(TurnCW());
  102.                 Debug.Log("No walls to left or right sensed. There was previously a wall to right.");
  103.                 wasToRight = false;
  104.             }
  105.         }
  106.         else if(inFront == true) //Wall in front
  107.         {
  108.             StartCoroutine(TurnCCW());
  109.             Debug.Log("Wall in front sensed.");
  110.             wasToRight = false;
  111.         }
  112.         else if (inFront == false && toRight == true) //Wall to the right only
  113.         {
  114.             StartCoroutine(MoveForward());
  115.             Debug.Log("Wall to the right only sensed.");
  116.             wasToRight = true;
  117.         }
  118.     }
  119.  
  120.     public void turnOff()
  121.     {
  122.         done = true;
  123.     }
  124. }
  125.