Shift to sprint? (Fresh beginner)

I followed a basic movement and first person-camera video by Brackeys. I’ve gotten here so far, and I want to be able to make a script that speeds up the player when they hold shift. I’ve already got some other scripts that I need to use.

Here’s the “code” that I attempted to make myself based on the script I followed.

public class Sprint : MonoBehaviour
{

    public CharacterController controller;

    //public float speed;
    public float sprintZoom = 5f;
    public float sprintSpeed = 1.3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;

    void Update()
    {
//        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

 //       if (Input.GetButtonDown("Jump") && isGrounded)
        {
 //           controller.Move(speed * sprintSpeed);
        }
    }
}

I turned the parts that I wasn’t very sure on into comments.

Here’s the script that Brackeys made:

public class PlayerMovement : MonoBehaviour
{

    public CharacterController controller;

    public float speed = 12f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if(isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);

        if(Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        velocity.y += gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);
    }
}

Once again, I installed Unity just yesterday, so please try to keep it easy! Thank you!

I’ll tell you the rough steps, see what you can do with it.

See where you have that class level variable called speed and how you immediately use it in line 31 above?

Instead of doing that, do these steps:

  • remove the speed variable in the class
  • make two new variables: walkSpeed and runSpeed
  • on line 31 above, BEFORE you do the .Move call, do this:
    — make a temp float called speed
    — assign walkSpeed to speed
    — check the condition you want (in this case the spacebar) and if “run” is being commanded, assign runSpeed to speed.
  • assign reasonable values to the two new speed variables, and make sure they’re okay in the inspector

Now just allow line 31 above to continue, and it will use that new temp variable called speed, which if you did your input logic correctly should either have the value of walkSpeed or runSpeed in it, appropriately!

Does that make sense?