Home > database >  Continue executing "GetKeyUp" command
Continue executing "GetKeyUp" command

Time:02-22

So I figured out how my code's going to work, but I don't know what to write. I want to execute a code that continuously subtracts a value after a certain key is released, in my case, left shift. I figured I should use "GetKeyUp" command, but it only executes once so it didn't continue subtracting. Here's some of my code:

//sprintIncrease is the value on how many it subtracts or adds, to give the impression of the value slowly increasing or decreasing

//defaultSpeed is the normal walk speed, but this is the value where i can change it indirectly
  
//Executes when shift is released, to slowly go back to default speed (I need help in this)
        if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            walkSpeed -= sprintIncrease * Time.deltaTime;
        }

//To stop Walk Speed from being slower than the Default Speed while getting subtracted
        if (walkSpeed < defaultSpeed)
        {
            walkSpeed = defaultSpeed;
        }
        

CodePudding user response:

You could simply check if the key is not currently pressed using Input.GetKey and check if it is false like e.g.

if (!Input.GetKey(KeyCode.LeftShift))
{
    walkSpeed -= sprintIncrease * Time.deltaTime;

    //To stop Walk Speed from being slower than the Default Speed while getting subtracted
    if (walkSpeed < defaultSpeed)
    {
        walkSpeed = defaultSpeed;
    }
}


    
  • Related