How to smootlhy change values in amout of time?

I know it can be kinda easy, but...

I'm trying to make my character run if I'll press "Fire1". So the speed of my character should smoothly change into higher value, from 6 to 15.

I tried to do it by Animation Curves, but it seems like i can't apply 2 other animations to 1 object at the same time (like for example run and crouch).

So... is it any other good way, that allowys me to take control above evertything (time, when value changes)? Och, and please comment everything, cause I'm new in programming.


Do you know about Time.deltaTime? (the time that has elapsed since last update)

var speedMin = 6.0;
var speedMax = 15.0;
var accelerateTime = 3.0;
private var currentSpeed = 0.0;
private var timePassed = 0.0;

function FixedUpdate()
   if (Input.GetAxis("Fire1"))
      timePassed += Time.deltaTime;
      currentSpeed = Mathf.Lerp(speedMin, speedMax, timePassed/accelerateTime);


The public vars can be adjusted in the editor, you can adjust "accelerateTime", and the character should speed up faster.

How you apply this "currentSpeed" value depends on how you are moving your character. If using a characterController, this link may help

That does not work. I have code like that:

if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0){
        timePassedSpeed += Time.deltaTime;
        currentSpeed = Mathf.Lerp(speedMin, speedMax, timePassedSpeed/accelerateTime);

    timePassedSpeed += Time.deltaTime;
    currentSpeed = Mathf.Lerp(speedMax, speedMin, timePassedSpeed/accelerateTime);

So i simply want to accelerate, when any arrow key is down, and decrease speed, when it's not. Its smooth, when variable "currentSpeed" do not equals speedMin or speedMax. When it equals that and I try to accelearate or decrease speed, variable changes immadietly from 6 to 15, or from 15 to 6.

what you need is Mathf.SmoothDamp function.
Then it shifts from var A to var B through time.
there is a tricky thing with this function using ref yVelocity to (eventually) smooth the curve between A and B…

so first declare :
private float (or var) y Velocity = 0;
public float transTime(or whatever) = 1f; (fixed time to go from a to b, I guess we can use a function too, but should be sufficient)

then in your Update:
if (Input.GetKey (KeyCode.UpArrow)) {
currentSpeed = Mathf.SmoothDamp (speedMin, speedMax, ref yVelocity, transTime);

I hope it helps…