Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lunawins · Apr 04, 2015 at 08:48 PM · animationjavascriptspeedslidertimescale

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lunawins · Apr 09, 2015 at 12:56 PM 0
Share

Thanks for the answer, this was exactly what I was looking for, cheers! :)

avatar image MrAkroMenToS · Apr 09, 2015 at 07:46 PM 0
Share

You can accept it as an answer so other ppl know that it's answered :)

avatar image
0

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;
 
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jbergs · Feb 06, 2016 at 02:24 PM 0
Share

vfxjex, Is there a way to use this script with Animator clips?

avatar image
0

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);
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Current speed of an object? 5 Answers

Help with my "speeding up time" script 1 Answer

Animation play when dead 2 Answers

how to make Time in your game while you play? 1 Answer

Can I make animations snap to a frame? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges