How to stop character from running

Hey guys, I’m new to C# scripting and was having an issue making my character move. I was messing around with a script I found on the Unity API to add a run function. However, when I press Shift my character won’t stop running. I’ve spent a few hours today trying to find an answer but I can’t seem to find any. I’m sure it’s something simple that I’ve missed though. Here is my code

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

	//Movement Variables
	public float walkSpeed = 1.0f;
	public float jumpSpeed = 3.0f;
	public float runSpeed = 2.0f; //Speed while holding Left Shift
	public float gravity = 20f;

	private Vector3 moveDirection = Vector3.zero; //Moves character along the X axis
	
	void Update () {
		CharacterController controller = GetComponent<CharacterController>(); //Forces script to use CC

		//WASD&Sprint Movement
		if (controller.isGrounded) {
			moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);
			moveDirection *=walkSpeed;
			if(Input.GetButton("Jump"))
				moveDirection.y = jumpSpeed;
		    if (Input.GetButton ("Run")) 
			    walkSpeed = runSpeed;
		} 
	    moveDirection.y -= gravity * Time.deltaTime;
	    controller.Move(moveDirection * Time.deltaTime);
	  
	}
}

Thanks for any help guys

Perharps doing something like that ?


//WASD&Sprint Movement
        if (controller.isGrounded)
        {
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= walkSpeed;
            if (Input.GetButton("Jump"))
                moveDirection.y = jumpSpeed;
            if (Input.GetButton("Run"))
                walkSpeed = runSpeed;
            else
            {
                walkSpeed = 0;
            }
        }

Set something like:

if (Input.GetButtonDown("Run")){
    walkSpeed = runSpeed;
} else if(Input.GetButtonUp("Run"))
{
    walkSpeed = // set walkspeed to its normal value;
}

On a side note, or you can chop off the legs of your character. :wink: