简体   繁体   中英

Unity 5 mecanim animation controlled by UI slider

I'm trying to get a slider to control an animation in my scene. I need to the length of the slider to match the length of the animation and when I scrub the slider, the animation should play the appropriate part.

 public Slider slider;
public Animator animator;

void Start()
{
    slider.onValueChanged.AddListener(OnValueChanged);
}

private void OnValueChanged(float changedValue)
{
    animator.speed = 0;
    animator.Play("yourAnimationName", -1, slider.normalizedValue);
}

I have also placed this on my slider components on value changed section but the two still do not synch up. What is it I'm doing wrong / missing?

edit updated code sample and removed the error

Error is quite clear about it. Delegate has to have float parameter. Change

private void OnValueChanged()

to

private void OnValueChanged(float changedValue)

Update:

Don't set the animation speed to 0:

public Slider slider;
public Animator animator;

void Start()
{
    animator.speed = 0.00001f;
    slider.onValueChanged.AddListener(OnValueChanged);
}

private void OnValueChanged(float changedValue)
{
    animator.speed = 0.00001f;
    animator.Play("yourAnimationName", -1, slider.normalizedValue);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM