- Home /
How to control animation with UI slider?
So I have problem, how I control animation with UI slider, so if I am moving slider forward it plays animation forward and if backwards it plays animation backwards. I have this script, but it doesn't work anymore in U5 and I have no idea how to make it work with UI slider.
var sliderPos : float = 0;
var animationName = "metarig|height";
function Start() {
animation[animationName].enabled = true;
animation[animationName].weight = 1;
}
function OnGUI() {
sliderPos = GUI.HorizontalSlider(Rect(40,40,400,20), sliderPos, 0.0, 1.0);
animation[animationName].normalizedTime = sliderPos;
}
import UnityEngine.UI;
var obj = GameObject("");
var animName = obj.GetComponent.<Animation>();
var whatAnimation = "";
var wrap$$anonymous$$ode = Wrap$$anonymous$$ode.PingPong;
var SliderPos : float;
var animationSlider = GetComponent(UnityEngine.UI.Slider);
function Start () {
animationSlider.direction = Slider.Direction.LeftToRight;
animationSlider.$$anonymous$$Value = 0;
animationSlider.maxValue = GetComponent.<Animation>().clip.length;
animName.GetComponent.<Animation>().Play(whatAnimation);
}
function Update() {
animationSlider.value = whatAnimation.value;
animationSlider.value = GetComponent(UnityEngine.UI.Slider).value;
animation[animName].time = GetComponent.<Animation>().normalizedTime = SliderPos;
}
function moveSlider(newPos : float) { SliderPos = newPos; }
Still doesn't work I can't get SliderPos to affect animation and even slider wont do anything with SliderPos variable :( help needed.
Answer by RamiT · Mar 10, 2015 at 07:47 PM
I made it work. :D There is code if someone need help with similar problem.
import UnityEngine.UI;
var obj = GameObject("");
var animName = obj.GetComponent.<Animation>();
var whatAnimation = "";
var animWrapMode = WrapMode.PingPong;
var SliderPos : float;
var animationSlider = GetComponent(UnityEngine.UI.Slider);
function Start () {
animationSlider.direction = Slider.Direction.LeftToRight;
animationSlider.minValue = 0;
animationSlider.maxValue = whatAnimation.Length;
animName[whatAnimation].enabled = true;
animName[whatAnimation].weight = 1;
animName[whatAnimation].wrapMode = animWrapMode;
}
function Update() {
animName[whatAnimation].normalizedTime = SliderPos;
}
function moveSlider(newPos : float) {
SliderPos = newPos;
}
Hello RamiT, can you please help to link your script to object position ins$$anonymous$$d UI slider position ?
Hello! I didn't fully understand what you mean can you tell it more specifically? :)
Answer by vfxjex · Aug 25, 2015 at 03:07 AM
here's a simple code for C#
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
How to Control the Animation using Slider ? 3 Answers
UI Slider, Save Values and Change Them 0 Answers
Help With Animation Play Back - Reverse 1 Answer
Dynamic Values in Animation Clip 0 Answers