- Home /
auto select enemys
Hello im trying to make a game that has a fighting system like oblivion. I am trying to make it so any enemy within a certain distance of the player looses health i DON'T want it to be like most mmorpg's where they target enemys i want it to be base on skill. Heres my script for PlayerAttack:
var target : GameObject; var attackTimer : float; var coolDown : float; var damage : float = 10;
// Use this for initialization function Start () { attackTimer = 0; coolDown = 1.0f;
}
// Update is called once per frame function Update () { if(attackTimer > 0) attackTimer -= Time.deltaTime;
if (attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyUp(KeyCode.F)) {
if(attackTimer == 0){
Attack();
attackTimer = coolDown;
}
}
} function Attack() { var distance = Vector3.Distance(target.transform.position, transform.position);
var direction = Vector3.Dot((target.transform.position - transform.position).normalized, transform.forward);
Debug.Log(direction);
if(distance < 3) {
if(direction > 0) {
target.GetComponent(EnemyHealth).AddjustCurrentHealth(-damage);
}
}
}
heres my EnemyHealth script:
var curHealth : float = 20; var maxHealth : float = 20;
function Update () { AddjustCurrentHealth(0);
}
function AddjustCurrentHealth(adj) { curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if (curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
if(curHealth < 1) { //die
Destroy(gameObject);
}
}
the scripts currently work fine but I want it to be adapted to muliple enemys. I want it to find GameObject.tag == "Enemy";
within a distance of 3 or something and is in the direction im facing or something along those lines. Can someone please help thanks.
It would be helpful if you described what behaviour your seeing now? Does this currently not work? Or would you want the code adapted for multiple enemies?
ok currently the script works but i dont want to only have one enemy (the target variable) i want to be able to walk up to any enemy and attack it but only that one looses health.
oh and yes i want it to be adapted for multiple enemys thanks
Answer by Casper 1 · Apr 25, 2011 at 10:39 AM
What you want to do is set the tag "Enemy" for all your enemies.
In your PlayerAttack you find all gameobjects with the tag enemy (Save them into an array), then you find the distance between your player and your enemies (Save the distance in the array above as well).
Select the enemy who has the lowest distance and attack him.
I've got the tag enemy for all my enemy's, but how do I save them as an array and find what one is closest I don't know alot about scripting. Thanks
IF the below (Answer by GesterX) doesn't work you can look here http://answers.unity3d.com/questions/54813/how-do-i-get-my-script-to-work - He is trying to accomplish the same as you
Answer by GesterX · Apr 25, 2011 at 01:24 PM
Look at the second example on this page of the scripting reference here: http://unity3d.com/support/documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html
It contains an example of a function to return the closest enemy. You can then just use this to find the closest enemy and decrease his health etc...
Answer by Aydan · Apr 26, 2011 at 09:21 PM
Here I figured it out thanks for your help.
Targeting Script:
var target = closest; var closest : GameObject;
function Update(){ target = closest; // Find all game objects with tag Enemy var enemies : GameObject[]; enemies = GameObject.FindGameObjectsWithTag("Enemy"); var distance = Mathf.Infinity; var position = transform.position; // Iterate through them and find the closest one for (var enemy : GameObject in enemies) { var diff = (enemy.transform.position - transform.position); var curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = enemy; distance = curDistance; } } return closest; }
PlayerAttack Script
var target : GameObject; var attackTimer : float; var coolDown : float; var damage : float = 10;
// Use this for initialization function Start () { attackTimer = 0; coolDown = 1.0f;
}
// Update is called once per frame function Update () { target = GetComponent(Targeting).target;
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if (attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyUp(KeyCode.F)) {
if(attackTimer == 0){
Attack();
attackTimer = coolDown;
}
}
} function Attack() { var distance = Vector3.Distance(target.transform.position, transform.position);
var direction = Vector3.Dot((target.transform.position - transform.position).normalized, transform.forward);
Debug.Log(direction);
if(distance < 3) {
if(direction > 0) {
target.GetComponent(EnemyHealth).AddjustCurrentHealth(-damage);
}
}
}
Thanks to GesterX and Casper.