- Home /
Question by
Mactadilis · Jul 28, 2019 at 02:58 PM ·
c#floatdecrease
Sliding speed decreases over time?
I wan to make my character slide under some conditions, and I want to decrease player speed over, lets say 3 seconds, to 0. I have code that proves that i press the right button, but i have no idea how to make float decrease over time constantly until it reachese 0. Delta time doesent work constatly for some reason.
public float playerSpeed = 5.2f;
private void Slide()
{
if (Input.GetKey(KeyCode.Space) && (isRunning == true || isSprinting == 1))
{
isSliding = true;
}
else
{
isSliding = false;
}
}
Comment
Answer by blueshark- · Jul 28, 2019 at 07:43 PM
Here's the code that would make the stamina go down depending on the frames and the rate. If it doesn't work try calling the function in Update() perhaps.
float maxSlideTime = 3.0f; // Max sliding speed
float slidingTime; // Current time the player is sliding
float slidingRate = 1.0f; // The rate the slidingTime decreases at
if(isSliding && slidingTime > 0.0f)
{
slidingTime -= slidingRate * Time.deltaTime;
}
else if(!isSliding)
{
slidingTime = maxSlideTime; // Insta recharge
slidingRate += slidingRate * Time.deltaTime // Increasing back depending on rate
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
JsonUtility serializes floats with way too many digits 2 Answers