Change Animations When a Key is Repeatedly Pressed

I am creating a 3D in which a player can ride a horse. I want the horse to increase in gait when the vertical positive input is pressed more than once, and decrease when the vertical negative input is pressed, and after the animation has run for a certain amount of time, it should time out. I think I have the last part pretty well figured out, but I just don’t know how to do the rest.

I tried using a float and increasing or decreasing it when the player presses the vertical input, and changing the animations with that, but it didn’t work.

Any help would be appreciated!
Thanks in advance!

I’m not sure I understand correctly the way this works. You press ‘up’ to increase Speed, ‘down’ to decrease speed, and want to time out (i.e. you have controls to speed up and slow down, and you have a speed decay)? So, if I press “up” three times in rapid succession, and then do nothing, what will happen? As I understand it, the hose Speeds up, then gradually slows down to a stop? Is that correct?

If that is what you want, the simplest way would be to to add a larger fixed speed increase with every ‘up’, subtract a larger speed decrese with every down, and subtract a smaller speed every update (use Time.deltaTime for this). when speed goes below Zero set it to Zero.

Yes, that is what I meant. I didn’t realize that I was so vague. I’m sorry about that.

How would I go about decreasing the variable using Time.deltaTime?

Each time your Update() method is invoked, you can Access the variable Time.deltaTime. It holds the sliver of time that this frame is occupying, usually a small number, like 1/65 - that’s because it tells you how much of a fraction of a second this frame has taken.

You can use this number to continually increase or decrease something so it matches a continual value per second, and that is what you can use here. Imagine that you want to continually decrease the horse’s speed by 2.0f over a second.

Each time through Update(), simply subtract 2.0 multiplied by the time slice or

horseSpeed = horseSpeed - 2.0f * Time.deltaTime;

Then make sure that the Speed ist negative, or the horse will start accelerating backwards:

if (horseSpeed < 0f){
   horseSpeed = 0f;
}

As you can see, it’s all pretty straightforward.

That works great, but when it increases, it increases with decimal values.

Also, when the float reaches a value of 2, it doesn’t play the animations. Anything I’m doing wrong?

public float Gait = 0;
void Update{
Gait = Gait - 2.0f * Time.deltaTime;
            if(Gait <= 0f){
                Gait = 0f;
            }

if(Input.GetAxis("Vertical") > 0f){
                        if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
                        {
                            if(Gait < 4f){
                                Gait = Gait + 1.0f;
                            }else if(Gait >= 4.0f){
                                Gait = 4f;
                            }
                            //Debug.Log("Gait Is: "+Gait);
                        }else if( Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S)){
                            if(Gait < 0f){
                                Gait = Gait - 1.0f;
                            }else if(Gait <= 0f){
                                Gait = 0f;
                            }
                        }
            }

            float vertical = Input.GetAxis("Vertical");
            float horizontal = Input.GetAxis("Horizontal");
            transform.Rotate(0, Input.GetAxis("Horizontal") * 2, 0);
            transform.position += transform.forward * 7 * vertical * Time.deltaTime;
            blue.SetFloat("Gait", Gait);
}

For those who have this problem in the future, I found this worked:

float counter = 0;

void Update()
    {
            if(Input.GetAxis("Vertical") > 0f){
                        if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
                        {
                            if(Gait < 4f){
                                Gait = Gait + 1.0f;
                            }else if(Gait >= 4.0f){
                                Gait = 4f;
                            }
                        }
            }else if(Input.GetAxis("Vertical") < 0f ){
                if( Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S)){
                            if(Gait > 0f){
                                Gait = Gait - 1.0f;
                            }else if(Gait <= 0f){
                                Gait = 0f;
                            }
                }
            }else{
                //Increment Counter
                counter += Time.deltaTime;

//IF IT HAS BEEN 5 SECONDS
                if (counter >= 5)
                {  
                    Gait = Gait - 1.0f;
                    if(Gait <= 0f){
                        Gait = 0f;
                    }
                    //RESET Counter
                    counter = 0;
                }
               
            }

           
            transform.Rotate(0, Input.GetAxis("Horizontal") * 2, 0);
           
            speed = Gait + 3;
            blue.SetFloat("Gait", Gait);
            transform.position += transform.forward * speed * Gait * Time.deltaTime;
       
    }