- Home /
Control an animation with a slider
Does anyone know how to do this? I tried to use animation.sample but didn't quite work.
I don't understand what you mean - do you mean a slider?
Thought - so, did my answer get close to what you wanted?
I didn't yet try it. It seems about same as my version but i used .time ins$$anonymous$$d of normalizedtime and used .sample. I will try your code when I go back home.
Answer by whydoidoit · Jun 22, 2012 at 06:37 PM
Well as I'm off for a bit - I will assume you meant how do I control the current position of an animation using a slider:
var sliderPos : float = 0;
var animationName = "nameOfAnimation";
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;
}
thank you very much I would change .normalizedtime to .time. I am seeing usual results with normalizedtime.
Answer by vfxjex · Aug 25, 2015 at 03:18 AM
here is a simple code for c#.
//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"].normalizedTime= slider.value;
}
}
Answer by C-Blunt · Jun 22, 2012 at 06:29 PM
Sounds like iTween could be the answer: http://itween.pixelplacement.com/examples.php scroll down to "Using PutOnPath and PointOnPath", there's plenty of documentation on how to implement it on the itween website itself.
Answer by kamullis · Jul 03, 2013 at 04:08 PM
This is working for me pretty darn well. I am currently working on getting this to work with a play/pause button as well. Let me know if you want to see what I have for that so far.
private var hSliderValue : float = 0.0;
private var myAnimation : AnimationState;
function Start(){
myAnimation = animation["Take 001"];
}
function LateUpdate() {
myAnimation.time = hSliderValue;
myAnimation.enabled = true;
animation.Sample();
myAnimation.enabled = false;
}
function OnGUI() {
// Horizontal slider
GUILayout.BeginArea (Rect (60,Screen.height - 30,1220,60));
hSliderValue = GUILayout.HorizontalSlider (hSliderValue, 0.0, myAnimation.length, GUILayout.Width(1200.0f));
GUILayout.EndArea ();
}
Answer by gringofxs · Aug 04, 2013 at 04:05 AM
For me work perfect. How can i the size of the touch slider buttom bigger?