- Home /
Question by
AbdulhamidB7 · May 09, 2017 at 05:23 AM ·
c#animation
Script to access a button animation
So I have a control panel model, with a button model that is animated. I am trying to come up with a script that when the button is pressed( input would be mouse click ), the animation of the button going down would be played. And then the button would stay in that position due to another animation, and then if clicked again the animation of the button going will be played. Here is what I have so far:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Button: MonoBehaviour {
public Animator buttonAnimator;
bool Pressed = false;
bool Not_pressed = false;
public bool clicked = false;
bool which = false;//false is closed
void Start()
{
buttonAnimator = this.GetComponent<Animator>();
buttonAnimator.Play("Nothing");
}
void Update()
{
if (clicked)
{
if (!which)
{
if (this.buttonAnimator.GetCurrentAnimatorStateInfo(0).IsName("Nothing"))
{
pressed();
}
else if (this.buttonAnimator.GetCurrentAnimatorStateInfo(0).IsName("StillDoor"))
{
clicked = false;
which = true;
}
}
else if (which)
{
if (this.buttonAnimator.GetCurrentAnimatorStateInfo(0).IsName("StillDoor"))
{
not_pressed();
}
else if (this.buttonAnimator.GetCurrentAnimatorStateInfo(0).IsName("Nothing"))
{
clicked = false;
which = false;
}
}
}
}
public void pressed()
{
buttonAnimator.Play("ButtonDown");
}
public void not_pressed()
{
buttonAnimator.Play("ButtonUp");
}
}
Comment
Your answer