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 /
  • Help Room /
avatar image
0
Question by TheCallipo · Sep 22, 2016 at 12:37 PM · c#speedfloatifsprint

currentSpeed doesnt change when pressing Shift (C#)

Hello. I'm fairly new to programming and game making so I'm setting up my first character controller.

What I have is a Player who can walk along the x and z axis. I have 3 floats in my script named "walkSpeed", "sprintSpeed" and "currentSpeed" and I would set them up in the Inspector. ie: walkSpeed = 5 and sprintSpeed = 10.

I want to use the walkSpeed as the normal Speed and the sprintSpeed only if the left Shift key is pressed.

What I got for now:

     public float walkSpeed;
     public float sprintSpeed;
     public float currentSpeed;
 
     void Update () {
 
         if (Input.GetKeyDown("left shift"))
             currentSpeed = sprintSpeed;
         else
             currentSpeed = walkSpeed;
 
         float verMov = Input.GetAxis("Vertical") * currentSpeed;
         float horMov = Input.GetAxis("Horizontal") * currentSpeed;
 
         verMov *= Time.deltaTime;
         horMov *= Time.deltaTime;
 
         transform.Translate(horMov, 0, verMov);
 
     }

In my understanding the currentSpeed should change to 10 IF the left shift key is pressed and ELSE the currentSpeed should stay 5. but when i start the game the currentSpeed stays 5 even when im pressing left shift.

I would love to hear your tips regarding this code. And best you can do is finding a way to let this script work and not posting your script and saying "replace this with yours it works 100%".

Thank you :)

EDIT: when I revert currentSpeed = sprintSpeed and currentSpeed = walkSpeedto sprintSpeed = currentSpeed and walkSpeed = currentSpeed the walkSpeed goes instantly to 0 and if I press the shift key the sprintSpeed changes from 10 to 0.

That shows that the code recognizes that the key is pressed.

Comment
Add comment · Show 2
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 Sergio7888 · Sep 22, 2016 at 04:21 PM 1
Share

Use Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift) not Input.Get$$anonymous$$eyDown("left shift")

avatar image TheCallipo Sergio7888 · Sep 22, 2016 at 05:19 PM 0
Share

This hasn't solved my problem since ($$anonymous$$eyCode.LeftShift) is referring to the same key as ("left shift") . Both can be used as far as I know.

$$anonymous$$y console doesnt show any error.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by NoseKills · Sep 22, 2016 at 05:50 PM

Did you check what GetKeyDown returns and when ?

Returns true during the frame the user starts pressing down the key identified by name.

So (if all else is OK) the speed will be sprintSpeed for the duration of 1 frame and go back to walkSpeed the next (unless you managed to release and press the button again in about a 60th of a second). Use lnput.GetKey() instead. It returns true every frame the key is being pressed.

And yes, you should always use KeyCode.LeftShift instead of a hardcoded string because

A) you can't typo a variable name without getting a compiler error, but it's easy to accidentally write "left shift " and not notice the space at the end

B) if a Unity update should change the value of that keycode, your code would break, but code using the variable would not.

Comment
Add comment · Show 1 · 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 TheCallipo · Sep 22, 2016 at 06:22 PM 0
Share

a few moments before your answer I indeed noticed that sometimes the currentSpeed turns 10 for a couple of milliseconds. I should definitley read more of the documentations.

ah okay so "left shift" isnt a actual c# variable but just a string unity made up?

Thank you very much for clarification! :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot convert float to int? 1 Answer

How to revert speed back to normal after changing with Key. 1 Answer

Something is wrong with my max. speed code 0 Answers

WaitForSeconds don't work with variables from another scripts 2 Answers

C# float variable isnt behaving like its value 1 Answer


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