- Home /
Unity3D raycast ignore self
I have an object raycasting, from transform.position forward, however the raycast is hitting the original object. I can't switch layers, as I have many of these objects, all of which need to check for eachother.
I originally thought it was because I had a convex mesh collider of the object, so I tested out just a box collider, however it is still just hitting itself. Where have I gone wrong here?
Answer by komodor · May 07, 2015 at 12:00 PM
Unity 4.6.1 has the option turn on/off the ability to detect a collider that overlaps the start of any 2D line/raycast in Edit -> Project Settings -> Physcis 2D -> Raycasts Start In Colliders.
http://stackoverflow.com/questions/24563085/raycast-but-ignore-yourself
i am generally using the second option, because i usually need ignore more than one collider
you can also disable the collider before raycast and enable after (edit: actually this worked before, then, during 4.6 evolution there was time it wasn't working and i am not sure if it works in u5)
I've seen that a couple times - sounds like that's only a Unity 2D thing though? I'm working on a 3D project.
well check the stackoverflow link then, i guess that's all you can do
Necroing this thread to say that it's "Queries Start In Colliders" now. (I'm using Unity 2020.2.5f1)
Answer by joshua-lyness · May 07, 2015 at 12:01 PM
do you need the box collider on the object? If you don't, then delete it.:)
If you do need it, what about physics.raycastAll? It will list all objects that collide with the ray, and then you could say that if it collides with an object with a tag not equal to "emitter" then it will have an effect...?
I definitely need a collider on it. RaycastAll sounds like it might work for me, I'll try that!
Answer by dudester · May 07, 2015 at 05:00 PM
ok well first make sure you are raycasting from inside the collider make sure there are no colliders on the outside of the object and you should be golden, again the origin of the collider needs to be inside the collider otherwise itll collide with the object , seeing as you are raycasting against objects in the same layer you cant use layermasks so yeah let me know if that works for you.
Aye, I'd found out about that already. The origin of the raycast is inside the mesh, and it is a single collider on the gameObject, and even in a test scene with just two objects this problem occurs.