- Home /
I am trying to sync a slider to an animation.
The animation consists of a balloon inflating as the slider is moved towards the right. Can someone explain as to how this could be achieved in C# script?
Answer by jmorhart · Jul 30, 2015 at 12:06 AM
I'm assuming you want the slider to control the animation, and that you are using a uGUI slider and an Animator. If that's the case, you might be able to use something like this:
public Slider slider;
public Animator animator;
void Start()
{
slider.onValueChanged.AddListener(OnValueChanged);
}
private void OnValueChanged()
{
animator.speed = 0;
animator.Play("yourAnimationName", -1, slider.normalizedValue);
}
Thanks for the reply. Can you also explain how I can link a particular frame of the animation to the value of the slider?
The coding part is like this:
public void Change(float ValueOfSlider) { anim. = ValueOfSlider; }
If we link the above C# file to the slider then whenever the value of the slider is increased, the frame corresponding to that value in the animation is initialized. This way it'd control the animation.
Answer by SohailBukhari · Feb 05, 2016 at 04:15 PM
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;
}
}
Your answer
Follow this Question
Related Questions
How to check if slider is moving? 1 Answer
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers
unity Image fill like animation 0 Answers
control split animation using slider. 0 Answers