- Home /
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. $$anonymous$$aybe use Get$$anonymous$$eyDown ins$$anonymous$$d.
Here is a list of all the keyCodes that Unity offers.
http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$eyCode.html
thank you this works but now the animation plays repeatedly i want it to play once after you press the button
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 ins$$anonymous$$d of loop
Answer by robert_mathew · Sep 12, 2011 at 03:03 AM
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
I have this
using UnityEngine; using System.Collections;
public class Putter : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
GetComponent<ConstantForce>().enabled = false;
}
// Update is called once per frame
public bool animation_bool;
void Update () {
if (Input.GetButtonDown("Jump"))
{
animation_bool = true;
GetComponent<ConstantForce>().enabled = true;
}
if (animation_bool == true)
{
animation.Play("Putter");
}
void OnCollisionEnter(Collision other)
{
Destroy(gameObject);
}
}
}
i'm trying to make a $$anonymous$$i Golf game and I want it to play the animation then hit the ball and Destroy. but I dont know how to get it to play the animation first
Answer by robert_mathew · Sep 11, 2011 at 04:08 PM
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
Answer by fjodorbelet · Dec 28, 2012 at 09:30 PM
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 :(
just change this line:
animation_bool = true;
to this:
animation_bool != animation_bool;
:)
Answer by Stiangul · Jul 03, 2014 at 07:24 PM
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
}
}
Answer by ahsanbutt · Nov 21, 2017 at 01:36 PM
hi judges * by Time.delta *time animation_bool = true;