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

3 Replies

· Add your reply
  • Sort: 
avatar image
7
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
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 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#, $$anonymous$$ike 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
1

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
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 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
1

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
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 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

enemy targeting player in different scene 1 Answer

Attacking enemy script problem 0 Answers

Closest target acquisition using Physics Overlap Sphere script not working 1 Answer

Next enemy wave timer issue 0 Answers

Script for shooting a target 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