How can you increase a float to certain number and stop, while it is being used.
So I have a blend tree that has a variable speed. But I need to increase it smoothly from walk speed to run speed while I'm at the same time using it to translate the object forward, this is required so the animations won't look so instant and choppy any help? I tried this method myself that the script shows, still doesn't work does it instantly also I tried placing the Mathf.Lerp straight into the line where the current speed is right now didn't work. I have also tried Mathf.SmoothStep didn't work either.
Script:
void Movement(float v, float h)
{
if (h != 0f || v != 0f)
{
Rotate(v, h);
if (!Input.GetButton("Run") || Input.GetButtonUp("Run"))
{
if (ran)
currentSpeed = Mathf.Lerp(runSpeed, walkSpeed, 3f);
if (currentSpeed == walkSpeed)
ran = false;
playerAnimator.SetFloat("speed", 1);
transform.Translate(Vector3.forward * currentSpeed * Time.deltaTime);
} else if (Input.GetButton("Run"))
{
playerAnimator.SetFloat("speed", 3);
currentSpeed = Mathf.Lerp(walkSpeed, runSpeed, 3f);
transform.Translate(Vector3.forward * currentSpeed * Time.deltaTime);
}
if (Input.GetButton("Run"))
{
ran = true;
}
idle = false;
}
else
{
playerAnimator.SetFloat("speed", 0);
idle = true;
}
}
Answer by CitizenVeen · Oct 23, 2018 at 02:36 PM
If I understand you correctly all you want to archieve is a steady rise of a var, called currentspeed, between its minimum, called walkspeed, to its maximum, called runspeed?
You are using Mathf.Lerp wrong. That equation gives you a value between its minimum a and its maximum b, based on what your third value c is. Example:
Mathf.Lerp(0,200,0);
// gives you 0
Mathf.Lerp(0,200,0.5f);
// will give you 100
Mathf.Lerp(0,200,1);
// will give you 200
As you assign 3f to your third value, mathf.lerp will allways give you the same value. What you need to do is steadely increase the value of c, the following code should increase the currentSpeed between your minimum, walkSpeed to your maximum, runSpeed in 2 seconds from the moment the scene starts, then stay at the runSpeed.
float increaser;
float secondsTillMaxSpeed = 2;
void Update()
{
increaser = Mathf.Clamp01(Time.time / secondsTillMaxSpeed);
currentSpeed = mathf.Lerp(walkSpeed, runSpeed, increaser);
}
I think it really helps to understand what mathemathical equasions actually do when using them, so when they don't perform how you expect them, you don't get baffled as to what to do about it. This video: https://www.youtube.com/watch?v=beWwwxn7dLQ&list=PLFt_AvWsXl0fnA91TcmkRyhhixX9CO3Lw∈dex=16 gives you an excellent explanation as to what Mathf.Lerp does in very much the same situation as you are trying to use it.
Thank you very much! And thank you for the video, It seems like I didn't quite understand lerp correctly now I know what it does exactly :)
Answer by no00ob · Oct 24, 2018 at 07:34 PM
(Edit: Umm... how would I decrease it? I can't get that to work now...) I realized that the animations speed value was different to my walk speed so I was doing the "Blending" between walk and run for nothing, but that didn't work either to begin with so thanks still as this helped a lot.
Your answer
Follow this Question
Related Questions
Lerp between two int vaules smoothly 4 Answers
Smooth movement for a 3rd person camera 2 Answers
Smooth camera to target Y-height 3 Answers
Play animation controlled 0 Answers
Interpolated movement in a circle 1 Answer