- Home /
[C#]Toggle Run on/off
Hello, I want to make my current run script which currently makes you run when the left or right shit key is pressed, but I want to change that to being you have to press the shift key to begin running, then hit it again to stop. Code below, thanks in advance.
using UnityEngine;
using System.Collections;
public class charRunCrouch : MonoBehaviour
{
public float walkSpeed = 7; // regular speed
public float crchSpeed = 3; // crouching speed
public float runSpeed = 15; // run speed
bool running = false;
private CharacterMotor chMotor;
private Transform tr;
private float dist; // distance to ground
// Use this for initialization
void Start ()
{
chMotor = GetComponent<CharacterMotor>();
tr = transform;
CharacterController ch = GetComponent<CharacterController>();
dist = ch.height/2; // calculate distance to groundw
}
// Update is called once per frame
void FixedUpdate ()
{
float vScale = 1.0f;
float speed = walkSpeed;
if ((Input.GetKey("left shift") || Input.GetKey("right shift")) && chMotor.grounded)
{
speed = runSpeed;
}
if (Input.GetKey("left ctrl") || Input.GetKey("right ctrl"))
{ // press C to crouch
vScale = 0.5f;
speed = crchSpeed; // slow down when crouching
}
chMotor.movement.maxForwardSpeed = speed; // set max speed
float ultScale = tr.localScale.y; // crouch/stand up smoothly
Vector3 tmpScale = tr.localScale;
Vector3 tmpPosition = tr.position;
tmpScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5 * Time.deltaTime);
tr.localScale = tmpScale;
tmpPosition.y += dist * (tr.localScale.y - ultScale); // fix vertical position
tr.position = tmpPosition;
}
}
Toggle questions have been asked many, many times. A search on UA/Google should show any number of code examples, tuts/vids, etc.
Try a new boolean - isRunning
Add something like
if(Input.Get$$anonymous$$eyDown((Shift))
isRunning = !isRunning
then you can use isRunning to know if the state is on/off and adjust speed
Answer by SteelArrow21 · Apr 19, 2014 at 12:19 AM
Try The Following:
if ((Input.GetKeyDown("left shift") || Input.GetKeyDown("right shift")) && chMotor.grounded)
{
if(speed = runSpeed) //If The player is running and shift is pressed, then the player will walk.
speed = walkSpeed;
if(speed != runSpeed) //If The player is walking and shift is pressed, then the player will run.
speed = runSpeed;
}
Does not seem to be working, its still just making me run when the shift key is pressed
Hmm. Try this:
if ((Input.Get$$anonymous$$eyDown("left shift") || Input.Get$$anonymous$$eyDown("right shift")) && ch$$anonymous$$otor.grounded && speed == runSpeed)
speed = walkSpeed;
if ((Input.Get$$anonymous$$eyDown("left shift") || Input.Get$$anonymous$$eyDown("right shift")) && ch$$anonymous$$otor.grounded && speed == walkSpeed)
speed = runSpeed;
Still nothing, now even when shift is pressed the character does not run at all.
private bool isRunning = false;
void Update(){
if((Input.Get$$anonymous$$eyDown("right shift") || (Input.Get$$anonymous$$eyDown("left shift")) && ch$$anonymous$$otor.grounded)
Run();
}
void Run(){
isRuning = !isRunning;
if(isRunning == false)
{
speed = walkSpeed;
}
else if(isRunning == true)
{
speed = runSpeed;
}
}
}
Still nothing, not even running when key is pressed
public float walkSpeed = 7; // regular speed
public float crchSpeed = 3; // crouching speed
public float runSpeed = 15; // run speed
private bool isRunning = false;
private Character$$anonymous$$otor ch$$anonymous$$otor;
private Transform tr;
private float dist; // distance to ground
// Use this for initialization
void Start ()
{
ch$$anonymous$$otor = GetComponent<Character$$anonymous$$otor>();
tr = transform;
CharacterController ch = GetComponent<CharacterController>();
dist = ch.height/2; // calculate distance to groundw
}
// Update is called once per frame
void Update ()
{
float vScale = 1.0f;
float speed = walkSpeed;
if ((Input.Get$$anonymous$$ey("left shift") || Input.Get$$anonymous$$ey("right shift")) && ch$$anonymous$$otor.grounded)
{
Run();
}
if (Input.Get$$anonymous$$ey("left ctrl") || Input.Get$$anonymous$$ey("right ctrl"))
{ // press C to crouch
vScale = 0.5f;
speed = crchSpeed; // slow down when crouching
}
ch$$anonymous$$otor.movement.maxForwardSpeed = speed; // set max speed
float ultScale = tr.localScale.y; // crouch/stand up smoothly
Vector3 tmpScale = tr.localScale;
Vector3 tmpPosition = tr.position;
tmpScale.y = $$anonymous$$athf.Lerp(tr.localScale.y, vScale, 5 * Time.deltaTime);
tr.localScale = tmpScale;
tmpPosition.y += dist * (tr.localScale.y - ultScale); // fix vertical position
tr.position = tmpPosition;
}
void Run(){
float speed = walkSpeed;
isRunning = !isRunning;
if (isRunning == false) {
speed = walkSpeed;
}
else
if(isRunning == true)
{
speed = runSpeed;
}
}
}
Answer by RatherGood · Oct 30, 2014 at 03:05 AM
This worked for me. Toggles an action on keydown. Have to release it to toggle again.
private bool isWalking = false;
private bool wasLShiftReleased = true;
void FixedUpdate ()
{
//toggle Walk/Run
if (Input.GetKey(KeyCode.LeftShift)) {
if (wasLShiftReleased == true) //did it change
{
isWalking = !isWalking;
wasLShiftReleased = false;
}
}
else
{
wasLShiftReleased = true;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613143039im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Toggle between scripts with a script? 2 Answers
Adding a listener to Toggle.onValueChanged via Script 3 Answers
How can i add a time limit to running? 1 Answer
Distribute terrain in zones 3 Answers