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 Iceblitzyt · Dec 04, 2013 at 07:12 PM · c#tagsenemiesmissile

How can I get my missile script to target the nearest tagged enemy?

Hey,

I've made a script in which I basically get the missile to target the gameobject tagged enemy and move towards it and destroy it. However I found that if you have multiple enemies in the scene it will not work. This is the code:

 using UnityEngine;
 using System.Collections;
 
 public class Turret : MonoBehaviour {
     public Transform Enemy;
 
     void Start () 
     {
         Enemy = GameObject.FindGameObjectWithTag("Enemy");
     }
 
     void Update () 
     {
         float dist = Vector3.Distance(Enemy.transform.position,transform.position);
         if(dist <150)
         {
             transform.position = Vector3.MoveTowards(transform.position, Enemy.position, 60 * Time.deltaTime);
         }
 
     }
 }

I understand that this will not work because there is more than one GameObject tagged "Enemy" But I don't understand how to get around this? How can i tweek this so it fires towards the nearest tagged enemy within 150 units.

Many thanks in advance

Comment
Add comment · Show 8
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 Shatcore · Dec 04, 2013 at 07:26 PM 0
Share

I take it enemies are prefabs that are instantiated at some point? inside this script I would create a public static enemy list, everytime an enemy is spawned push it onto the list, then in this missile script access the list and sort it into distance order (moving the closest to element zero), then move towards myEnemyList[0].transform.position

avatar image Iceblitzyt · Dec 04, 2013 at 07:34 PM 0
Share

No the missiles are instantiated, the enemies are set up normally. These have their own waypoint pathing in the game, but since it's a space fighter, most of the enemies are in groups. IE mothership and 5 fighterjets.

avatar image alienbaby · Dec 04, 2013 at 07:42 PM 0
Share

fighterjets in space?I don't know the specific code - I'm pretty much a unity noob myself, but something like this?

(pseudocode)

Find all elements with tag 'enemy'

for all the enemies;

  • Get enemy position

  • Get a vector from the player position to the enemy position (position.enemy - position.player)

  • Get that vectors magnitude

  • keep a track of all the enemy magnitudes (say, in an array or some kind of list structure)

find the enemy that has the $$anonymous$$imum magnitude

target missile at that enemy

avatar image Iceblitzyt · Dec 04, 2013 at 07:48 PM 0
Share

Hmm I have an idea on how to do this using lists, i thought there might me a 2 line of code easy fix for this. I'll work on this now and if I solve it I'll post and an answer for other people to use as learning resources. But what you said alienbaby is what I had originally thought to do, i was hoping there was an easier way ha

avatar image Shatcore · Dec 04, 2013 at 07:49 PM 0
Share

If the enemies are not instantiated then thats fine, you can just create a static list on startup and from there go on to sort it in distance order.

Show more comments

3 Replies

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

Answer by Ambro · Dec 04, 2013 at 08:43 PM

There is an article on unity gems that has an elegant solution for this problem, but it's using Linq :

link text

The advantage is that it takes a very few lines to do it, just add at the top of your script :

 //C#
 using System.Collections.Generic; //Always a good idea
 using System.Linq;
  
 //JavaScript
 import System.Linq;

And where you want to make your detection :

 //C#
  
 var closestGameObject = GameObject.FindGameObjectsWithTag("MyTag")
     .OrderBy(go => Vector3.Distance(go.transform.position, transform.position)
     .FirstOrDefault();
  
 //Javascript
  
 var closestGameObject = GameObject.FindGameObjectsWithTag("MyTag")
     .OrderBy(function (go) Vector3.Distance(go.transform.position, transform.position))
     .FirstOrDefault();

The only downside is that the code above doesn't take maximal distance of 150 units into account, but if you look at their article, there is some exemples that use this kind of distance limitation.

If you don't want to learn Linq, just add a distance check on the closestGameObject that it returns. Something like :

 float dist = Vector3.Distance(closestGameObject.transform.position,transform.position);
 
 if(dist <= 150)
 // Do Something

Comment
Add comment · Show 2 · 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 Iceblitzyt · Dec 04, 2013 at 08:49 PM 0
Share

Thanks, I've done a solution myself which i posted here, but I've never used ling before I'm going to research it now. Thanks for the tips.

avatar image Ganda · Jul 31, 2015 at 01:19 AM 1
Share

For anyone who's still interested in this, a working link is here

avatar image
6

Answer by Iceblitzyt · Dec 04, 2013 at 08:35 PM

Okay Guys I fixed this with my own script, I'm adding this to the post so people can use this as a learning resource.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Turret : MonoBehaviour {
     public List <Transform> Enemies;
     public Transform SelectedTarget;
 
     void Start () 
     {
         SelectedTarget = null;
         Enemies = new List<Transform>();
         AddEnemiesToList();
     }
 
     public void AddEnemiesToList()
     {
         GameObject[] ItemsInList = GameObject.FindGameObjectsWithTag("Enemy");
         foreach(GameObject _Enemy in ItemsInList)
         {
             AddTarget(_Enemy.transform);
         }
     }
 
     public void AddTarget(Transform enemy)
     {
         Enemies.Add(enemy);
     }
 
     public void DistanceToTarget()
     {
         Enemies.Sort(delegate( Transform t1, Transform t2){ 
             return Vector3.Distance(t1.transform.position,transform.position).CompareTo(Vector3.Distance(t2.transform.position,transform.position)); 
         });
     
     }
 
     public void TargetedEnemy() 
     {
         if(SelectedTarget == null)
         {
             DistanceToTarget();
             SelectedTarget = Enemies[0];
         }
     
     
     }
 
     void Update () 
     {
         TargetedEnemy();
         float dist = Vector3.Distance(SelectedTarget.transform.position,transform.position);
         //if(dist <150)
         //{
             transform.position = Vector3.MoveTowards(transform.position, SelectedTarget.position, 60 * Time.deltaTime);
         //}
 
     }
 }
 
Comment
Add comment · Show 3 · 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 MegaManX_777 · Jun 26, 2015 at 01:32 PM 0
Share

How can I add the target once it enters a collider that is on trigger?

avatar image FoxTheory · Jul 01, 2015 at 01:01 AM 0
Share

Nice. this does exactly what I want except it's a little advanced for me. How would I get the the target out of the list after it's been collided with.

avatar image wasicool7 · Apr 04, 2017 at 10:04 PM 0
Share

Hi, I know this is a little to late but I was wondering if you can help me fix this problem I ran into using your code. You see I get this error:

                    ArgumentOutOfRangeException: Argument is out of range.
                    Parameter name: index

when there isn't a target for the bullets to shoot at, is there a way of making the bullets wait until it detects if there's an enemy on sight? Thank you for your answer by the way, it was really helpful! :)

avatar image
0

Answer by Shindy117 · Apr 16, 2017 at 10:23 PM

This script works for me however the only problem comes when the object is destroyed. Since it has nothing to destroy. It errors out.

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

My enemies merge when they come towards me 4 Answers

Is there an opposite of GameObject.FindGameObjectsWithTag() ? 2 Answers

C# Enemy list for player to attack 2D 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