Hitscan not always hit first target in sight

zented

Member
Hello,

In one of my scenes with many mesh colliders, under certain angles, I can't hit a target (a character) that's in front of me. Instead, the hit gameobject is some environment asset behind with a mesh collider (I know it with Debug.Log(hitGameObject) added line 965 in ShootableWeapon script).

This is not happening every time but when it does, I immediately disable most of the environment assets and after that, I can hit again the target that's right in front of me from the very same angle.

It's like the hitscan is somehow confused when there is too many mesh colliders in line of sight.
But I don't understand why the first object in line of sight isn't always the one that will be hit.

Anyone saw something similar?
 
On the ShootableWeapon there is a max collision count value that you can adjust. When doing a hitscan the raycast results array will be allocated to that value.

But I don't understand why the first object in line of sight isn't always the one that will be hit.
Physics.RaycastNonAlloc doesn't have a concept of "first". It returns the distance but that distance isn't in order.
 
When the problem occurs, increasing the Max Hitscan Collision Count value does nothing.
But let's say the target is within a 5 meters range, if I then set the Hitscan Fire Range value to 5, it can be hit again from the same angle every time I shoot (when the problem occurs, I don't move/rotate).
 
Last edited:
You can debug this by placing a breakpoint within ShootableWeapon.HitscanFire. It's the Physics.RaycastNonAlloc line.
 
Placing a breakpoint does nothing. Maybe something's wrong with Visual Studio Code or something is misconfigured :/
 
I thought of writing a script that would raycast and find the first object in line of sight and then would adjust the Hitscan Fire Range value accordingly.
It feels like a dirty solution though.
 
Hmm, if the breakpoint isn't being toggled then it doesn't sound like you are using the hitscan method. I would start to debug why it's not being hit.
 
Top