Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question [SOLVED] Raycast misses up to 20% of the time

Discussion in 'Physics' started by WidmerNoel, Jul 15, 2021.

  1. WidmerNoel

    WidmerNoel

    Joined:
    Jun 3, 2014
    Posts:
    66
    There are a few threads out there where people discuss raycasts that sometimes miss colliders. Apparently that cannot happen and people often claim that the order of events is causing the issue. I am opening this new thread because I have a more isolated scenario with fewer sources for problems. This is what I have:
    1. A player that walks around.
    2. A rifle which is a child of the player.
    3. The rifle can spawn projectiles.
    4. A projectile has no parent, is independent and knows nothing about the player.
    5. These projectiles then move along their forward direction in each update (or fixed update; I tried both using the appropriate time step for each).
    6. Before moving, the projectile checks if there is anything that blocks the way, in which case it destroys itself.
    7. There are no rigidbodies in the scene.
    8. The only obstacle is a static box collider with a mesh (for visual purpose) and no other components.
    When my player just stands in front of the wall and shoots at it, then all projectiles hit the wall. 100% of them. Every time. But when my player starts moving, the projectiles start to ignore the wall. This does not make any sense to me because the projectiles don't even know about the player. They are not in the player's hierarchy and know nothing about him. I can reach a miss rate of approximately 20% when my player moves parallel to the wall. Moving perpendicular to the wall results in a 100% hit rate. For some reason The angle seem to influence the results.

    I would love to hear your thoughts and appreciate anyone who has encountered a similar issue. :)

    Additional thoughts:
    I am drawing debug lines to represent the raycast. And I can confirm that none of the misses start / end within the collider. All of them start before the wall and end after the wall.
     
    Last edited: Jul 15, 2021
  2. WidmerNoel

    WidmerNoel

    Joined:
    Jun 3, 2014
    Posts:
    66
    I solved the issue. For some reason the hit result for the static wall collider sometimes claimed to be a trigger (by the isTrigger property). No idea why. This seems to be a bug to me. My projectiles ignored collisions with triggers. I solved the issue by specifying QueryTriggerInteraction.Ignore explicitly during my raycast and not manually ignoring triggers after a raycast hit occured.