How to Control the Animation using Slider ?

Hi All,

I have a 3D model with 20 seconds of Animation time.I need real time control on animation using Slider ( like movie player seek bar ).

Suppose my slider value is 0-20 and if i place the slider value as 10, animation should play from 10th second.

Can anyone suggest the solution ?

The solution provided by Vfxjex doesn’t work for me because the Animation component has to be marked as Legacy. I try this solution using Animator instead of Animation and it works for me:

using UnityEngine;
using UnityEngine.UI;

public class ControlAnimation : MonoBehaviour
{
    private Animator anim;
    public Slider slider;   //Assign the UI slider of your scene in this slot 

    // Use this for initialization
    void Start()
    {
        anim = GetComponent<Animator>();
        anim.speed = 0; 
    }

    // Update is called once per frame
    void Update()
    {
        anim.Play("YourAnimationName", -1, slider.normalizedValue);
    }
}

Use AnimationState.time to go to specific time from your animation like:

animation["AnimationClip"].time = 10.0f;

Now you can set the 10.0f value to be the value you set by your slider. Something like below in C#:

using UnityEngine.UI;  // Needs to be included at the top in order to be able to access the new UI elements.

Slider mySlider; // Assign your slider you want to use to control the animation time.

animation["AnimationClip"].time = mySlider.value;

try this code

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;
	}
}

you need to play your animation so you can update via slider and just make the animation speed to zero.