- Home /
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.
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?
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)))
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.
nsStartValue is the NitroSlider which would usually start at 0 if nitro is fully used.
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.
Thanks @ JaredHD.
if there is any question, i ask you. :)
Your answer
Follow this Question
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