Control animator animation with UI Slider
Hey guys, so I know there are quite some topics like mine, but none worked for me:
I have the camera animated and I want to be able to use the slider to change where I was in the animation. The idea is that I can go through my animation like a video on youtube. I managed to control the animation with my slider, but the animation only runs as long as I move the slider. Means: Its not starting like a video, but just stands still until I move my slider with the hand. 
 Anyway, here is my code:
 
 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class AnimControllSlider : 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("TestAnim", -1, slider.normalizedValue);
 }
 
               }
Changing the anim.speed to for example 1 or so doesnt do the trick.
Thanks in advance!
Answer by MrMarans · Oct 26, 2018 at 01:43 PM
Got it!
 private Animator anim;
 public Slider slider;   //Assign the UI slider of your scene in this slot 
 
 Animator m_Animator;
 string m_ClipName;
 AnimatorClipInfo[] m_CurrentClipInfo;
 float m_CurrentClipLength;
 float timer;
 // Use this for initialization
 void Start()
 {
     anim = GetComponent<Animator>();    
     //Get them_Animator, which you attach to the GameObject you intend to animate.
     m_Animator = gameObject.GetComponent<Animator>();
     //Fetch the current Animation clip information for the base layer
     m_CurrentClipInfo = this.m_Animator.GetCurrentAnimatorClipInfo(0);
     //Access the current length of the clip
     m_CurrentClipLength = m_CurrentClipInfo[0].clip.length;
     //Access the Animation clip name
     m_ClipName = m_CurrentClipInfo[0].clip.name;
     print(m_CurrentClipLength);
     timer = (1 / m_CurrentClipLength)/60;
 }
 
               Sliders are moving between 0 and 1, thats why I used 1. Then we need the length of the clip and then be devided by 60[because of 1 update per second for Update() ] and thats it. I now have something like a youtube-timeline
 // Update is called once per frame
 void Update()
 {
     anim.Play("NameOfAnimation", 0, slider.normalizedValue);
     slider.normalizedValue += timer;
 }
 
               And this calculated it for every frame.
Your answer