Animate with button

Hey all

I am very new to Unity, so please go easy on me.

What i’m trying to create is simple animation using the UI, so when a UI button is clicked, an Object animation starts, and when you click the UI button again, it reverses back to original positon.

The project works fine, and when the UI button is clicked an animation starts, but my problem is that there’s a strange delay before the animation starts when the button is clicked, which seems to have a varied delay, and I can’t seem to see what’s causing this delay.

This is the only way I could figure out how to do it, and this is what im doing to create the effect (I dont think i’ve missed anything out)

++++++++++++++++++++++++++

1: Added Cube

2: Clicked cube and select animation from (Window > Animation)

3: Create random animation and turned off Loop and Autostart

4: Applied a parameter “Ani_Speed” to the animation

5: Created C# script

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Ani_Tester : MonoBehaviour
{

  public Button Button;
  public Animator Animation;
  public Canvas Canvas;
  public string Status;

  void Start ()
  {
  Button = Button.GetComponent<Button> ();
  Animation.enabled = false;
  Canvas.enabled = true;
  }

  public void Press()
  {

  Button.enabled = true;
  Status = Button.tag;

  if (Status == "Inactive")
  {
  Button.tag = "Active";
  Animation.SetFloat ("Ani_Speed", 5f);
  }
  else
  {
  Button.tag = "Inactive";
  Animation.SetFloat ("Ani_Speed", -5f);
  }

  Animation.enabled = true;
  }

}

6: Created 2 tags (Active \ Inactive)

7: Added a UI button and assigned the tag “Inactive”

8: Dragged animation script to Main Camera

9: In Inspector for Main Camera, dragged;

The UI Button to “Button”
The Object to “Animation”
The UI Canvas to “Canvas”

10: Clicked the UI button and under Inspector

Under “On Click()”
Click + Symbol
Dragged main camera to “Runtime C” drop down
Drop down selected “Ani_Tester”
Selected Press();

++++++++++++++++++++++++++

thanks for any help

In the Animator window, if you select the transition arrow and look at the inspector, do you have Has Exit Time turned on? If so, try turning it off.

Hey thanks, but when I do as you suggested, I just get this

2962625--219970--Capture.JPG

Sorry, my bad. I thought you were transitioning from one animation state to another.
Now the reason for the delay is because when you click the button, the normalized time of the animation state is either < 0 or > 1. 0 is at the start of the animation, and 1 is the end/length. When you reverse the speed, and since your animation is not looping, the state counts up or down from the current normalized time and plays the animation when the time is between 0 and 1. To visualize this, put this in your update function, start the game and check the console:

Debug.Log(Animation.GetCurrentAnimatorStateInfo(0).normalizedTime);

To get around this, you could use **Amimator.Play()** method instead.

Add this before the if statement:

AnimatorStateInfo animInfo = Animation.GetCurrentAnimatorStateInfo(0);

Then add this after each SetFloat():

float time = Mathf.Clamp01(animInfo.normalizedTime);
Animation.Play("YourStateName", 0, time);

Hope this helps.

Thanks, i’ll try that. Is this the best method to do what i’m trying to do, or is there a more elegant way? Thanks