Because of this, I went for an alternative. I made several events where the laser only target specific objects. And while I did get what I want, it created another problem: Now, the laser (aka the Hitscan) makes contact with two different objects at once. It doesn’t matter whether the object is behind another and whether if both are still not in a death animation.
My first thought was using **nearest object ** but the nearest might be behind an obstacle. You can’t use for each object because that goes through the instances by order of creation.
You could create a list of objects by distance and give them an ID number and use that list or maybe set a boolean on each object like IsReachable if it’s not blocked and then use a condition to filter just the IsReachable ones. You could then use **pick the nearest ** from those picked objects.
If there weren’t too many objects it wouldn’t be too resource intensive.
Maybe I over thought your problem. If you’re just trying to ignore the first and shoot the 2nd then maybe if you check the animation first and then do the the ray cast or pick nearest. I guess it depends on how many targets you have. You might still need to go through the objects by distance.
I made an interesting tower defense example a while back. For efficiency, it used the health extension plus a temporary health. As soon as it was targeted the temp health was deducted. So, when a new target was chosen it would be ignored if the predicted health was zero. Should the projectile miss, the temp health was added back otherwise it was deducted from the real health.
In my example it fires from all 3 towers when you touch. The enemies are draggable.
It used the health to target the weakest. I believe I looped through to look for the one with the lowest predicted health.
I should rework it. I’m sure there’s a lot that I might do differently now but it still has some interesting concepts.