Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Martin_Gonzalez · Dec 12, 2011 at 10:40 PM · enemydistancechecktower defensebetween

Check Between Enemy List

Hi, i'm trying to finish a TD and i have the last problem. When i press return enemies spawn and they get into a List and walk in paths, i have towers near the paths so they can shoot. My problem is that i do a for to recognize all of them and check wich is the nearest to my tower. i have a state pattern in my towers so i have something like this. What i try to do is : i have 2 enemies, and two towers, i want to make the tower attack the nearest enemy, and the aother tower attack the otherone (that is nearest to it) but my problem is that both towers recongnize the first enemy and wait him to be nearest and perhaps another enemy pass behind a tower and it dont attack :(

in State Iddle (Context is the ''ParentClass'')

 public void UpdateIdle(Context context) {
     context.myEnemyList = context.enemiesList.enemies;
     if (context.tower)
     {

         if (context.myEnemyList != null)
         {

                 for (int i = 0; i < context.myEnemyList.Count; i++)
                 {
                     context.myEnemyList[i] = GameObject.FindWithTag("Enemy");
                     if (Vector3.Distance(context.tower.transform.position, context.myEnemyList[i].transform.position) < 15)
                     {

                         context.enemy = context.myEnemyList[i];
                         Debug.Log(context.i);
                         context.SetState(new StateAttack());
                        
                     }
                 }
         }
     }


Comment
Add comment · Show 1
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 syclamoth · Dec 12, 2011 at 10:55 PM 0
Share

This is kind of a standard problem in Tower Defence games. One popular solution is to mode the towers manually- the player can click on a tower, and tell it what enemy to prioritise (front, back, strongest, weakest, and so on).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by seedoubleyou · Dec 13, 2011 at 01:15 AM

How about a "I'm Being Attacked" boolean? When the tower01 attacks, the enemy01 "I'm Being Attacked" boolean becomes true. So, when tower02 goes to attack enemy01, it finds the boolean switched on, so it ignores it and looks for a different enemy?

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Move the enemy object opposite to player and keep its distance relative to the player? 1 Answer

Need help with tower defense win level system,need help with tower defense win level system 0 Answers

Check for an instantiated object farthest from player? 1 Answer

check if an always increasing variable has stop increasing 2 Answers

How can I check if my Player leaves a Sphere? 2 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