Author Topic: Controlling Animations with a slider  (Read 3918 times)

NineTails

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Controlling Animations with a slider
« on: April 19, 2014, 05:39:57 AM »
I'm trying to tie up my animation to a slider. When I play, the slider length should be that of my animation and when I drag the slider, it should play the appropriate part of the animation.

However, I'm having some real issues doing this.

This is what I have so far

  1.         public AnimationClip animation1;
  2.         public UISlider slider;
  3.  
  4.         // Use this for initialization
  5.         void Start ()
  6.         {
  7.                 anim.clip = animation1;
  8.                 anim.Play();
  9.                 float animationLength = slider.value * animation1.length;
  10.                 slider.value = animationLength;
  11.        
  12.         }

But this is wrong. Could someone please help me out?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Controlling Animations with a slider
« Reply #1 on: April 19, 2014, 11:00:27 PM »
Slider value ranges from 0 to 1.

baustin27

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Controlling Animations with a slider
« Reply #2 on: April 23, 2014, 01:42:31 AM »
Im also having a problem with linking animation with ui slider, im new to coding all together.

I know using the gui in unity you use this on the object that has the animation, but what would you do for the ngui slider?

  1. public class slidertest : MonoBehaviour {
  2.  
  3.         private AnimationState myanim;
  4.         public float hSliderValue = 0.0F;
  5.  
  6.         // Use this for initialization
  7.         void Start () {
  8.  
  9.                 myanim = animation ["anim"];
  10.        
  11.         }
  12.        
  13.         // Update is called once per frame
  14.         void Update () {
  15.        
  16.         }
  17.  
  18.         void LateUpdate(){
  19.                 myanim.time = hSliderValue;
  20.                 myanim.enabled = true;
  21.  
  22.                 animation.Sample ();
  23.                 myanim.enabled = false;
  24.  
  25.                 }
  26.  
  27.  
  28.         void OnGUI() {
  29.                 hSliderValue = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), hSliderValue, 0.0F, myanim.length);
  30.         }
  31. }
  32.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Controlling Animations with a slider
« Reply #3 on: April 23, 2014, 05:39:59 AM »
You want your slider to control your animation position?
  1. myanim.normalizedTime = slider.value;