- Home /
Make Raycast ignore collider on boolean
Hey guys! very simple question. how do i make that my raycast ignore some kinds of colliders and just pass through them? I got tags for those colliders but i dont know how i can make that i can switch it on and off.
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
var Raycastignore : boolean;
if(Physics.Raycast(transform.position, fwd , hit, rayLength))
{
if(!Raycastignore)
{
if(hit.collider.gameobject.tag == "collider")
{
do stuff
}
else
{
if(hit.collider.gameobject.tag == "collider")
{
just ignore and pass through and then do stuff there.
}
}
}
On the forum I just found on making raycast ignore objects but I didnt foudn how to make it with a boolean. thanks for help!
Answer by pansapiens · Jan 31, 2015 at 02:01 AM
Rather than use tags, you should use Layers. Put the objects you want to ignore in a different Layer (or Layers), then do something like this:
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
var hitLayers : LayerMask;
var ignoreLayers: LayerMask;
private var _raycastLayers : LayerMask;
var RaycastIgnore: boolean;
if (RaycastIgnore) {
var invertedHitLayers = ~hitLayers;
_raycastLayers = ~(invertedHitLayers | ignoreLayers);
} else {
_raycastLayers = hitLayers;
}
if(Physics.Raycast(transform.position, fwd , hit, rayLength, _raycastLayers))
{
if(hit.collider.gameObject.CompareTag("collider"))
{
if (RaycastIgnore) {
// do stuff
} else {
// do other stuff
}
}
}
hitLayers are all the layers you normally want the raycast to hit. ignoreLayers are the layers to ignore when RaycastIgnore = true. These will appear as a dropdown in the Inspector where you can select the layers required.
We do the actual raycast using the _raycastLayers variable - toggling RaycastIgnore will change this from ignoring those layers to hitting those layers.
Note the use of the tilde (~) - this is a bitwise operation that inverts a LayerMask so that the ray will not hit those layers. The pipe | symbol is a bitwise OR, which effectively combines two layer masks. The lines: var invertedHitLayers = ~hitLayers; _raycastLayers = ~(~hitLayers | ignoreLayers); inverts hitLayers, adds ignoreLayers, then inverts everything - if my logic is correct, this will remove ignoreLayers from hitLayers.
A simpler alternative if you want to avoid the bitwise operations could be to just define one LayerMask for when RaycastIgnore = true and another for when RaycastIgnore = false.
Related Unity docs: "Casting Rays Selectively".
Also note that I'm using CompareTag rather than ==, since it's apparently more efficient.
If you are doing lots of manipulation of Layer$$anonymous$$asks (eg inversion etc), I suggest taking a look at Layer$$anonymous$$askExtensions on the Unity wiki.
thanks. but how i make it switchable with an boolean?
A simple way would be to toggle between using two layer masks, depending on what you want to hit in each state. I've updated the answer with a version of what I think you are trying to achieve.
thanks man. thank you very much! i think I´ll be able to do now what i want
Cool. Be sure to accept the answer if it solves your problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220613182004im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
RayCast Boolean Help 1 Answer
How to use a Raycast to see the distance traveled within a collider. 2 Answers
Move player to specific location and avoid obstacle. 2 Answers
Im trying to create a word game with a twist 0 Answers
Raycast not detecting ANY HITS AT ALL when starting inside a collider. 0 Answers