• 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 Stef · Aug 07, 2014 at 07:32 PM · arrayselectclosestnext

Select/attack closest target array

Hey Guys,

I have a horde of Zombies (of course) that are attacking a group of towers, each tagged "Tower". The script works okay, but I want the zombies to attack the closest building first, then the next closest, and so on. I'm sure I need an array to do t$$anonymous$$s, but have little experience. Here's my code thus far:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
     public Transform target;
     public int moveSpeed;
     public int rotationSpeed;
     public float maxDistance = .5f;
 
     private Transform myTransform;
     NavMeshAgent agent;
 
 
     void Awake () {
         myTransform = transform;
     }
 
 
     void Start () {
         agent = GetComponent<NavMeshAgent> ();
 
     }
     
 
     void Update () {
 
         GameObject go = GameObject.FindGameObjectWithTag("Tower");
         
         target = go.transform;
         Debug.DrawLine(target.position, myTransform.position, Color.yellow);
 
         //Look at Target
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
 
         if (Vector3.Distance(target.position, myTransform.position) > maxDistance) {
         //move towards Target
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
     }
 }

What would be the best way to add the array and make t$$anonymous$$s work?

Thanks,

Stef

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
0

Answer by Alessio89 · Aug 07, 2014 at 08:30 PM

The simplest way is to iterate through an array and keep track of the closest one until you checked them all. Or you can use Linq (since you're using C#) and do somet$$anonymous$$ng like t$$anonymous$$s:

 GameObject closestTower = (from tower in GameObject.FindGameObjectsWithTag("Tower") orderby Vector3.Distance(transform.position, tower.transform.position) select tower).FirstOrDefault();

T$$anonymous$$s will return the closest object tagged as tower. However, you should note that: To use Linq you must include System.Linq in your usings, also you don't get much in terms of performance compared to iterating through an array or list, but it's cleaner if you ask me.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

How To Find the Closest Object 0 Answers

Cycle through targets. 2 Answers

How do I select all other arrays 1 Answer

How do i get my script to work? 2 Answers

Move an object towards closest enemy. 1 Answer


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