- Home /
How to ignore a raycast collision with a particular object?
I have a raycast and there can be multiple objects in front of the object that casts the ray. I want to ignore some of the objects, say object A (tag). In the below situation, I want the raycast to hit to the object B (tag):
Player raycast ---- A ---- B --->
I have seen some answers including "layers". I have no idea if they are necessary in this siutation, and I don't know how to use them or what they are, either. How am I supposed to do this?
Answer by robertbu · Aug 26, 2013 at 02:44 PM
You want to use layers. Here is a reference page that explains layers:
http://docs.unity3d.com/Documentation/Components/Layers.html
Alternately you can use Physics.RaycastAll(), and then sort through the unsorted list of hits.
Using layers is more efficient. RaycastAll() is more flexible since you can setup specific cirteria.
I have played around with levels, read the links you provided along with some other answers but I'm sort of having trouble with the layer system. Can you illustrate a little in your answer by giving a tiny example for ignoring the collisions with the object with tag "B" ?
Layers and tags are two different things. You don't use tags with layers. From this limited information, I don't know the needs of your game. Let's assume that all the objects tagged 'A' are placed on a Layer you names 'LayerA', and all the tags objects with 'B' are on their own layer name 'LayerB'. At the top of the file put:
var layer$$anonymous$$ask : Layer$$anonymous$$ask;
Then raycast will be something like:
if (Physics.Raycast(ray, hit, 100, layer$$anonymous$$ash) {
}
You can manipulate the layer$$anonymous$$ask variable in the inspector. Note you can set more than one layer to true, so be sure to check the value after you set/cleared any of the layers. Just whatever layers you want to be included in the Raycast() to true.
Is there a way to do this without using layers, because my object is already assigned to another layer that I need it to be on, but I can't ignore any of the other stuff that is also in that layer when I do this raycast check?
I just want to ignore this specific object (any instances of it) and not anything else on a particular layer. And I can't put it on another layer because I'm already using it with a layer to make it invisible to the camera and a couple of other things.
As indicated in the answer by robertbu, you cal use RaycastAll
and filter the colliders returned in the array according to your criteria.
I looked at the Unity documentation for this but the example didn't make it clear at all to me how to actually use it. It has some weird array or something, but I have no clue how you get from that all the objects the ray has hit and then ignore certain ones. And does it check all objects in a single straight line and keep going, or what? It's really not clear or intuitive how this works and how I'm supposed to use it.
I can't use a different layer because as soon as I put the object on another layer it just falls through the level (presumably because it's not on the same as the floors and stuff that makes up the level).
So how do I make the raycast ignore the actual object in such a case?
Answer by girishbs · Oct 19, 2013 at 08:19 AM
Just select Layer "Ignore raycast" for the particular object to not affect your raycast so the u can find the object behind it.
Ignore raycast is not making my gun raycast ignore the obect, and when I try to use my own layer that I setup for the gun raycast to ignore, the object is just falling through the scenery.
Any idea why my object might be still be getting hit by the gun's raycast when I set it to layer "ignore raycast" and why selecting my own layer for it would make it fall through the floor?
Note: I don't have that layer set in the Settings to ignore anything like the scenery or whatever.
I can't use this because I need other raycasts to hit the object. Example: I want the raycast for the gun to hit it, but I don't want the raycast for teleporting to hit it. And I can't use a different layer because as soon as I put the object on another layer it just falls through the level (presumably because it's not on the same as the floors and stuff that makes up the level). So how do I make the raycast ignore the actual object in such a case?
I love simple solutions to an unexpected problem. I had a BoxCollider (to register touches on an object and around a SphereCollider (to detect when a user arrives). The SphereCollider was blocking the Raycast for the BoxCollider. Selecting Layer "Ignore Raycast" worked for me. Thanks! Rewarded ;)
Answer by Eli-Davis · Mar 24, 2019 at 07:12 PM
If you have a reference to the specific object, instead of creating layers or checking all hits, you can just temporarily disable the object's collider for the raycast, then re-enable it.
It may seem obvious but at first i didn't think about that and I think it's the cleanest way to handle that i you want to only exclude one entity from the ray cast
Answer by dylanfries · Nov 14, 2020 at 08:06 PM
From the docs: https://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html
RaycastHit[] hits;
hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F);
One thing that can be confusing about Raycasts when you first encounter them.
hits will be a variable of all things you hit, you can access the hit object itself from the hit.
Transform targetTransform = null; // target assigned in inspector
RaycastHit[] hits; // returned hits
LayerMask mask = new LayerMask(); // set in inspector
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.
}
}
The other concepts that are confusing with Raycasts are the out variable (which basically assign the response to the out var even though it is a parameter) and the QueryTriggerInteraction which simply tells the Raycast to ignore Triggers and can be set with QueryTriggerInteraction.Ignore. The default behavior is to include triggers so you only need that if you want to remove triggers from the Raycast.
Your answer
Follow this Question
Related Questions
Custom Collision Detection 4 Answers
Collision Detection If Raycast Source Is inside A Collider? 4 Answers
Optimization of bullet hit calculation per frame 1 Answer
Raycast Mouse Click On Specific Objects Only 0 Answers
Need help ignoring collision 1 Answer