- Home /
Question by
KenvixIlyas · Mar 26, 2013 at 04:37 AM ·
tagmultipletargetmelee
Multiple Targets Using Tags
I have a melee script and I want it to attack multiple target. How do I do this?
Here is the script :
var target : Transform;
var range : int = 3;
var countDown : int = 0;
var coolDown : int = 2;
var damage : int = 35;
var attackSound : AudioClip;
function Start ()
{
if (target == null && GameObject.FindWithTag("Player"))
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
if(countDown > coolDown) {
countDown -= 1 * Time.deltaTime;
}
if(countDown < 0) {
countDown = 0;
}
if(Input.GetButtonDown("Fire1")) {
Slash();
Debug.Log(target.GetComponent(HealthLogic).currentHealth);
}
}
function Slash() {
var distance : int = Vector3.Distance(target.transform.position, transform.position);
var dir = (target.transform.position - transform.position).normalized;
var direction = Vector3.Dot(dir, transform.forward);
if(distance <= range) {
if(direction > 0) {
target.GetComponent(HealthLogic).currentHealth -= damage;
}
}
if(attackSound)
{
AudioSource.PlayClipAtPoint(attackSound, transform.position);
}
}
Help me please......
Comment
Answer by liamcary · Mar 26, 2013 at 05:21 AM
Instead of storing one Transform as "target" store an array or list of transforms and iterate over them when you want to effect them (damage, etc). You can find every gameobject with that tag using:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html
Your answer
Follow this Question
Related Questions
Attack multiple targets in the same tag? 1 Answer
Can I use Multiple tags on if? 1 Answer
targeting multiple enemys 1 Answer
Camera Target Switch By Tag HELP! 1 Answer