- Home /
C# Parameter questions 2D game
Hi I am pretty new to Unity and I need some help with parameters. I know that you can use bool parameters to transition from Any State to another animation.
I have my animation set up and have my Any State transitioning to my Death animation. I have a Dead bool parameter and have the transition set to when Dead = true.
Then in my code I have this in void Update().
if (animator) {
//get the current state
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo (0);
if (stateInfo.nameHash == Animator.StringToHash ("Base Layer.Any State")) {
if (health <= 0)
animator.SetBool ("Dead", true);
} else if (health > 0) {
animator.SetBool ("Dead", false);
}
}
For some reason it doesn't work. I have tried everything from just using if(health <=0) animation.SetBool ("Dead", true); vise versa, to a more complicated approach.
For some reason it never recognizes that Dead = true when health = 0. The box won't check during the game for Parameter Dead.
I got it to work for my Ground Bool Parameter to check if you are on the ground.
Not sure if it is because it is in the "health" script and not the controller script. I'm still learning.
The health bar goes down when I come in contact with an enemy, health goes up when I get a health coin. This is the most trouble I have had getting something to work so far.
Forgot to mention. I have animator = GetComponent();
in void Start()
and
protected Animator animator;
at the top
Answer by Mikenekro · May 12, 2014 at 10:31 PM
Ok so I got it to check the "Dead" parameter by taking all of that out and just using this.
if (health == minHealth)
animator.SetBool ("Dead", true);
lol So simple too.