- Home /
How i make the player damage the enemy specific enemy he is colliding with
public var target:Transform;
public var attackTimer:float;
public var cooldown:float;
private var anim:Animator ;
public var delay:float=0.1;
function Start () {
attackTimer = 0;
cooldown = 2.0f;
//starts the animator
anim = GetComponent(Animator);
yield WaitForSeconds(0.8);
Destroy(this);
}
function Update () {
anim.SetBool("Attack001",false);
go = GameObject.FindGameObjectWithTag("Enemy");
target = go.transform;
if(Input.GetKeyUp(KeyCode.F)){
anim.SetBool("Attack001",true);
}
}
//take 10 from healthbar
private function Attack() {
(anim.SetBool("Attack001",true));
var Healthbarph :Healthbar ;
Healthbarph = target.GetComponent("Healthbar");
Healthbarph.AddjustCurrentHealth(-10);
}
function OnCollisionEnter(other :Collision){
if (other.collider.tag == "Enemy"){
Attack();
yield WaitForSeconds(delay);
Destroy(this);
}
}
my problem is that this script allows the player to damage the enemy he is colliding with by gathering all the enemies with tag enemy and damage them upon collision but it gathers all the healthbar in a random order so if i collided with enemy a enemy b will take damage what i want is a way to damage the specific enemy i am colliding with i tried to take out all of the targeting lines
go = GameObject.FindGameObjectWithTag("Enemy");
target = go.transform;
and change
Healthbarph = target.GetComponent("Healthbar");
Healthbarph.AddjustCurrentHealth(-10);
to
Healthbarph = GetComponent("Healthbar");
Healthbarph.AddjustCurrentHealth(-10);
but it didn't work
i have a targeting script but cant use it for the as i only targets one person at a time and i need some attacks damage multiply people i am colliding with
here is the healthbar script
public var maxHealth: int = 100;
public var curHealth: int = 100;
public var healthBarLength: float;
function Start() {
healthBarLength = Screen.width / 2;
}
function Update() {
AddjustCurrentHealth(0);
}
function OnGUI() {
GUI.Box(Rect(10, 50, healthBarLength/2, 20), curHealth + "/" + maxHealth);
}
function AddjustCurrentHealth(adj: int) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0 ;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1 ;
if(curHealth == 0 ){
Destroy (gameObject, 1);
animation.Play("Death");}
var maxHealthF: float = maxHealth;
healthBarLength = (Screen.width / 2) * (curHealth / maxHealthF);
}
That's a lot of text without a real specific question but it sounds like you need to rethink how you are applying damage. Search for 'find closest enemy', maybe that will work for you.
Find closest enemy will just target one enemy plus if the player wants to attack a far away person. It wont let them my question is when I collide with an enemy how to call the attack function to that specific enemy I'm colliding with
Your script as posted is already doing this: "how to call the attack function to that specific enemy" (re: OnCollisionEnter)
The problem, I think, is that nothing prevents it from calling Attack() for all collisions with your current code they could happen one after another nearly simultaneously. It's not random, it's the order of the collisions.
Add a boolean to OnCollision such that it activates once for the first enemy hit and does your Attack(), but no other Attack()/collision calls will until your Attack() is done and you reset. Note that depending on your game mechanics you might need to add an OnCollisionStay.
Also, find closest enemy could be combined with List/Array to deter$$anonymous$$e closest enemies (plural) and with a distance check to enable ranged. Obviously I don't know your game or what you are trying to accomplish, just added it as food for thought.
Ill try to explain it better its a third person game this script is design to call the function. Attack when the player collides with an enemy tagged enemy which it does perfectly. How. I control it is turning the script off and on from another script the problem is it gathers up all the enemies tagged enemy and lets the player attack in a random order what I mean by this is if the player attacks. Enemy A the healthbar for enemy B will go down this is because the srcipt just gathers all enemies healthbar and allow the player to attack a random one upon collision not the specific healthbar of the enemy who the player attacked what I want is the player to damage the specific healthbar of enemy they attacked I would use attack closest enemy but i already explain why it wouldn't work in the comment above thank you for the help I hope this clears things up
Like I said in my first comment, you should rethink how you are applying damage. For example, your Update() contains
go = GameObject.FindGameObjectWithTag("Enemy");
target = go.transform;
and you really don't want to be doing that every Update(). If you have multiple "Enemy" gameobject/tags, exactly what are you expecting target=go.transform to be after every Update()?
Your Attack() function could be changed to receive the collider.transform ins$$anonymous$$d of the generic 'target' but the whole thing seems like it needs some more thought. Good luck!
Answer by robertbu · Sep 20, 2013 at 08:15 AM
You say, "gathering all the enemies with tag enemy and damage them upon collision", but how I read you code is that it damages one enemy at "random." That is you call GameObject.FindGameObjectWithTag("Enemy");
which returns some enemy, not necessarily the one you hit. Get rid of target completely, and change the rest of your code as follows:
private function Attack(go : GameObject) {
(anim.SetBool("Attack001",true));
var Healthbarph :Healthbar ;
Healthbarph = go.GetComponent(Healthbar);
Healthbarph.AddjustCurrentHealth(-10);
}
function OnCollisionEnter(other :Collision){
if (other.collider.tag == "Enemy"){
Attack(other.collider.gameObject);
yield WaitForSeconds(delay);
Destroy(this);
}
}
These changes have the game object you collided with used in the Attack() function. Also it is a better practice to use the type rather than a string in GetComponent().
Your answer
Follow this Question
Related Questions
(C#) Enemy health and take damage from bullets 1 Answer
One enemy triggers all the enemies 2 Answers
For loop on collision (two hit kill) 2 Answers
How to cause damage on collision? 1 Answer