- Home /
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 ?
Answer by Rodlaiz · Feb 22, 2017 at 02:57 PM
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);
}
}
Yep, this works for the new Animator system!
Wish there was some way to deprecate / downgrade all the help/forum answers on the internet that still apply to the old Legacy Animation system. Really confusing having to weed through both.
Answer by HarshadK · Nov 18, 2014 at 10:28 AM
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;
Now is there a way to use Time.deltaTime() to control the slider movement?
Answer by vfxjex · Aug 25, 2015 at 02:56 AM
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.
Your answer
Follow this Question
Related Questions
Animation Stop and Stay 1 Answer
How to control animation with UI slider? 2 Answers
Animation Control by frame (use Animation.State.time) 1 Answer
Animation Time? 2 Answers