Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 watchermagic3 · Aug 16, 2018 at 09:59 PM · runwalksprintdouble-tap

[SOLVED] Walk, Run and Sprint with single key

I'm trying to create a character controller where I can walk forward using W, and then by pressing another key (say Q,) I can switch to running, and press it again to start walking again. I'd also like to be able to double-tap for sprinting, then press to return to walk/run again. I have forward motion and walk and run by holding Q down, but how do I change it to register double-tapping and changing states with only a single press? here's my code:

  private void Update ()
     {
         //walk
         if (Input.GetKey(KeyCode.W))
         {
             humanPlayerRB.velocity = transform.forward * walkSpeed;
         }
 
         //run
         if (Input.GetKey(KeyCode.W) && (Input.GetKey(KeyCode.Q))) //I've tried using GetKeyDown here, to no effect
         {
             humanPlayerRB.velocity = transform.forward * runSpeed;
         }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Trevdevs · Aug 16, 2018 at 10:28 PM

You're question is confusing and doesn't provide enough details but if I understand what you're trying to do here are some code snippets

 void Update()
 {
     bool sprinting = false;
 
     if(Input.GetKey("WalkKey"))
     {
         if(sprinting)
         {
             //sprint
         }
         else
         {
             //walk
         }
     }
 
     if(Input.GetKeyDown("RunKey"))
     {
         sprinting = !sprinting; //This acts as a toggle statement setting the bool to not what is currently is 
     }
 }
Comment
Add comment · Show 6 · 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 watchermagic3 · Aug 17, 2018 at 07:10 PM 0
Share

Thank you! Unfortunately nothing is happening. I can walk as normal, but pressing the sprint key does nothing. I tried using a Debug.Log that outputs the speed to see if I just couldn't tell the difference, but it looks like the sprint method is never called.

 private void Update ()
     {
         bool sprinting = false;
         
         //walk
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
         {
             if(sprinting)
             {
                 humanPlayerRB.velocity = transform.forward * sprintSpeed;
                 Debug.Log("Speed = " + sprintSpeed); //never enters the consol
             }
             else
             {
                 humanPlayerRB.velocity = transform.forward * walkSpeed;
                 Debug.Log("Speed = " + walkSpeed);
             }
         }
 
         //toggles sprinting
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q))
         {
             sprinting = !sprinting;
         }
 }
avatar image RustyCrow watchermagic3 · Aug 17, 2018 at 11:14 PM 0
Share

I am just glancing over this, getting late here :P BUT

    //toggles sprinting
              if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q)) // <-- this is in update i think maybe this triggering multiple times ?
              {
                   Debug.Log("Sprint status = " + sprinting );
                  sprinting = !sprinting;
              }

Get key up should only trigger once.

avatar image watchermagic3 RustyCrow · Aug 17, 2018 at 11:50 PM 0
Share

Thanks for stopping by, I need it ;) The debug.log only outputs false; sprinting never ends up equaling true; I don't think it's triggering multiple times, whenever I press it it only outputs one message.

EDIT: Wait! Putting debug.log after sprinting = !sprinting only outputs true values. So... well, I don't know, but I'll keep experimenting.

Show more comments
avatar image Trevdevs watchermagic3 · Aug 18, 2018 at 01:40 AM 0
Share

Whoops I made a mistake in my code that bool should be global not local. Its creating a new variable every frame is setting it to false do this...

 public class $$anonymous$$yScript : $$anonymous$$onoBehaviour
 {
    public int walkSpeed;
    public int sprintSpeed;
 
    /Add me
   private bool sprinting = false;
 
    void Update()
    {
     ...
     }
 }
avatar image watchermagic3 · Aug 18, 2018 at 06:37 PM 0
Share

Oh that makes sense! Thank you, it's working now :) Here's my code for any passers-by

 [SerializeField] public int walkSpeed;
 [SerializeField] public int sprintSpeed; 
 
 Rigidbody humanPlayerRB;
 
 private bool sprinting = false;
 
     private void Start()
     {
         humanPlayerRB = GetComponent<Rigidbody>();
     }
 
 // Update is called once per frame
     private void Update ()
     {
 
         //walk
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
         {
             if(sprinting)
             {
                 humanPlayerRB.velocity = transform.forward * sprintSpeed;
                 Debug.Log("Sprinting");
             }
             else
             {
                 humanPlayerRB.velocity = transform.forward * walkSpeed;
                 Debug.Log("Walking");
             }
         }
 
         //toggles sprinting
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q))
         {
             //This acts as a toggle statement setting the bool to not what is currently is 
             sprinting = !sprinting;
         }
 }

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

89 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 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 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 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

Double Tap to Run 2 Answers

Slow down and play walk animation on left shift? 3 Answers

Animation problem (noob) 1 Answer

Character controller with key click to move forward 0 Answers

How to make different walk sounds 2 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