x


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);
...
more ▼

asked Jun 02, 2010 at 03:25 PM

towerer gravatar image

towerer
123 9 8 13

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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;
}
more ▼

answered Jun 03, 2010 at 09:37 AM

duck gravatar image

duck ♦♦
47.4k 129 188 457

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

Jun 03, 2010 at 11:07 AM towerer

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

May 03, 2011 at 12:14 PM Bunny83

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

May 18 at 08:53 AM sandybites
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jun 02, 2010 at 03:32 PM

spinaljack gravatar image

spinaljack
9.8k 26 38 109

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.

Jun 03, 2010 at 11:09 AM towerer
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jun 02, 2010 at 03:40 PM

towerer gravatar image

towerer
123 9 8 13

thank you!

Sep 12, 2011 at 05:13 AM bananaboy

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

Sep 12, 2011 at 07:01 AM vxssmatty
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1033
x346
x181
x75

asked: Jun 02, 2010 at 03:25 PM

Seen: 3784 times

Last Updated: May 18 at 08:53 AM