- Home /
Control animation speed with slider
Hey all, I'm quite new to scripting so bare with me :) I need to make slider that controls the speed of a specific animation. For example, it needs to automatically play at x1 speed and up to x10 speed. So far I have made buttons that controls it by using Time.timeScale, but am uncertain how to do this with a slider.
I'm working with JavaScript and this is for the UI that became available for Unity 4.6 btw.
Answer by MrAkroMenToS · Apr 09, 2015 at 12:35 PM
Instead using a ton of "if" you can use something like this:
Time.timeScale=(int)(speed/5);
Basically it divides the speed by 5 and it crops the numbers after the whole part. [if speed is 17.5 timeScale will be 3] I guess this is what you are looking for, if not feel free to write me:) I wrote this via my iPad, sorry if i misstype something.
Thanks for the answer, this was exactly what I was looking for, cheers! :)
You can accept it as an answer so other ppl know that it's answered :)
Answer by Lunawins · Apr 09, 2015 at 11:44 AM
I figured out a way to make this work actually, and by using c# instead. But as you can see it looks quite inefficient to write all those lines of code when you could probably do something like making the Time.timeScale value dependent on speed's value. Problem is, I don't have a clue on how to write this.. a helping hand with this would be greatly appreciated, thx :)
using UnityEngine;
using System.Collections;
public class speedAdjust : MonoBehaviour {
public float speed = 0f;
public void normalTime () {
// set scale at which time passes to 1.0, running normal speed.
Time.timeScale=1f;
}
void Update () {
if (speed <=5f) {
Time.timeScale=1f;
}
if (speed >=10f) {
Time.timeScale=2f;
}
if (speed >=15f) {
Time.timeScale=3f;
}
if (speed >=20f) {
Time.timeScale=4f;
}
if (speed >=25f) {
Time.timeScale=5f;
}
if (speed >=30f) {
Time.timeScale=6f;
}
if (speed >=35f) {
Time.timeScale=7f;
}
if (speed >=40f) {
Time.timeScale=8f;
}
if (speed >=45f) {
Time.timeScale=9f;
}
if (speed >=50f) {
Time.timeScale=10f;
}
}
public void AdjustSpeed(float newSpeed) {
speed = newSpeed;
}
}
Answer by vfxjex · Aug 25, 2015 at 03:12 AM
here a simple way
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"].speed = slider.value;
}
}
vfxjex, Is there a way to use this script with Animator clips?
Answer by Rodlaiz · Feb 22, 2017 at 02:58 PM
Try this:
using UnityEngine;
using UnityEngine.UI;
public class ControlAnimation : 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("YourAnimationName", -1, slider.normalizedValue);
}
}
Your answer
Follow this Question
Related Questions
Current speed of an object? 5 Answers
Help with my "speeding up time" script 1 Answer
Animation play when dead 2 Answers