Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How can I make it so if I hold down the button to move I keep moving?

Discussion in 'Scripting' started by AresGamez, Jan 22, 2021.

  1. AresGamez

    AresGamez

    Joined:
    Jan 20, 2021
    Posts:
    5
    Code (CSharp):
    1. if(Input.GetKeyDown(KeyCode.Space))
    2.             rb.AddForce(Vector3.up * 100f);
    3.         if (Input.GetKeyDown(KeyCode.W))
    4.             rb.AddForce(Vector3.forward * 80f);
    5.         if(Input.GetKeyDown(KeyCode.D))
    6.             rb.AddForce(Vector3.right * 80f);
    7.         if (Input.GetKeyDown(KeyCode.A))
    8.             rb.AddForce(Vector3.left * 80f)
    This is the code
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Try switching all your GetKeyDown calls to just GetKey.

    GetKeyDown is only true on the frame the key is pressed down. GetKey is true for every frame the key is held down.
     
  3. AresGamez

    AresGamez

    Joined:
    Jan 20, 2021
    Posts:
    5
    Thank You!Also,how can I make it so that there is a cooldown for the jumping.So if you jump once you have a cooldown.
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    I get the impression you haven't done any of your own searching - these are super basic questions, and I don't wanna cut you down for asking for help - but you really need to search for quite some time to solve a vast majority of the beginner issues like this, as they have been answered in about a million different ways a million different times.

    https://lmgtfy.app/?q=unity+create+a+delay
     
    Joe-Censored likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Do you just want to use a timer? If using a timer you have the potential for double jumps if you're still in the air when the timer runs out.

    If that is what you want, you'd switch back to GetKeyDown. Then set a float variable with how much time you want to run down. Every update you subtract Time.deltaTime from that timer variable. When you check for whether to jump, you check if that timer variable is at or below 0f. There is more than one way of accomplishing the same thing, but the above will work.

    Also as a side note, you should place any AddForce calls in FixedUpdate(). FixedUpdate is called immediately before the physics update. But FixedUpdate isn't called every frame. So you don't want to check for input with GetKeyDown in FixedUpdate, because you will sometimes miss inputs. Since GetKey is true every frame it is held down, it isn't that bad to check in FixedUpdate. But generally the approach to player input triggering physics based movement should be you check for the input in regular old Update(), then act on the input in FixedUpdate with your physics force calls.
     
    MD_Reptile likes this.
  6. ashkanaral

    ashkanaral

    Joined:
    Sep 3, 2018
    Posts:
    22
    Thank you. Joe-Censored. You are right! I had that problem because I had another script that used GetKeyDown Now I changed it and it is in sync with the other script!