Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by flyup · Feb 20, 2018 at 07:15 AM · proximity

Most efficient way to target enemies for a tower defense

I've currently written a bit of code for a td-like game that I'm working on. It's how the tower's find which enemies to target when they are in range of the tower. I'd like to know whether there is a more efficient/effective method to carry out the same job with a different approach.


My approach - GIve the tower a big sphere collider which covers the whole range of the tower. When an enemy triggers the collider, it automatically becomes the target of the tower. If the tower already has a target, it is added to a list of objects that are 'in range' of the tower. When the tower's current target leaves the trigger(OnTriggerExit), it chooses another target in random among the enemies 'in range' of the tower. If there are no enemies 'in range', the tower goes dormant.


I've found that there is also a raycasting approach and the Physics.OverlapSphere approach, which are probably better approaches than my approach when the tower has a target. But considering that both approaches have to scan their vicinity constantly to look for targets(when there are not any), it seems somewhat heavier in my opinion.

The problem is, the td game that I am creating will probably have about a maximum of around 60~70 enemies at the same time, making about 20~30 possible enemies being 'in range' of a single tower. This game will have A LOT of towers(possibly 70~100) in the same screen, meaning a lot of objects being tracked at the same time, which I believe could be more resource-heavy than other approaches.(This is what I am not sure about)


Are there any other more cost-effective methods to target enemies or is my method seemingly efficient enough?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Harinezumi · Feb 20, 2018 at 08:18 AM

I think your approach of using a trigger SphereCollider and adding those entered to a list is very good. Checking for "collision" with a SphereCollider is relatively cheap in physics (especially if you model your enemies' physics with spheres as well ;) ). The only thing I would change is that in many TD games the player can choose the tower's target selection strategy, usually among "closest to end of path", "random", "least hit points", etc.

My personal opinion is that the raycasting method would definitely be slower, and I think the Physics.OverlapSphere as well, simply because of the constant scanning.

Finally, to suggest an alternative approach, you could completely circumvent physics and just get the list of enemies from a manager for each tower, and check if each enemy is in range (using Vector3.SqrMagnitude, which is a lot faster than Vector3.Magnitude). This would have about an O(enemies * towers) cost, but not using physics at all.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flyup · Feb 20, 2018 at 09:23 AM 1
Share

Exactly the answer I was looking for. I really like your alternative approach as well, which I will test out in the future if my approach turns out to be laggy in certain situations.(I'll give you a heads-up on the post once I have done) Thanks a lot for the reply.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

getting enemys to follow character. 1 Answer

How to use collision to enable light? 1 Answer

help please :S? im confusing myself. 2 Answers

Turn on lights based on proximity to controller. 1 Answer

Enemy Proximity Script 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges