- Home /
Trigger and ennemy damage
Hello,
i want to make my player possibility to damage the ennemy and kill them so i make a script when ennemy touch player it hurt them.
HERE IS THE TRIGGER FUNCTION:
function OnTriggerEnter ( other : Collider )
{ // trigger events for collider on foot
print("Collision");
if ( other.tag == "Ennemy")
{
print("Ennemy Collision When attack");
//Get component for modify health
Ennemy = other.GetComponent ( EnnemyAI );
//Define that ennemy near to player
ColEnemy = true;
}
else
{
ColEnemy = false;
}
}
AND HERE A SAMPLE OF DAMAGE (I USE ANOTHER WITH TOUCHPAD ECT... FOR ANDROID)
function Update() {
//DEBUG TEST
//If ennemy near to player
if(ColEnemy)
{
//Modify health var of the ennemy
Ennemy.enHealth -= 50;
print(" Perte vie" + Ennemy.enHealth);
}
}
So do you have a question or it was just a post to show your code to other people ?
Answer by $$anonymous$$ · Jan 06, 2013 at 05:00 PM
You should specify your question, because it isn't clear what's the problem you're having. Other than that, what I already see as problems are these:
1) In Update()
method, you should set ColEnemy = false
after you apply damage, because this way 50 damage will be applied in every frame, which makes your enemy die instantly.
2) Also, in an OnTriggerExit()
, you should set ColEnemy = false
, so your enemy can actually save itself from the player once it entered your player's damaging radius, by moving out of it.
3) This code will only work well for exactly ONE enemy. The reason for this is if multiple enemies collide with the player simultaneously, this code still only applies damage to one of them.
In general I say that the way you coded this is not very good programming practice. To make it better:
a) Move the damage code from Update()
to OnTriggerEnter()
. This also eliminates the need for the ColEnemy
variable, and you can actually damage every enemy which triggers the player.
b) If you'd like your enemies to take damage over time, you can also move the damage code from OnTriggerEnter()
to OnTriggerStay()
. This will be called every frame that the enemy is actually in collision with your player. OnTriggerStay()
of course already means that an OnTriggerEnter()
happened, so you don't have to check that if you don't want to.
c) The above means that your enemies will be damaged every frame, so let's move the damage code again, now to EnnemyAI's OnTriggerStay()
. Make a function takeDamage(damage : int)
, and check here if enough time has passed since the last application of damage with the help of a damageInterval : float
and secondsSinceLastDamage
variable, which control how much seconds need to pass between damaging:
function OnTriggerStay(collider : Collider)
{
if (collider.gameObject.tag == "Player")
{
takeDamage(50);
}
}
function takeDamage(damage : int)
{
if (secondsSinceLastDamage > damageInterval)
{
ActuallyApplyDamageAndCheckIfKilled(damage);
secondsSinceLastDamage = 0;
}
}
Also append this to your EnnemyAI Update()
: secondsSinceLastDamage += Time.deltaTime
. ActuallyApplyDamageAndCheckIfKilled(damage : int)
is just a placeholder here, so make a function for this or just write the code in place here, to subtract from enHealth
and if the enemy died, destroy it with Destroy(gameObject)
, and call any additional game logic here.
Still this code can be further polished, for example to query the player (or some game logic script) as to how much damage should be applied, and not hardcode it to 50, but since you haven't actually asked a question, this is pretty much already so far I've gone with this!
Answer by blad00x · Jan 06, 2013 at 07:26 PM
Thanks you OnTriggerStay was exactly what i was looking for. i modify my script now it's smaller and work well.
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]
Under the answer where it says edit | delete | more , click on more , then convert to comment
How to accept an answer :
On the left-hand-side of the Answer box , there are the following icons :
Thumb Up
Number (of votes)
Thumb Down
A Tick
If an answer worked for you , click on the 'Tick' , the answer should now be highlighted in green. If you like an answer on Any question , you can click on the Thumb UP , the thumb should now be highlighted in green , and the number of votes should rise by 1.
How to reply to an answer / post a comment :
To make a comment , USE the [add new comment] button, a window then opens to type in. The answer fields are for ANSWERS, so unless you are answering your own question , DON'T write in an answer box. This helps the 'site work properly, especially when other people are searching for answers, and want to read answers , not comments.
IF your question changes slightly while commenting and reading comments , EDIT the original Question, so anyone reading from the beginning knows what you are asking.
This will make for a happy experience for everyone. I made mistakes starting on this 'site too, but everyone is helpful if you learn and change these habits.
Following these simple steps helps the website work , and other readers to find answers also.
Happy Coding =]
Your answer
Follow this Question
Related Questions
Gameobject not registering collisions 1 Answer
Distance will not calculate after button is pressed 0 Answers
Melee Combat Collision 2 Answers