Answers for "unity make raycast ignore layer"

1

raycast ignore layer

public LayerMask IgnoreMe;
     private Ray ray;
     private RaycastHit hit;
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
             NeroFiresEverything();
     }
 
     public void NeroFiresEverything()
     {
         // Basic example ray
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit, 1000f, ~IgnoreMe))
         {
             Debug.Log("Get Rekt " + hit.collider.name);
         }
     }
Posted by: Guest on October-05-2021
0

unity make raycast ignore object

Transform targetTransform = null; // target assigned in inspector 
 RaycastHit[] hits; // returned hits
 LayerMask mask = new LayerMask(); // set in inspector - USE LAYER MASK TO FILTER
 hits = Physics.RaycastAll(transform.position, transform.forward, mask);
 
 for(int i =0; i < hits.Length; i++){
     if( hits[i].transform == targetTransform){
         // Look for a specific transform instance
     }
     // Or get a specific Component
     Ship theShipWeHit = hits[i].transform.GetComponent<Ship>();
     if( theShipWeHit != null){
         // Found something with a specific script, such as Health or whatever
     }
     // Or get a specific Tag
     if(hits[i].transform.gameObject.CompareTag("Tag")){
         // Found something with a given tag. 
     }
 }
Posted by: Guest on July-01-2021

Browse Popular Code Answers by Language