Animation playback with a slider

Hi friends, this is my first topic for Unity!

…i need to playback a single clip animation using 2 button to start and stop(i’ve already made them), and a slider to move trough the “timeline”.
What i need is the code to make the knob of the slider moving according to the actual time of the clip and vice-versa: drag the knob to modify the actual time of animation.

Thanks to all!
Nicola

Simply connect your slider to the clip’s animation.time, possibly using animation.sample() if you adjust the slider manually.

Thanks Quietus, i understand your suggestion, but i’m pretty new to Unity and JS so…can you post an example of the code needed to make the slider?

PS: Cthulhu rules!

Sure, sliders are easy!

    GUILayout.BeginHorizontal();
    myValue = GUILayout.HorizontalSlider(myValue, 0.0, 1.0, GUILayout.MaxWidth(100)); 
    GUILayout.EndHorizontal();

They’re done similar to the above. The slider that appears will be 100 pixels wide and would change the variable myValue from a range of 0 to 1. Remember though, that it’s a float, so you’ll need to account for that according to your animation’s rate and time duration. Simple enough though.

There’s the documentation for the class.

OK, here is the complete script with button and sliders; all works fine, but when i click the knob slider the animation continues and the knob moves according to it, i need to stop the animation when the knob is pressed!

private var MyCurrTime : float = 0.0;
private var MySliderTime : float = 0.0; 


function Start () { 
	Time.timeScale = 1;
	animation["Take 001"].enabled = true;
}


function OnGUI () { 
	//Animations buttons (works fine)
	if (GUI.Button (Rect (600,25,75,30), "Stop")) { 
	Time.timeScale = 0;
	}
	if (GUI.Button (Rect (700,25,75,30), "Play")) { 
	Time.timeScale = 1;
	}
	if (GUI.Button (Rect (800,25,75,30), "Rallenty")) { 
	Time.timeScale = 0.1;
	}
	if (GUI.Button (Rect (900,25,75,30), "Restart")) { 
	animation["Take 001"].normalizedTime = 0.0;
	animation["Take 001"].enabled = true;
	animation.Sample();
	}
	//Looping animation (works fine)
	if(animation["Take 001"].normalizedTime>1){
	animation["Take 001"].normalizedTime = 0.0;
	animation["Take 001"].enabled = true;
	animation.Sample();
	}
	//slider (works but, when i click the knob i also need to stop the animation!)
	MyCurrTime = animation["Take 001"].normalizedTime;
	MySliderTime = GUI.HorizontalSlider (Rect (12, 600, 1000, 30), MyCurrTime, 0.0, 1.0);
} 


function Update () {
	animation["Take 001"].normalizedTime = MySliderTime;
	animation.Sample();
}

Thanks for your help!

Unfortunately I don’t have a clear solution for that. There use to be a GUIElement.HitTest that you could use in conjunction with checking if the mouse button is down. However that apparently doesn’t work with the new gui system.

There is a hack, but it isn’t too pretty. It goes something like this.

  1. Create a label, button or some other gui element that encompasses the entire screen space. Make sure it’s drawn first so it’ll be in the back or set the zdepth manually.

  2. Assign a GUI.tooltip to your slider.

  3. Check if the mouse button is down. If it is, and the tooltip equals your keyword then you can be relatively certain that the user clicked on your slider. You can then stop your animation’s running.

Hopefully someone else has an elegant solution for you, instead of the above ugly one.


Edit: Maybe you could disconnect the value of the slider from the animation, track it separately. If the value changes (as ongui is called multiple times a frame) then it’s likely user interaction. Something along those lines, just a random thought.

The post is a bit old so I post a new way using Unity UI

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

public class AnimControl : MonoBehaviour {
    Animation anim;
    public Slider slider;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animation> ();
        anim.Play ("SphereAnim");
        anim ["SphereAnim"].speed = 0;
    }
   
    // Update is called once per frame
    void Update () {
        anim["SphereAnim"].time = slider.value;
    }
}

Hope this helps…