play animation on button press

Hello Im trying to set my game up so that you can slash your sword by pressing a button, I have made an animation for this and written a code but when I press the button the animation won’t play this is my code.

if(Input.GetButtonDown("]")){
animation.Play("slash");

}

i already set this button up in input what should I do.

I am not totally sure that you can use !"#¤%&/() and so forth using GetButtonDown unless you created them in the input manager. Maybe use GetKeyDown instead. Here is a list of all the keyCodes that Unity offers. http://unity3d.com/support/documentation/ScriptReference/KeyCode.html

can u specificity what you want when u press the button do u want to play animation one time or repeatedly when u press the button .what is happening now using this code

you can break the Boolean loop or in fbx make animation once instead of loop

7 Answers

7

public bool animation_bool;

         void Update()
                    {

             if(animation_bool == true)
                    {
                animation.Play("slash");

                    }

           if(Input.GetButtonDown("space"))
                    {
              animation_bool = true;

                    }



               }

above one is c# coding

    var  animation_bool : boolean ;

      function Update()
                     {

          if(animation_bool == true)
                     {
                 animation.Play("slash");

                     }

          if(Input.GetButtonDown("space"))
                     {
              animation_bool = true;

                     }
				
	         }

above one is java script you can use any one of these it is checked codes this solve your problem

public bool animation_bool;

         void Update()
                    {

              if(animation_bool == true)
                    {
                    animation.Play("slash");

                    }\\animation_bool ends

               if(Input.GetButtonDown("]"))
                    {
                  animation_bool = true;

                    }

if you try call from inside the gui button loop the call will be a fraction of seconds if you animation to play continuously when gui button is pressed you should call using Boolean function like i have done changes in your coding.

         }\\update ends

my animaton play from itselves when i check the animation_bool box

and if i uncheck the box my animation doesn’t work if i press the button

Please help ???
sorry for bad english :frowning:

just change this line: animation_bool = true; to this: animation_bool != animation_bool; :)

Just to fix your script a little, it wont work if you type if(Input.GetButtonDown(“]”)){ it has to be like this:

function Update()
{
    if(Input.GetKeyDown(Keycode.R))
    {
    //in here you can put whatever you want, for example: renderer.material.color = Color.red for example or as you want animation.play
    }

}

hi judges * by Time.delta *time
animation_bool = true;

This is the first result from a search so I should say that this question and the answers are already outdated. You should use the Animator class instead

The website has a tutorial on how to control animation on Controlling Animation - Unity Learn

For new people who finding this post, here are a simple solution. You could use a trigger, by using this you just need to set the trigger to be used in code:

private Animator animTrigger;

private void Upadate()
{
    if (Input.GetKeyDown(KeyCode.RightBracket)
    {
        animTrigger.SetTrigger("Your trigger name");
    }
}

Sorry my English, this is not a skill for me.