playing an animation once c#

Hi,
I have searched all of the similar questions and none of the solutions have worked. I have checked if it is playing using animation.IsPlaying(), changed the WrapMode to once, but none of it has worked. Also, there is no C# code in any of the answers.

I am playing the animations in this if statement:


        //stuff to do
        if(another control blah blah blah){
        ...
    	else if(Input.GetMouseButton(0)){
    		animation.Play("claw");
    	}
    	//biting
    	else if(Input.GetMouseButton(1)){
    		animation.Play("bite");	
    	}

Claw and Bite are the ones i need to to click and play once.

Thank you

Thank you for actually describing the problem.

That makes it a lot easier to help.

What you're describing sounds different from my personal experience with animations: my animations typically play all the way through once I start them.

You might want to check and/or alter the WrapMode setting of your animation.

You can check it like this:

Debug.Log("wrap mode: " + transform.animation.wrapMode);

The above will print the current wrap mode to the console.

You can change an animation's WrapMode so it repeats forever like this:

transform.animation.wrapMode = WrapMode.Loop

. . . .

This might not be related, but you probably also want to use Input.GetMouseButtonDown(0) rather than Input.GetMouseButton(0).

The difference is this:

Input.GetMouseButtonDown

  • returns true once during the frame the button is pressed down

Input.GetMouseButton

  • returns true every frame the button is held down

If you use GetMouseButton (like your code suggests) you run the risk of having that code execute multiple times (when you might want it to happen only once).

This could be causing problems in your code... not sure.

Have you tried putting the animation.Play(“claw”) and animation.Play(“bite”) outside of the input if structure?


int animationCode = 0;

void Update{
 //stuff to do
        if(another control blah blah blah){
        ...
        else if(Input.GetMouseButton(0)){
           animationCode = 1;
        }
        //biting
        else if(Input.GetMouseButton(1)){
           animationCode = 2;   
        }
    
        if (animationCode = 1){
        animation.Play("claw");}
        if (animationCode = 2){
        animation.Play("bite");}
}

Ok.... Now that you tell us more about your code, that changes things again.

When you say it "goes back to the idle animation" it makes me think your issue is related to the fact that you're trying to play multiple animations.

Try reading the documenation for the particular version of Animation.Play that you're using... It seems to describe exactly what you need to know to fix your problem:

This is directly from the documentation:

....

If mode is PlayMode.StopSameLayer then all animations in the same layer will be stopped. If mode is PlayMode.StopAll then all animations currently playing will be stopped.

If the animation is already playing, other animations will be stopped but the animation will not rewind to the beginning.

If the animation is not set to be looping it will be stopped and rewinded after playing.

Play() will return false if animation can't be played (no animation clip or no default animation). ....

...

Here are two pointers that will help you a great deal with Unity and Unity Answers:

  1. Read the documentation before you post. It's very helpful.
  2. Try to be as descriptive as possible when you ask a question. That makes it much easier for people to understand your problem if they want to help.

...

Here's one last pointer that I think will help you with your problem:

Insert a call to Animation.Stop() before you call Animation.Play().

In other words: when you start an animation, stop all the other animations that are currently running. And if you want your bite animation to play all the way through, make sure you don't tell the idle animation to play again until the bite animation is complete.

You can do it like this (C# works a bit different in JavaScript) :
make booleans for EVERY animation you have an on the Input ( or nothing like for an idle animation)

those booleans control all animations.

and to and an Animation use this Enumerator :

IEnumerator WaitForEndOfAnimation (AnimationClip animation) {

yield return new WaitForSeconds (animation.length - 0.5F);
if (animation.name == “claw”){
clawBool = false;
}
else if (animation.name == “bite”){
biteBool = false;
}
else if (animation.name == “Jump”) {
jumpBool = false;
}
}