Need some help from pros

This is getting really frustrating , what i want is for my fps controller to move faster while holding shift but only while holding w aswell , i want to use the script i made because i need it … so whats wrong with it?

function Update ()
{


   if(Input.GetButtonDown("forward") && Input.GetButtonDown("sprint") )
   {
      isSprinting = true;
      maxForwardSpeed = 20;
      
      }
      else
      {
      
     
      if(Input.GetButtonUp("forward") && Input.GetButtonUp("sprint") )
      {
           isSprinting = false;
           maxForwardSpeed = 6;
           }
      }


}

Take out the else. This is how it should look like:

if(Input.GetButtonDown("forward") && Input.GetButtonDown("sprint") )
   {
      isSprinting = true;
      maxForwardSpeed = 20;
 
   }
if(Input.GetButtonUp("forward") && Input.GetButtonUp("sprint") )
      {
           isSprinting = false;
           maxForwardSpeed = 6;
      }

Put this in your Update function :slight_smile:

You shouldn’t use Input.GetButtonDown, it returns true on the first frame you press button on, and then as frames progress, holding down the button, GetButtonDown will return false.

This means that, if you try to hold shift and then walk forward, it will not actually work because Input.GetButtonDown("sprint") returned true the moment you pressed shift, but then it’s false. The only way for your code to work is if you press shift and forward at the same time :slight_smile:

So use Input.GetButton instead, which will return true while the button is being pressed. More info on Input.GetKey and Input.GetButton families can be found here.

Your first check should be:

if(Input.GetButton("sprint") && Input.GetButtonDown("forward"))
{
   isSprinting = true;
   maxForwardSpeed = 20; 
}

This means: If the shift button is held down && at the moment I press forward, do the stuff below:…

Looking at your code a bit further:

if(Input.GetButtonUp("forward") && Input.GetButtonUp("sprint") )
{
  isSprinting = false;
  maxForwardSpeed = 6;
}

Input.GetButtonUp returns true the moment you let go of the key (key goes up) - so for your previous code to work right, you have to let go of shift and w at the same time :slight_smile:

Armed with the information I just gave you, you should be able to fix it yourself :slight_smile:

If you’re lazy, here’s how it goes, you basically got more than one way of doing this. You could do (Pseudo code):

   if (both spring and forward are being 'held')
   {
     // do your stuff
   }
   else 
   {
     // do the other stuff
   } 

Or:

   if (spring is being 'held' && forward has just been pressed)
   {
     // do your stuff
   }
   else if (spring is NOT being 'held')
   {
     // do the other stuff
   }