user3529998
user3529998

Reputation: 3

Raycast passing through box collider when trigger is enabled for object

I'm setting up footstep sounds in a 3rd person game. All works fine, except getting water collision to work correctly. I setup a simple box collider where the top of the box is on the surface of the water and the bottom of the box is below the terrain and water. However, when I set the collider to a trigger the player passes through the collider (correct) but the raycast also skips the collider and hits the next object which is the terrain (raycast is checked on in settings to collide with triggers..). of course if i turn off the trigger the player walks on the box collider, which is not what I want. The transfer.position is the in chest of the player model. the water comes up to the model's waist. Any help is appreciated. Thanks.

void Update () {
    RaycastHit hit;


    if (Physics.Raycast(transform.position, Vector3.down, out hit))
    {
        Debug.Log(hit.collider.tag);

    }
}

Upvotes: 0

Views: 11772

Answers (1)

sampenguin
sampenguin

Reputation: 46

Ideas to try:

1) Check Edit->Project Settings->Physics, make sure Raycasts Hit Triggers is checked (looks like you already did, thumbs up).

2) Make sure that your trigger is not on the Ignore Raycast layer.

3) Make your trigger thicker/wider.

4) Give your Raycast call an explicit layer mask and make sure your trigger is on that layer.

5) Make sure your trigger component is actually enabled when the call goes through.

6) Try the other *cast methods... Collider.Raycast, Physics.Spherecast, etc.

7) Solve it a different way, like using another Collider specifically for this purpose on your character object, and using OnTriggerEnter or collision events.

8) I vaguely recall having a similar problem a long time ago, and the solution involved putting a dummy RigidBody on the trigger object... something about Triggers getting filtered out in certain cases when no RigidBody is attached.

Hope that helps!

Upvotes: 1

Related Questions