[SOLVED] Projectile raycast misses up to 20% but only when the object that spawns the projectile is moving
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:
A player that walks around.
A rifle which is a child of the player.
The rifle can spawn projectiles.
A projectile has no parent, is independent and knows nothing about the player.
These projectiles then move along their forward direction in each update (or fixed update; I tried both using the appropriate time step for each).
Before moving, the projectile checks if there is anything that blocks the way, in which case it destroys itself.
There are no rigidbodies in the scene.
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 parallell 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.
Answer by WidmerNoel · Jul 15, 2021 at 03:05 PM
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.
Your answer
Follow this Question
Related Questions
Ray cast to object to transform, instead all objects with script transform. 0 Answers
How Can I Detect Objets on Click with Raycast? 0 Answers
cant get raycast to work with tags 1 Answer
Cone 'Line of Sight' will not trigger on collision 0 Answers
Help with raycast stopping player from going through walls 1 Answer