0
\$\begingroup\$

For example: Physics.Raycast(ray, out hit, range, layerMask), let's say layerMask = groundMask. What I want to do is only return true if it didn't hit a ground mask. Like what I mean is like "Ignore raycast except groundMask". Is there anyway to do this?

Some example:

origin ------------ (groundmask) -------- (other mask) --- // return false; 
because there is a ground mask in a range
origin ---------------------------------- (other mask) --- // return true;
because there is no ground mask in a range
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

If you cast against a layer you should only return true if your cast hits the layer specified.

You can setup the QueryTriggerInteraction to also register objects that have colliders set to trigger.

You can use this to cast against whichever layers you would like to check and it will only return true to the ones you touched.

example: "groundmask"

public bool GroundCheck()
{
    return isGrounded = Physics.Raycast(transform.position, Vector3.down, out hit, range, groundMask,QueryTriggerInteraction.Collide);
}

In your case to see if you are not grounded you can use if !GroundCheck() or if !isGrounded to see if you are no longer touching ground.

Be sure to also set the layer to the object you use for ground or it will not get a return for the ground layer check.

enter image description here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .