Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Control animation playback with slide

Discussion in 'Animation' started by DRDemo, Sep 25, 2017.

  1. DRDemo

    DRDemo

    Joined:
    Aug 31, 2017
    Posts:
    3
    All, I have a simple animation where blocks move upwards sequentially. Please see attached project.

    I have added a slider and want to be able to move backwards and forwards through the animation by dragging the slide position. I have been looking for a solution to this for ages and would appreciate some help.

    Script for the animator is below, project file attached.


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class ControlAnimation : MonoBehaviour
    {
    private Animator AnimationControl;
    public Slider slider; //Assign the UI slider of your scene in this slot

    // Use this for initialization
    void Start()
    {
    AnimationControl = GetComponent<Animator>();
    AnimationControl.speed = 0;
    }

    // Update is called once per frame
    void Update()
    {
    AnimationControl.Play("Animation", -1, slider.normalizedValue);
    }
    }

    Many thanks
     

    Attached Files:

  2. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    You might want to look at the documentation for "AnimationClip.SampleAnimation"

    Haven't done this myself but it might just be what you are looking for. Basically, don't call "Play" because then you loose control over the animation. Instead use "AnimationControl.GetCurrentAnimatorStateInfo(0)" (assuming the clip is on layer 0) to get to "AnimatorClipInfo.clip". Once you have a reference to the actual animation clip you want to control, use "AnimationClip.length" to know how long the animation is and make your slider's max value match the length of the clip.

    Next, simply set your slider's OnValueChanged event to pass that value to the "SampleAnimation" function and that should move the clip to the position you want.

    Good luck
     
  3. DRDemo

    DRDemo

    Joined:
    Aug 31, 2017
    Posts:
    3
    Thanks for the reply, unfortunately I am not familiar enough to be able to start at first principles like that. :(
     
  4. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    It's really not that complicated, actually... All you need is a reference to the clip that you are currently playing... Once you have that you are good to go.

    Code (csharp):
    1. public Slider mySlider;
    2. AnimationClip clip;
    3. void SetSliderValue(AnimationClip the_clip)
    4. {
    5.     clip = the_clip;
    6.     mySlider.MaxValue = clip.Length;
    7.     mySlider.Value = 0f;
    8. }
    9. void OnSliderValueChanged(float value)
    10. {
    11.     clip.SampleAnimation(value);
    12. }
    Again, not tested but that should give you a good starting point. First, though, get a reference to the current animation being played... See? My first post sounded way more scary than it really is :)

    Good luck