• 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
Question by towerer · Jun 02, 2010 at 03:25 PM · enemytargetlocktower-defense

How to make Towers fire at multiple enemies?

I am making some kind of tower defence. The tower has a gun that locks to enemy and fires projectiles. It follows the enemy using Quaternion.LookRotation and Quaternion.Slerp and it works great. The problem is I copy the enemy many times by Instantiate. The tower kills the first enemy and does not lock on the rest.How can I make it follow every enemy? The relevant part of code:

var target : Transform;

if (target == null && GameObject.FindWithTag("enemy")) target = GameObject.FindWithTag("enemy").transform; var targetPoint = target.position; var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 20.0); ...

Comment
duck
limdingwen

People who like this

2 Show 0
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

3 Replies

  • Sort: 
avatar image
Best Answer

Answer by duck · Jun 03, 2010 at 09:37 AM

While "FindWithTag" will find another enemy once the first is destroyed, you don't have much control over which enemy it finds.

Whereas what you probably want is for it to find the next nearest enemy.

You can do that with a bit of scripting, like this:

function GetNearestTaggedObject() : Transform {

 var nearestDistanceSqr = Mathf.Infinity;
 var taggedGameObjects = GameObject.FindGameObjectsWithTag("Enemy"); 
 var nearestObj : Transform = null;

 // loop through each tagged object, remembering nearest one found
 for (var obj : GameObject in taggedGameObjects) {

     var objectPos = obj.transform.position;
     var distanceSqr = (objectPos - transform.position).sqrMagnitude;

     if (distanceSqr < nearestDistanceSqr) {
         nearestObj = obj.transform;
         nearestDistanceSqr = distanceSqr;
     }
 }

 return nearestObj;

}

Comment
towerer
Hei
Bunny83
Aldwoni_legacy
Lr_K
moonLite
FatallErroR

People who like this

7 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 towerer · Jun 03, 2010 at 11:07 AM 0
Share

Thank you, I just found that independently last night and it works great with FindGameObjectsWithTag.

avatar image Bunny83 · May 03, 2011 at 12:14 PM 0
Share

Just in case somebody needs that one in C#, Mike did a translation here: http://answers.unity3d.com/questions/38143/could-someone-help-me-change-this-to-c/38145#38145

avatar image sandybites · May 18, 2014 at 08:53 AM 0
Share

Hi guys, can anyone post a c# version of this? Thanks! THe link above no loner is available...

avatar image

Answer by spinaljack · Jun 02, 2010 at 03:32 PM

How about creating an array of enemies which you can add to as you instantiate new ones. This also means that the turret will target the enemies in the order that they were instantiated (unless you write a script to search through the array).

e.g.

var enemies : Array;

function Start(){ enemies = new Array(); }

function AddEnemy(){ enemies.Push(Instantiate(enemyPrefab,spawnPoint,spawnRotation)); }

Then when an enemy is killed you can use the RemoveAt function to remove it from the array and multiple turrets can share the same enemy array for targeting.

http://unity3d.com/support/documentation/ScriptReference/Array.html

Comment
burnumd
towerer

People who like this

1 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 towerer · Jun 03, 2010 at 11:09 AM 0
Share

Thank you, I have solved my problem otherwise, but array approach is great too. Previously I had tried to put objects to arrays but could not figure it. I will try it your way.

avatar image

Answer by towerer · Jun 02, 2010 at 03:40 PM

OK, I have solved my problem. I noticed that if Iremove the if part above and write target = GameObject.FindWithTag("enemy").transform in Update function it finds all the enemies with the tag.

Comment
Samir 1

People who like this

1 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 bananaboy · Sep 12, 2011 at 05:13 AM 0
Share

thank you!

avatar image vxssmatty · Sep 12, 2011 at 07:01 AM 0
Share

Your problem was that you didnt reset 'target' to null so when the if statement did its thing, target wasnt null and thus wouldnt look for the tagged object again

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can anyone tell me whats wrong with my MoveTowardsEnemy part of my script? 1 Answer

Enemy targeting player in different scene 1 Answer

Moving Enemies? 1 Answer

Tower Defence style spawning(enemies) 1 Answer

Targetting Script Help 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