Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 behdadsoft · Nov 02, 2017 at 07:46 PM · slider

UI slider value control

Hi.

i made a car game that can get some bonus like Nitro and i have a slider in my scene that show Nitro time. (default slider value is 5.)

so i have NitroTimer in my code that i can decrease my slider value with it. like below:

 if (NitroTimer > 0)
 {
     NitroTimer -= Time.deltaTime;
     slider.value = NitroTimer;
 }

All things work very well. but i want when NitroTimer <= 0 and my slider value is 0, Simultaneously i increase slider value to 5 again and decrease NitroSpeed to 0. for do this i used this code but it don't work correctly:

 if (NitroTimer <= 0 && NitroSpeed > TempSpeed)  // TempSpeed is last car speed before enable NitroSpeed 
 {
     NitroSpeed -= 1f;  // decrease NitroSpeed 
 
     // increase slider value
     float val = NitroSpeed / 5 * Time.deltaTime;   
     slider.value += val;
 }

Does anyone how can do it?

Thanks.

Comment
Add comment · Show 6
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 jchester07 · Nov 03, 2017 at 07:22 AM 0
Share

You want to increase the slider value until NitroSpeed < TempSpeed but you are increasing the slider value where NitroSpeed < TempSpeed? Should it be NitroSpeed > TempSpeed?

avatar image jchester07 · Nov 03, 2017 at 07:37 AM 0
Share

I'm thinking you could do a $$anonymous$$athf.Lerp ins$$anonymous$$d of adding value to the slider.Also it depends on how you want the value to increase.

 NitroSlider.value = $$anonymous$$athf.Lerp(nsStartValue, ns$$anonymous$$axValue, 1 - (NitroSpeed/(origNitroSpeed - TempSpeed)))


avatar image behdadsoft jchester07 · Nov 05, 2017 at 08:56 PM 0
Share

Thanks @jchester07

in your code:

 NitroSlider.value = $$anonymous$$athf.Lerp(nsStartValue, ns$$anonymous$$axValue, 1 - (NitroSpeed/(origNitroSpeed - TempSpeed)))

my nsStartValue and ns$$anonymous$$axValue both has same value (`speed * 2f`). or other mean, NitroSpeed is equal to speed * 2f.

and above code don't work.

avatar image jchester07 behdadsoft · Nov 06, 2017 at 12:48 AM 0
Share

nsStartValue is the NitroSlider which would usually start at 0 if nitro is fully used.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by JaredHD · Nov 03, 2017 at 09:04 PM

Hello, So I made a short example script below:


 using UnityEngine;
 using UnityEngine.UI;
 
 public class Nitro : MonoBehaviour
 {
     //Actual Nitro
     private float nitro = 5;
 
     //Attach slider in the editor
     public Slider nitroSlider;
 
     private void Update()
     {
         //If space is pressed and we have nitro
         if (nitro > 0 && Input.GetKeyDown(KeyCode.Space))
         {
             nitro -= Time.deltaTime;
         }
         else //Else refil over time
         {
             nitro += Time.deltaTime;
         }
 
         //Clamp it between 0 and 5
         nitro = Mathf.Clamp(nitro, 0, 5);
         nitroSlider.value = nitro;
     }
 }

Note: If you need anything explained please ask.



Update

I saw that you were looking at lerping, so I decided to have some fun and update the above example:
 using System;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Nitro : MonoBehaviour
 {
     [Header("Lerp the Value or Clamp it.")]
     [SerializeField]
     bool lerp = true;
 
     //Actual Nitro value, start is 0
     private float nitro = 0;
 
     //LERP
 
     //Show in the inspector
     [SerializeField]
     float nitroMin = 0;
     [SerializeField]
     float nitroMax = 5;
 
     [Range(0,1)]
     [SerializeField]
     float nitroSpeedRegen = 0.1f;
     float lerpSpeed;
 
     // /LERP
 
     //Attach slider in the editor
     public Slider nitroSlider;
 
     private void Update()
     {
         if (lerp && !Input.GetKey(KeyCode.Space))
         {
             LerpNitro();
         }
         else if (!Input.GetKey(KeyCode.Space))
         {
             ClampNitro();
         }
 
         //Check if we are using nitro
         activateNitro();
     }
 
     private void activateNitro()
     {
         //If space is pressed and we have nitro
         if (nitro > 0 && Input.GetKey(KeyCode.Space))
         {
             //Decrease Nitro
             nitro -= Time.deltaTime;
             //Update the slider
             nitroSlider.value= nitro;
             //Work out new lerp speed
             lerpSpeed = nitro/nitroMax;
         }
     }
 
     private void ClampNitro()
     {
         //Clamp it between 0 and 5
         nitro = Mathf.Clamp(nitro, 0, 5);
         nitroSlider.value = nitro;
     }
 
     void LerpNitro()
     {
         //Calculations
         lerpSpeed += nitroSpeedRegen * Time.deltaTime;
         nitro = Mathf.Lerp(nitroMin, nitroMax, lerpSpeed);
 
         //Apply the value to the slider
         nitroSlider.value= nitro;
 
         //Prevent the speed from going too fast
         Mathf.Clamp(lerpSpeed, 0, 1);
     }
 }



Note: the above includes both the clamping and the lerping. Attach this script to any gameobject and assign the slider.


Thereafter simply pressing space will decrease the value, and not doing anything will increase the value. Yet again, if you have any questions please ask


Some night time reading: lerping, Clamping, SerializeField if you want to know what these functions do.

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 behdadsoft · Nov 05, 2017 at 08:56 PM 0
Share

Thanks @ JaredHD.

if there is any question, i ask you. :)

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

73 People are following this question.

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

Related Questions

GUI Slider sets a value but the value appears to be reset by the end of OnGUI() 1 Answer

How I can change this command to my Keyboard? 1 Answer

Rotation with a slider 2 Answers

Horizontal Slider 1 Answer

How to Center Horizontal Thumb Slider? 0 Answers


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