Raycast to specific layers in Unity

14,086

Lets start with, you should not be casting a ray in every frame in the update(). Don't know how the Engine may react. You should start by setting a condition to cast the ray, for example when you press a key or similar.

Then, try this instead the 1 << 8 for the mask:

int layer_mask = LayerMask.GetMask("Floor");

//Actually you can add any layer name you want, for example:
//int layer_mask = LayerMask.GetMask("Ground","Enemy","Boxes");

 //do the raycast specifying the mask
 if (Physics.Raycast (ray, out hit, distance, layer_mask))
 {

 } 

Just in case you change the order of the layer accidentally or you modify it in the future, passing the names directly, from my point of view, it's safer.

In case that is not the problem, check what is forward for the GameObject whose transform you are using to cast the ray. Maybe you are just casting the ray in a direction where there is nothing, or at least not Ground.

Share:
14,086

Related videos on Youtube

P1505C
Author by

P1505C

Updated on June 04, 2022

Comments

  • P1505C
    P1505C almost 2 years

    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.

    using UnityEngine;
    
    public class GVRTeleport : MonoBehaviour {
    
        public float viewHeight = 7f;
    
        Vector3 fwd;
        public float maxDistance = 10f;
        public LineRenderer line;
        public GameObject parent;
        public GameObject targetIndicator;
    
        public StraightLineParam genLine;
    
        int layerMask = 1 << 8;
    
        void Start() {
        }
    
    
        void Update() {
            RaycastHit hit;
            Ray ray;
    
            if (Physics.Raycast (transform.position, Vector3.forward, Mathf.Infinity, layerMask)) {
                Debug.Log ("The ray hit the floor");
    
    
                if (debugWithMouse) {
                    Vector2 mousePos = new Vector2 (Input.mousePosition.x / Screen.width, Input.mousePosition.y / Screen.height);
                    ray = Camera.main.ViewportPointToRay (mousePos);
                } else {
                    ray = new Ray (transform.position, transform.forward);
                }
    
                if (Physics.Raycast (ray, out hit)) {
                    Debug.DrawLine (transform.position, hit.point, Color.red);  
                }
    
                if (Input.GetMouseButton (0)) {
                    if (Physics.Raycast (ray, out hit)) {
    
                        if (useViewHeight) {
                            targetIndicator.transform.position = new Vector3 (hit.point.x, hit.point.y + viewHeight, hit.point.z);
                        } else {
                            targetIndicator.transform.position = new Vector3 (hit.point.x, hit.point.y, hit.point.z);
                        }
    
                        targetIndicator.transform.LookAt (hit.point);
                        targetIndicator.GetComponent<Light> ().intensity = 8;
    
                        genLine.genLine (new Vector3 (ray.origin.x + 2, ray.origin.y - .5f, ray.origin.z), hit.point);
    
                        line.material.SetTextureOffset ("_MainTex", new Vector2 (Time.timeSinceLevelLoad * -4f, 0f));
                        line.material.SetTextureScale ("_MainTex", new Vector2 (hit.point.magnitude, 1f));
                    }
                }
    
                if (Input.GetMouseButtonUp (0)) {
                    if (Physics.Raycast (ray, out hit)) {
                        if (!debugNoJump) {
                            if (useViewHeight) { //better way?
                                parent.transform.position = new Vector3 (hit.point.x, hit.point.y + viewHeight, hit.point.z);
                            } else {
                                parent.transform.position = new Vector3 (hit.point.x, hit.point.y, hit.point.z);
                            }
                        }
                        if (!debugLine) {
                            line.SetVertexCount (0);
                        }
                    }
                    targetIndicator.GetComponent<Light> ().intensity = 0;
                }
                Debug.DrawRay (this.transform.position, ray.direction * 5, Color.blue);// .DrawLine(transform.position, hit.point, Color.red);
            }
        }
    }
    
  • P1505C
    P1505C over 6 years
    That worked. I changed a few other bits, will tidy it up and post to share.
  • Ignacio Alorre
    Ignacio Alorre over 6 years
    Perfect! sounds good