|
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:
(comments are locked)
|
|
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: Thank you, I just found that independently last night and it works great with FindGameObjectsWithTag.
Jun 03 '10 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 '11 at 12:14 PM
Bunny83
(comments are locked)
|
|
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.
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.
Jun 03 '10 at 11:09 AM
towerer
(comments are locked)
|
|
OK, I have solved my problem. I noticed that if Iremove the if part above and write thank you!
Sep 12 '11 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 '11 at 07:01 AM
vxssmatty
(comments are locked)
|
