- Home /
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); ...
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;
}
Thank you, I just found that independently last night and it works great with FindGameObjectsWithTag.
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
Hi guys, can anyone post a c# version of this? Thanks! THe link above no loner is available...
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
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.
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.
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
Follow this Question
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