- Home /
Can I make SphereCast work with triggers?
It seems that SphereCast won't cast against triggers (even though RayCast does...)
Is there any way that I can make them work with triggers? (Maybe a layer setting or something; I am hoping here!)
I also have this problem. Extremely frustrating because I need to use triggers as colliders are too inefficient.
I also have this problem... anybody have an alternative method??
Answer by Matou · Apr 01, 2014 at 09:46 AM
A bit late maybe, but it seems that there is a workaround:
create a new child object under your trigger object
add a collider to the child object that has the exact same shape as the trigger
create a new layer mask (let's say "ColliderCast") and set this layer mask on the child collider
- go to the Edit -> Project Settings
Physics Panel
disable all collisions on the "ColliderCast" layer mask.
Thus, nothing will collide with a "ColliderCast" object so this new collider shouldn't change the behavior of other objects passing through.
However the sphereCast should collide with it, because it's a collider and not a trigger (except if you filter the "ColliderCast" layer mask). It seems to work for me.
Hope that helps. Peace :)
This is a great answer BUT... Something to be aware of; in a game I worked on, I ran into issues when the "ColliderCast" collider was the exact same size as the trigger collider. After a lot of investigation (announcement debugs) I found that the weapons that used Raycasts (projectile weapons) where registering against the "ColliderCast" (used to register melee attacks). This is in spite of the box for Gun/$$anonymous$$elee being unchecked - objects on either of these layers should be completely unaware of each other.
I shrank one of the colliders by 0.1 of a unit and have never had the same false register since. It doesn't seem to matter whether it's the trigger or non-trigger collider.
BTW, I know this sounds illogical, I spent an hour trying to prove myself wrong; but I can recreate the "false positive" with 100% success and fix it 100% by ensuring at least some size difference between the colliders.
Answer by lil_billy · Oct 27, 2012 at 11:42 PM
sorry, but no the reference says you cant
however you could try a more complicated version of raycast like having a common center and shooting a bunch of rays from it in different directions or if you are using raycast in an update (which i dont recommend) you could lerp the direction to essentially create a sphere.
though doing it that way, i can't foresee it being particularly useful because of how inefficeint it is good luck
$$anonymous$$y goal is to simply give units a "range" in which they can detect other units around them (say, within 20 meters). Would OverlapSphere be the best approach?
ive heard that triggers can detect triggers if both objects have a rigidbody but on that if all you want to do is have a range in which to detect an enemy unit, you dont need to detect the enemy units range you can simply make the range to detect the unit surely your unit is using a solid collider
Your answer
Follow this Question
Related Questions
DontGoThroughThings vs Triggers 1 Answer
Raycast not updating inside a trigger collider 0 Answers
How do I detect if there's an object at the same position and destroy ONLY ONE of them? 1 Answer
Box Collider wont trigger a Trigger after moveing the object 0 Answers
Raycast hits a collider and halts. 1 Answer