Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Raycast ignore all but layer8

Discussion in 'Scripting' started by tomjoe, Nov 10, 2015.

  1. tomjoe

    tomjoe

    Joined:
    May 11, 2015
    Posts:
    44
    EDIT:wow, missing the distance did this to me. now works fine.
    thanks for looking

    Hello all.
    My problem is i want to cast a ray and have it pass-through/ignore all layers except for a single layer, layer a.
    i make 2 cubes, a and b.
    i make 2 custom layers, a and b. which are user layer 8 and 9.
    cube a goes to layer a
    cube b goes to layer b

    in editor, move cube b in front of cube a.
    run scene, and click.
    only hit b.
    if i move camera so i can see both, it will hit the first collider only. never passes through. also, it seems odd that it can even hit layer b, user layer 9, since i told it to only hit a, layer 8.

    my code:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6. void Update()
    7.  
    8.     {    
    9.         if (Input.GetMouseButtonDown(0))
    10.         {
    11.             int layerMaskTest = 1 << 8;      
    12.             RaycastHit hitInfo = new RaycastHit();
    13.             bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, layerMaskTest);
    14.             if (hit)
    15. Debug.Log(hitInfo.transform.gameObject);  
    16.             {
    17.                 Debug.Log("hit: " + hitInfo.transform.gameObject.layer);
    18.                if (hitInfo.collider.gameObject.name == "a")
    19.                 {              
    20.                     Debug.Log(hitInfo.transform.gameObject);              
    21.                     Debug.Log("Hit a");                
    22.                 }
    23.             }
    24.         }
    25.     }
    i have been looking at other examples, none seem to work for me.
    http://docs.unity3d.com/Manual/Layers.html didn't work, even when i copied their code.
     
    Last edited: Nov 10, 2015
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    In Physics.Raycast, the parameter after the hitInfo is always the maxDistance, meaning the length of the ray. Instead of specifying the layer, you are defining the ray length.
    http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

    Just add Mathf.Infinity as the maxDistance:
    Code (csharp):
    1. bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, Mathf.Infinity, layerMaskTest);
    As a side note: It is not necessary to create the RaycastHit on your own at line 12.
     
    tomjoe and Kiwasi like this.
  3. tomjoe

    tomjoe

    Joined:
    May 11, 2015
    Posts:
    44
    Thanks Dantun! :)