Best way to make Turret not shoot a target it cannot hit

Heya, I’m making a Tower Defense game and i’m trying to make my Turret’s AI system a little bit better. Here ill explain briefly how my Towers decide what to hit, because it could help a bit with the problem.
Turret AI

  • The Turret waits for enemies to enter its range, once they do they are added to a list of “Possible Targets”.
  • Depending on the selected “targeting” (selected from a menu in game), the Turret decides who is the best target among the ones in the list (if targeting is “Nearest”, the chosen target in the list is the nearest, if targeting is “Last”, the chosen target in the list is the lasttarget on the path, ecc…).
  • The target is passed down to a bunch of components that handle rotations and such, and finally the turret shots.

Right now my Towers shoot even is something is on the way of the bullet, and what i want is a way to make Towers “skip” a Target that the Turret simply cannot hit for a reason or another. I already thought of one actually: a Linecast is shot out of the Turret to the Target’s position, and if along the way the Linecast doesn’t it the ground (or Walls or whatever), then the Target can be hit, else the target is skipped for the second best target on the list. Now this is indeed a solution to the problem but i don’t think making a Linecast for every target for every single Tower is so good for performance, so i ask you guys for a better idea.

If you have questions ask them here.
Thanks in advance!

What you can do is have a Trigger sphere collider that keeps track of targets within the collider.
Sort the targets with the lowest distance (or largest, or highest hp or lowest hp or whatever stat)
Then Raycast from the tower to the target. If it can’t hit, go to the next target in the list. if it can hit then assign the target.

1 Like

I’m gonna throw a wild idea out here now.

Did you consider, colliders within colliders?

Never assume something is bad until you try it. Lots of AAA titles perform 100s if not 1000s of raycasts/linecasts. Test it make sure your assumption is correct.

This… ^ ^ ^ ^

Unity is a BEAST.

The following is the only way to handle optimization:

DO NOT OPTIMIZE “JUST BECAUSE…” If you don’t have a problem, DO NOT OPTIMIZE!

If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

Window → Analysis → Profiler

Failure to use the profiler first means you’re just guessing, making a mess of your code for no good reason.

Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

https://discussions.unity.com/t/841163/2

Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

Notes on optimizing UnityEngine.UI setups:

https://discussions.unity.com/t/846847/2

At a minimum you want to clearly understand what performance issues you are having:

  • running too slowly?
  • loading too slowly?
  • using too much runtime memory?
  • final bundle too large?
  • too much network traffic?
  • something else?

If you are unable to engage the profiler, then your next solution is gross guessing changes, such as “reimport all textures as 32x32 tiny textures” or “replace some complex 3D objects with cubes/capsules” to try and figure out what is bogging you down.

Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

This sort of speculative optimization assumes you’re properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.

ok i guess ill just try the linecast thing. thanks

Keep in mind that a bullet that takes X frames to travel may end up having to pass through invalid space anyway.

Scenario:

  • tower decides best enemy to shoot is the one that is just a millisecond later going to step behind a blocker

  • bullet goes out, bullet hits blocker

A lot of TD games just assume the bullet succeeds in one way or another: eg, push it through other geometry, make it instantaneous, etc.

If you elect to cancel the bullet then this feeds back on how hard it will be for you to balance the game, since timing of when a bullet fires will affect blocking, and if you cancel bullets differently, then you change the effect and balance of the game.

Try RaycastCommand