Search Unity

Raycast to avoid all but one layer.

Discussion in 'Physics' started by P1505C, Oct 17, 2017.

  1. P1505C

    P1505C

    Joined:
    Oct 12, 2017
    Posts:
    14
    Hi all.

    I am using a GVRTeleport script to allow teleporting with a cardboard app. I want the raycast for teleportation to ignore all but one layer. Going through this page I have modified the script (apologies, can't find the original link to the owner of this code) accordingly, but now the teleport script sees nothing at all. Any ideas? My floor layer is layer 8, that's the layer I want this raycast to interact with.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class GVRTeleport : MonoBehaviour {
    4.  
    5.     [Tooltip("Toggle, on for when you need to use the mouse to look around. off for when you want to use GVR look around.")]
    6.     public bool debugWithMouse;
    7.  
    8.     [Tooltip("Toggle,used for when you want to test the line Renderer. On for no teleporting")]
    9.     public bool debugNoJump;
    10.  
    11.     [Tooltip("Toggle, on for when applied to camera only. this ensures that the camera will always be set to the variable height")]
    12.     public bool useViewHeight;
    13.  
    14.     [Tooltip("Toggle, turns on Debug.DrawLine. Currently not in Use. ")]
    15.     public bool debugLine;
    16.  
    17.     [Tooltip("height that you would like ensure the camera stays at. Y axis. ")]
    18.     public float viewHeight = 7f;
    19.  
    20.     Vector3 fwd;
    21.     public float maxDistance = 10f;
    22.     public LineRenderer line;
    23.     public GameObject parent;
    24.     public GameObject targetIndicator;
    25.  
    26.     public StraightLineParam genLine;
    27.  
    28.     int layerMask = 1 << 8;
    29.  
    30.     void Start() {
    31.     }
    32.  
    33.  
    34.     void Update() {
    35.         RaycastHit hit;
    36.         Ray ray;
    37.  
    38.         if (Physics.Raycast (transform.position, Vector3.forward, Mathf.Infinity, layerMask)) {
    39.             Debug.Log ("The ray hit the floor");
    40.  
    41.  
    42.             if (debugWithMouse) {
    43.                 Vector2 mousePos = new Vector2 (Input.mousePosition.x / Screen.width, Input.mousePosition.y / Screen.height);
    44.                 ray = Camera.main.ViewportPointToRay (mousePos);
    45.             } else {
    46.                 ray = new Ray (transform.position, transform.forward);
    47.             }
    48.  
    49.             if (Physics.Raycast (ray, out hit)) {
    50.                 Debug.DrawLine (transform.position, hit.point, Color.red);  
    51.             }
    52.  
    53.             if (Input.GetMouseButton (0)) {
    54.                 if (Physics.Raycast (ray, out hit)) {
    55.  
    56.                     if (useViewHeight) {
    57.                         targetIndicator.transform.position = new Vector3 (hit.point.x, hit.point.y + viewHeight, hit.point.z);
    58.                     } else {
    59.                         targetIndicator.transform.position = new Vector3 (hit.point.x, hit.point.y, hit.point.z);
    60.                     }
    61.  
    62.                     targetIndicator.transform.LookAt (hit.point);
    63.                     targetIndicator.GetComponent<Light> ().intensity = 8;
    64.  
    65.                     genLine.genLine (new Vector3 (ray.origin.x + 2, ray.origin.y - .5f, ray.origin.z), hit.point);
    66.  
    67.                     line.material.SetTextureOffset ("_MainTex", new Vector2 (Time.timeSinceLevelLoad * -4f, 0f));
    68.                     line.material.SetTextureScale ("_MainTex", new Vector2 (hit.point.magnitude, 1f));
    69.                 }
    70.             }
    71.  
    72.             if (Input.GetMouseButtonUp (0)) {
    73.                 if (Physics.Raycast (ray, out hit)) {
    74.                     if (!debugNoJump) {
    75.                         if (useViewHeight) { //better way?
    76.                             parent.transform.position = new Vector3 (hit.point.x, hit.point.y + viewHeight, hit.point.z);
    77.                         } else {
    78.                             parent.transform.position = new Vector3 (hit.point.x, hit.point.y, hit.point.z);
    79.                         }
    80.                     }
    81.                     if (!debugLine) {
    82.                         line.SetVertexCount (0);
    83.                     }
    84.                 }
    85.                 targetIndicator.GetComponent<Light> ().intensity = 0;
    86.             }
    87.             Debug.DrawRay (this.transform.position, ray.direction * 5, Color.blue);// .DrawLine(transform.position, hit.point, Color.red);
    88.         }
    89.     }
    90. }
     
  2. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,982
    You can bitshift to choose what layers

    Your int layerMask = 1 << 8; should work.

    However you look like your doing multiple raycasts and not all are using the layermask? Im not sure what you think your doing here, can you explain what you believe your code is doing so I can help better ?
     
  3. P1505C

    P1505C

    Joined:
    Oct 12, 2017
    Posts:
    14
    Hi. To be honest I was playing around, first day in Unity. I had raycasting to interact with elements and a second to allow teleportation. I now have it working on one ray, and it's working fine. I can't get my Bluetooth controller (Box VR) to connect though, so will kill teleportation and figure out a waypoint system. This is fun, even for a non coder.
     
    MadeFromPolygons likes this.