Hi everyone.
I’ve a unit in 2d game. An archer that can shoot arrows to other units.
We’ve the following entities, each with a specific layer:
- UNITS
- OBSTACLES
- TARGET (that is a UNIT)
When the archer tries to shoot it check the distance with a racast.
RaycastHit2D hit = Physics2D.Raycast(shootRayOrigin, headingNormalized, Unit.attackRange - colliderSize, layerMaskRanged);
Then I simply check if hit == Target.
It works as supposed, and when I fire I check if the enemy is in range, and if the ray is not blocked.
=> If so the Archer shoots.
BUT my other UNITS currently stops the ray.
I’d like to have a ray that is blocked by the OBSTACLE layer, but sends back all the UNITS hit before this OBSTACLE (then I’ll check if TARGET is one of these UNITS).
Is it possible to achieve this with a single raycast? Or do I need to make 2 of them, one to check for OBSTACLES, and the second to check for UNITS?
Ray Example:
----U1----U2-----O-----U3–> MAX RANGE ----- U4
The raycast should be able to hit both hit U1 and U2, but shouldn’t hit U3 and U4.
(Currently my ray can hit just U1, that stops the ray).