- Home /
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
Answer by PommPoirAbricot · Jun 19, 2014 at 07:55 AM
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;
}
}
Answer by HarshadK · Jun 19, 2014 at 08:23 AM
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. ;-)
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Still doing Running Animation? 1 Answer
Networking Synchronize Problem. 0 Answers