shooting through objects to reduce chance of successful hit

Okay yeah, title’s not very effective, so I’ll explain in greater detail.

Basically player shoots a target, but rather than use direct collision detection, I want to apply a basic % chance to hit, reduced by terrain in the way, and then reduced by armor or dodge skills on the enemy. The math itself is obviously not very complex, but I’m wondering the best way to go about detecting the cover.

If I’m not mistaken, simply using a collider on the object would terminate the ray, would I then need to do a second (or third, fourth) from the opposite side of the terrain object to the target?

As an alternative, what about a series of rays fired down reporting what was hit? I assume the former method would likely be more efficient, but I’m not sure how to go about it and not have what would be a single line, turn into a lightning bolt or zig-zag.

You can use Physics.RacastAll. It will return a list of all colliders the ray encounters based on the layer mask and the distance parameter.

You can use two raycasts for this. Use the first to find if the target can be hit and the distance to the target. If the target can be hit, then cast a second ray using the distance and count the hits between the two player and the target, and do your calculations accordingly.

look up Physics.RaycastAll … it will shoot through everything and return all colliders it hits

Wow, thanks to both of you, I didn’t realize it would be so simple :slight_smile: