B
B
bydlokoder2282020-08-04 14:40:16
C++ / C#
bydlokoder228, 2020-08-04 14:40:16

How to start animation on click in unity?

I just started learning unity and I need to start the sprite animation on button click, I need the script itself

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zZaKko, 2020-08-04
@zZaKko

ui button? You make a public method, and in it you launch the animation you need by the number (or how you wrote it down). And then in the button you select this method.

public void fuckingclick(){ //публичный метод (виден в редакторе)
Animator anim = this.GetComponent<Animator>(); //присваиваем animator, где есть нужная анимация (this - этот обьект, вместо него можно/нужно префаб).
anim.setInteger("shit", 1); //запускаеться анимация shit:1.
}

Or by pressing the keyboard button? Then in update you check the click and start the animation.
{
Animator anim;
void Start(){
anim = this.GetComponent<Animator>(); //присваиваем animator, где есть нужная анимация (this - этот обьект, вместо него можно/нужно префаб).
}
void Update(){
if(Input.GetKeyUp(KeyCode.space))//если отжата space
anim.setInteger("shit", 1); //запускаеться анимация shit:1.
}

ps newbies, do not be lazy to google. https://docs.unity3d.com/ScriptReference/Animator....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question