Let's say I have a Raycast, but I don't want it to collide with any triggers. I don't want to change the Project settings since that's a little inflexible.
How can I have a Raycast ignore any triggers?
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Sign up to join this communityThere is a (optional) parameter for that in Physics.RayCast which is available in Unity 5.2 and up:
public bool RaycastNoTriggers(Vector3 startPosition , Vector3 direction, float distance)
{
Ray ray = new Ray(startPosition, direction);
RaycastHit hit;
return Physics.Raycast(ray, out hit, distance, DefaultRaycastLayers, QueryTriggerInteraction.Ignore);
}
Select a tag to all triggers called "Ignore Raycast" this will make the Ray ignore the triggers and pass through.
public bool RaycastNoTriggers(Vector3 startPosition , Vector3 direction, float distance)
{
Ray ray = new Ray(startPosition, direction);
RaycastHit hit;
Physics.Raycast(ray, out hit, distance);
if (hit.collider != null && !hit.collider.isTrigger) {
return true;
}
return false;
}