- Home /
change model state due to health
Hi all,
Been trying to find an answer for this for a while and im currently having a brain freeze.
I have a model which has 5 frames of animation. frame 1 its fine, frame 2 dented, frame 3 more dented, frame 4 more damaged and frame 5 crushed.
I've also created a health var for it.
What i want is when that health drops to under 80 the model changes to frame 2. If the health drops under 60 the model changes again to the next frame and so forth.
Does anybody know how to do it please?
I do apologies if im being thick. But its driving me slowly mad. If it helps i do own Unity Pro.
Thank you in advance.
Answer by equalsequals · May 27, 2010 at 11:31 PM
The way I would do it is break up the animation into separate animation clips and name them accordingly. I'd set their wrap mode to clamp forever as well.
Then you make sure your model's game object has an animation component attached to it and it has reference to the aforementioned animation clips.
From there it is simply a matter of implementing a conditional statement in your script where you accumulate your damage.
Note: You might have to adapt this to suit your Game Object structure, as I am operating under the assumption that the script in which you accumulate damage is a component of the same Game Object that your animation component is attached to.
if(health < 20)
{
animation.Play("animationnamehere");
}
else if(health < 40)
{
//so on and so forth
}
else if(health < 60)
{
//so on and so forth
}
Notice in my script I am working backwards, this is because if you did it the other way( if(health < 90) ) it will return true on that case for everything less than 90 and you wont see the state change because your logic is flawed.
Just a note that there are FAR more efficient ways to do this but I am working here for the sake of simplicity.
Hope that helps.
==
I Like simplicity :) Thankis for the speedy response, I shall give it a go when i get a chance and let you know the outcome.
Thank you again.
Your answer
![](https://koobas.hobune.stream/wayback/20220613032113im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Not identifying the collider? 2 Answers
Enemy Ragdoll help 1 Answer
Ranged damage not working 1 Answer
Help playing the right animation 1 Answer
Health and damage reciever 1 Answer