- Home /
Multiple Enemies Help
Hello, my problem is not with the enemy attacking player, but the opposite. How do I get multiple variables defined the way I want? I know of arrays, but I have no idea how to use those. Here is my script:
#pragma strict
var healthGUI : GUIText;
var health : float;
var attackTime : float;
private var zero : float;
var attackDamage : float;
var enemyAI : EnemyAI;
function Update () {
zero += Time.deltaTime;
healthGUI.text = "Health: " + health;
if(health >= 70)
healthGUI.color = Color.green;
else if(health <= 70 && health > 30)
healthGUI.color = Color.yellow;
else if(health <= 30)
healthGUI.color = Color.red;
if(Input.GetMouseButtonDown(0) && zero >= attackTime && enemyAI.inArea == true) {
enemyAI.SendMessage("PlayerHitting", attackDamage, SendMessageOptions.DontRequireReceiver);
}
}
As you can see I can only have one enemy attack because I have one variable. I don't want to add a variable every time I have to put a enemy in the scene. Please help! D:
Answer by Tehnique · Jul 31, 2014 at 03:13 PM
I notice you have an area in which you attack. Is this area centered on the player? If so, the way to go is:
Put all your anemies on a layer.
Run an OverlapSphere on the enemy layer, centered on the player and with the range you want.
Get all hits and send them the message you want.
The implementation is pretty basic, just read the documentation, and you shouldn't have any problems.
Ins$$anonymous$$d of an OverlapSphere, I used a Trigger. Thanks for pointing me in the right direction, though! Here is the code:
#pragma strict
var healthGUI : GUIText;
var health : float;
var attackTime : float;
var zero : float;
var attackDamage : float;
var inArea : boolean;
function Update () {
zero += Time.deltaTime;
healthGUI.text = "Health: " + health;
if(health >= 70)
healthGUI.color = Color.green;
else if(health <= 70 && health > 30)
healthGUI.color = Color.yellow;
else if(health <= 30)
healthGUI.color = Color.red;
}
function Damage (damage : float) {
health -= damage;
healthGUI.fontSize += 1;
yield WaitForSeconds(0.01);
healthGUI.fontSize += 1;
yield WaitForSeconds(0.01);
healthGUI.fontSize += 1;
yield WaitForSeconds(0.01);
healthGUI.fontSize += 1;
yield WaitForSeconds(0.01);
healthGUI.fontSize += 1;
yield WaitForSeconds(0.01);
healthGUI.fontSize += 1;
yield WaitForSeconds(0.01);
//////////////////////////
healthGUI.fontSize -= 1;
yield WaitForSeconds(0.01);
healthGUI.fontSize -= 1;
yield WaitForSeconds(0.01);
healthGUI.fontSize -= 1;
yield WaitForSeconds(0.01);
healthGUI.fontSize -= 1;
yield WaitForSeconds(0.01);
healthGUI.fontSize -= 1;
yield WaitForSeconds(0.01);
healthGUI.fontSize -= 1;
}
function OnTriggerStay (col : Collider) {
if(col.gameObject.tag == "Enemy" && zero >= attackTime && Input.Get$$anonymous$$ouseButtonDown(0)) {
col.gameObject.Send$$anonymous$$essage("PlayerHitting", attackDamage, Send$$anonymous$$essageOptions.DontRequireReceiver);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Die on collision doesnt work 2 Answers
I messed up my script. Anyone wanna help? 1 Answer
c#help reversing a script 1 Answer
Enemy AI won't take Damage 1 Answer