Hello
I have an Animation and I want it to start when the button is held and stop when i leave the button
its 2 state Animation so its very sample
my codes:
You are executing the Anmie() method every frame of the Update, this means that the animation is not visually playing because it is starting over every frame.
You need to add a condition to check if the function is already happening before executing it, so it doesn’t loop over itself.
Remember, everything that is in the Update without a condition preceding it will execute no matter what, avoid this situation since it is a bad practice and will give you problems in the future.
Change:
void Update()
{
if (Input.GetMouseButton(0) && !isPressed) //We get the input from the player and simultaneously checks if the animation is already playing, avoiding a loop
{
isPressed = true;
Anmie();
}
}
I would continue the code however you should change it completely, in my opinion you should have a function for your whole dictionary of animations, similar to this:
private void AnimatorWithPriorities(string animationRequested)
{
if (clipName != animationRequested)
{
clipName = animationRequested;
switch (clipName)
{
case "Death":
animator.Play(clipName);
break;
case "Spawn":
animator.Play(clipName);
break;
case "Walk":
animator.Play(clipName);
break;
}
}
}