Advertisement
Guest User

raycastcheck

a guest
Apr 10th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class DungeonRaycastCheck : MonoBehaviour
  6. {
  7.     // Start is called before the first frame update
  8.  
  9.     public GameObject left;
  10.     public GameObject Right;
  11.     public GameObject Up;
  12.     public GameObject Down;
  13.     int Direction = 0;
  14.  
  15.  
  16.  
  17.     void Start()
  18.     {
  19.         FindObjectOfType<Dungeon>().rayCheck = RaycastCheck;
  20.         FindObjectOfType<Dungeon>().rayCheck();
  21.     }
  22.  
  23.     // Update is called once per frame
  24.     void Update()
  25.     {
  26.        
  27.     }
  28.  
  29.     public void RaycastCheck()
  30.     {
  31.         for (; Direction < 4;)
  32.         {
  33.             ///Direction = Random.Range(0, 4);
  34.  
  35.             if (Direction == 0 && Up == true)
  36.             {
  37.                 RaycastHit2D hit = Physics2D.Raycast(Up.transform.position, Up.transform.TransformDirection(Vector2.up), 1f);
  38.                 Debug.DrawRay(Up.transform.position, hit.point, Color.green);
  39.                 if (hit != true)
  40.                 {
  41.                     print("didnt hit anything up");
  42.                    
  43.                     Direction = 1;
  44.                    
  45.                 }
  46.                 else { print("HitCube up"); }
  47.                 Direction = 1;
  48.  
  49.             }
  50.             else if (Direction == 1 && Down == true)
  51.             {
  52.                 RaycastHit2D hit = Physics2D.Raycast(Down.transform.position, Down.transform.TransformDirection(Vector2.down), 1f);
  53.                 Debug.DrawRay(Down.transform.position, hit.point, Color.green);
  54.                 if (hit != true)
  55.                 {
  56.                     print("didnt hit anything down");
  57.                     Direction = 2;
  58.                 }
  59.                 else { print("HitCube  down"); }
  60.                 Direction = 2;
  61.             }
  62.  
  63.             else if (Direction == 2 && left == true)
  64.             {
  65.                 RaycastHit2D hit = Physics2D.Raycast(Right.transform.position, Right.transform.TransformDirection(-Vector2.right), 1f);
  66.                 Debug.DrawRay(left.transform.position, hit.point, Color.green);
  67.                 if (hit != true)
  68.                 {
  69.                     print("didnt hit anything left");
  70.                     Direction = 3;
  71.  
  72.                 }
  73.                 else { print("HitCube left"); }
  74.                 Direction = 3;
  75.             }
  76.  
  77.             else if (Direction == 3 && Right == true)
  78.             {
  79.                 RaycastHit2D hit = Physics2D.Raycast(left.transform.position, left.transform.TransformDirection(Vector2.left), 1f);
  80.                 Debug.DrawRay(Right.transform.position, hit.point, Color.green);
  81.                 if (hit != true)
  82.                 {
  83.                     print("didnt hit anything right");
  84.                     Direction = 4;
  85.                 }
  86.                 else { print("HitCube right"); }
  87.                 Direction = 4;
  88.             }
  89.             FindObjectOfType<Dungeon>().rayCheck -= RaycastCheck;
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement