- Home /
Animation works only once when button pressed
Hello to all!
The title above sums it up pretty well, but here are some images to explain how the animation works only once on a button click, and to find a solution.
The Animator:
Button OnClick (Image 1), and Animation Inspector (Image 2):
The solution could be a script or something else, but I don't know in what direction to go in. If it is a script, any references or help to solve the problem would be great!
Answer by MezFo · Feb 26, 2018 at 04:24 AM
You just need to check "Loop" in the inspector when your animation is selected. You can see the check box in the photo you posted but it's unchecked. If you are using mouse click to make snake move I would assume you want the snake to stop moving when you take finger off mouse? If so then you should make a script and use GetButton to activate the animation and movement and to stop the animation and movement. You will need to add a transition to and from the snake animation in your Animator and on the transition add a bool to activate the transitions.
public Animator anim;
void Start () {
anim = GetComponent<Animator>();
}
void Update () {
if(Input.GetButton("Fire1")) {
anim.SetBool("Moving", true);
}
else {
anim.SetBool("Moving", false);
}
}