Caminhoneiro

raycaster

Dec 22nd, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.50 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4.  
  5. public class RayCaster : MonoBehaviour
  6. {
  7.  
  8.     #region Coordination_Variables
  9.     float maxDistance = 100;
  10.     float distanceTofloorFwd;
  11.     float distanceToFloorbwd;
  12.     float distanceToFloorUp;
  13.     float distanceToFloorDwn;
  14.     float distanceToFloorRight;
  15.     float distanceToFloorLeft;
  16.     #endregion
  17.  
  18.     #region LayerBitwise_Variables
  19.     static int layerMask_cylinder = 1 << 11;
  20.     static int layerMask_capsule = 1 << 10;
  21.     static int layerMask_sphere = 1 << 9;
  22.     // int bitLayerPack_cylinder = Convert.ToInt32(layerMask_cylinder);
  23.     // int bitLayerPack_capsule = Convert.ToInt32(layerMask_capsule);
  24.     // int bitLayerPack_sphere = Convert.ToInt32(layerMask_sphere);
  25.  
  26.     Int32 packed = 0;
  27.     #endregion
  28.     private void Start() {
  29.         // layerMask &= ~(layerMask);
  30.        
  31.         // packed = packed | layerMask_cylinder << 20;
  32.         // packed = packed | (layerMask_cylinder << 21);
  33.         // packed = packed | (bitLayerPack_capsule << 21);
  34.         // packed = packed | (bitLayerPack_sphere << 12);
  35.  
  36.  
  37.         packed = (1 << 11 | 1 << 9 );
  38.         UnityEngine.Debug.Log("Pack result" + Convert.ToString(packed, 2 ).PadLeft(32, '0'));
  39.     }
  40.  
  41.  
  42.     void Update()
  43.     {
  44.         Execute_RayCasterShooter();
  45.     }
  46.    
  47.     public void Execute_RayCasterShooter()
  48.     {
  49.         CastRayFoward(Color.green, packed);
  50.         //CastRayBackward();
  51.         //CastRayDown();
  52.         //CastRayLeft();
  53.         // CastRayRight();
  54.         //CastRayUp();
  55.     }
  56.     #region raycasters
  57.  
  58.     void UnpackBitsFromLayerMask()
  59.     {
  60.  
  61.     }
  62.  
  63.     public Vector3 CastRayFoward(Color color, int layerMask)
  64.     {
  65.         RaycastHit hitFoward;
  66.         Vector3 fwd = transform.TransformDirection(Vector3.forward);
  67.         if (Physics.Raycast(transform.position, fwd, out hitFoward, maxDistance, layerMask ))
  68.         {
  69.             //Debug.Log("We hit: " + hit.transform.name);
  70.             // distanceTofloorFwd = hitFoward.collider.gameObject.layer = layerMask;
  71.             // maxDistance = distanceTofloorFwd;
  72.             print(Convert.ToString(layerMask, 2).PadLeft(32, '0'));
  73.  
  74.             Debug.DrawRay(transform.position, fwd * hitFoward.distance, color);
  75.             Debug.Log("Distance from the collider to object who shooted raycast is: " + hitFoward.distance);
  76.             // print("The collided tag is: " + hitFoward.collider.tag);
  77.             print("The collided layer is: " + hitFoward.collider.gameObject.layer);
  78.             print("Hitting: " + hitFoward.collider.gameObject.name);
  79.             return hitFoward.point;
  80.         }
  81.         else
  82.         {
  83.             print("missed");
  84.             Debug.DrawRay(transform.position, fwd * maxDistance, Color.red);
  85.             return transform.position + (transform.forward * maxDistance);
  86.         }
  87.     }
  88.  
  89.     public Vector3 CastRayBackward()
  90.     {
  91.         RaycastHit hitBackward;
  92.         Vector3 bwd = transform.TransformDirection(Vector3.forward) * -maxDistance;
  93.         Debug.DrawRay(transform.position, bwd, Color.red);
  94.         if (Physics.Raycast(transform.position, bwd, out hitBackward))
  95.         {
  96.             distanceToFloorbwd = hitBackward.distance;
  97.             Debug.Log("Distance from the collider to object who shooted raycast is: " + hitBackward.distance);
  98.             print("The collided tag is: " + hitBackward.collider.tag);
  99.             return hitBackward.point;
  100.         }
  101.         return transform.position + (transform.forward * -maxDistance);
  102.     }
  103.  
  104.     public Vector3 CastRayUp()
  105.     {
  106.         RaycastHit hitUp;
  107.         Vector3 up = transform.TransformDirection(Vector3.up) * maxDistance;
  108.         Debug.DrawRay(transform.position, up, Color.blue);
  109.         if (Physics.Raycast(transform.position, up, out hitUp))
  110.         {
  111.             hitUp.distance = distanceToFloorUp;
  112.             Debug.Log("Distance from the collider to object who shooted raycast is: " + hitUp.distance);
  113.             print("The collided tag is: " + hitUp.collider.tag);
  114.             return hitUp.point;
  115.         }
  116.         return transform.position + (transform.up * maxDistance);
  117.     }
  118.  
  119.     public Vector3 CastRayDown()
  120.     {
  121.         RaycastHit hitDown;
  122.         Vector3 down = transform.TransformDirection(Vector3.up) * -maxDistance;
  123.         if (Physics.Raycast(transform.position, down, out hitDown))
  124.         {
  125.             hitDown.distance = distanceToFloorDwn;
  126.             Debug.Log("Distance from the collider to object who shooted raycast is: " + hitDown.distance);
  127.             //print("The collided tag is: " + hitDown.collider.tag);
  128.             return hitDown.point;
  129.         }
  130.  
  131.         return transform.position + (transform.up * -maxDistance);
  132.     }
  133.  
  134.     public Vector3 CastRayRight()
  135.     {
  136.         RaycastHit hitRight;
  137.         Vector3 rght = transform.TransformDirection(Vector3.right) * maxDistance;
  138.         Debug.DrawRay(transform.position, rght, Color.green);
  139.         if (Physics.Raycast(transform.position, rght, out hitRight))
  140.         {
  141.             hitRight.distance = distanceToFloorRight;
  142.             Debug.Log("Distance from the collider to object who shooted raycast is: " + hitRight.distance);
  143.             print("The collided tag is: " + hitRight.collider.tag);
  144.             return hitRight.point;
  145.         }
  146.         return transform.position + (transform.right * maxDistance);
  147.     }
  148.  
  149.     public Vector3 CastRayLeft()
  150.     {
  151.         RaycastHit hitLeft;
  152.         Vector3 lft = transform.TransformDirection(Vector3.right) * -maxDistance;
  153.         Debug.DrawRay(transform.position, lft, Color.red);
  154.         if (Physics.Raycast(transform.position, lft, out hitLeft))
  155.         {
  156.             Debug.Log("Distance from the collider to object who shooted raycast is: " + hitLeft.distance);
  157.             //print("The collided tag is: " + hitLeft.collider.tag);
  158.             hitLeft.distance = distanceToFloorLeft;
  159.             return hitLeft.point;
  160.         }
  161.         return transform.position + (transform.right * -maxDistance);
  162.     }
  163.  
  164.     #endregion
  165. }
Advertisement
Add Comment
Please, Sign In to add comment