- Home /
Take damage after seconds
I have two functions that work together in order to take damage off my first person controller. One has alot of other stuff (such as turn to target), and the main part being:
if(animation["AttackingPlayer"].time >= 0.6
{
RayCastFunction();
}
And then the RayCastFunction will create a raycast coming from the enemy, and hitting the first person controller. Although, the function seems to play a ton of times, and then the first person controller looses all of his life after 0.6 seconds. I can see it going down from 100, so he is not loosing it all at once. Is there a way I can play the RayCastFunction once per animation loop? Because I would only like to take the damage off the player once per attacking animation. Yet the animation must still wait for 0.6 seconds to take the damage since I do not want damage being taken off if the animation has only played for 0.1 seconds and the character decides to run away... Very unrealistic. Should be a VERY SIMPLE fix, although I am quite new to Unity. Thank you for your help!
Answer by The_Magical_Kiwi · Jul 11, 2012 at 11:37 AM
If you declare a bool at the top of your script, you can then use the bool as a switch.
An example will probably be clearer.
//Pseudo code
bool damageDone = false;
if(animation["AttackingPlayer"].time >= 0.6){
if(!damageDone){
RayCastFunction();
damageDone = true;
}
}
Once your animation has finished, set damageDone back to false and you start again.
...or you could take @asafsitner 's advice, which is a much better solution that I had forgotten about... ^^
Answer by zillion · Jul 11, 2012 at 01:25 PM
I believe that the animations event method will not work for this since my character starts in an idle animation, and the attacking animation is only called when the enemy is in close proximity of my first person controller. Unless I'm not using the animations event correctly? It's only letting me do it for the starting animation of the enemy...
Your answer
Follow this Question
Related Questions
Need help with simple RayCast Attack script 1 Answer
GetComponent() problems 2 Answers
Problem with yield waitForSeconds in for loop 0 Answers