- Home /
Raycast hits trigger
How can I prevent a raycast from detecting a trigger?
I'm simply checking if the raycast hit's anything.
forwards = Physics.Raycast (transform.position, -transform.forward, 0.5f);
Btw, most of the time I'm inside a big trigger, and it's colliding against the edge of the trigger from the inside.
Answer by Bob-The-Zealot · Apr 02, 2015 at 03:20 AM
Do this:
Go to: Edit -> Project Settings -> Physics -> Raycasts Hit Trigger
And set that to false.
Answering your own question is considered polite and correct if you locate the answer before the community does.
Thumbs up.
Answer by ian_sorbello · Apr 02, 2015 at 03:13 AM
You need to filter using layers. Your trigger collider needs to be assigned to a layer that your raycast won't flag as a hit.
To do this use
http://docs.unity3d.com/ScriptReference/Physics.IgnoreLayerCollision.html
Answer by Ebonicus · Dec 14, 2016 at 09:57 AM
Thank you for this post, I don't understand why by default a raycast would hit a trigger in Unity. Raycast is a linear sensor and a collider set to trigger/true is a volumetric sensor, sensors should never detect other sensors by default. That's like a laser pointer setting off a landmine.
It took me too long to figure out it was even a raycast causing my issue, I originally thought the trigger checkbox was bugged. You have brought me peace.
it's a old thread, but seeking myself more info about it land me here so i'll answer Ebonicus question about why would a trigger be hit.
I'll give you an easy example. I'm my scene, I see camera raycast to detect certain object and area. I have a door peeking mechanism and in order to peak the door, you look at the edge. The edge zone is a trigger collider. The raycast need to hit it, but i don't want the player to hit it. I can imagine dozen of exemple where a "zone" is require for certain action that rely on raycast whitout the need of a collider.