Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kelvik · Apr 19, 2014 at 12:12 AM · c#togglerunning

[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;
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Apr 19, 2014 at 12:14 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
      
            }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kelvik · Apr 19, 2014 at 03:30 AM 0
Share

Does not seem to be working, its still just making me run when the shift key is pressed

avatar image SteelArrow21 · Apr 19, 2014 at 03:45 AM 0
Share

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;
avatar image Kelvik · Apr 19, 2014 at 03:50 AM 0
Share

Still nothing, now even when shift is pressed the character does not run at all.

avatar image SteelArrow21 · Apr 19, 2014 at 04:00 AM 0
Share
 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;
      }
 }
 
 }
avatar image Kelvik · Apr 19, 2014 at 04:15 AM 0
Share

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;
         }
     }
 }
Show more comments
avatar image
0

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;
         }   
 }    
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Igorotak · Apr 02, 2018 at 09:55 AM 0
Share

how are you able to set isWalking back to true;

avatar image TheQiwiBoy · Apr 18, 2020 at 10:37 PM 0
Share

THANK YOU!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges