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 b05569 · Nov 11, 2021 at 07:03 AM · movementinputcontrolshift

Left Shift being cancelled by left control?

So I am just starting a 1st person platformer and I want to be able to sprint and slow down (kinda like crouch but no ducking). Both of these work individually, but in this script:

 void Update()
 {
     isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

     if(isGrounded && velocity.y < 0) {velocity.y = -2f;}

     if (Input.GetButton("Sprint") && isGrounded) 
     {
         speed = 36f;
     } else 
     {
         speed = 12f;
     };
     
     if (Input.GetButton("left control") && isGrounded) 
     {
         speed = 6f;
     } else 
     {
         speed = 12f;
     };

     float x = Input.GetAxis("Horizontal");
     float z = Input.GetAxis("Vertical");

     Vector3 move = transform.right * x + transform.forward * z;

     controller.Move(move * speed * Time.deltaTime);

     if (Input.GetButtonDown("Jump") && isGrounded)
     {
         velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
     };

     velocity.y += gravity * Time.deltaTime;
     controller.Move(velocity * Time.deltaTime);
 }

Shifting doesn't work - no speed increase occurs, but control does. It seems like the shift was being cancelled out. I did experiment with a bunch of stuff like GetKey, GetButtonDown, GetKeyDownetc. and nothing seems to be working. Any ideas?

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 Captain_Pineapple · Nov 11, 2021 at 12:23 PM

hey there,


at a point like this i can only give the advice: read your code step by step and write on a piece of paper what actually happens:

  1. you go into the function and speed has some unknown value

  2. you check if shift is pressed and you are grounded. if this is the case speed will be set to either 36 or 12.

  3. you check if control is pressed and you are grounded. if this is the case speed will be set to either 6 or 12.

So at this point you can see, no matter which result we have for point 2: we only ever can achive a speedvalue of 6 or 12 since you overwrite everything that was before.


So best way to solve this would be to have a speed multiplier for your crouch instead of setting this to a fixed value... something like speed *= 0.5 if the button is pressed.

Comment
Add comment · Show 3 · 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 b05569 · Nov 14, 2021 at 08:31 AM 0
Share

I tried that and it didnt work - because I'm checking it each function it just multiplied my speed by x0.5 or 2 each frame (took me a while to figure out cause i just went absolutely flying :) ) - would assigning a variable (e.g true if shift pressed) and then checking that each time i change the characters speed be better?

avatar image Captain_Pineapple b05569 · Nov 16, 2021 at 09:00 AM 2
Share

Can you share your updated code?

I am certain you simply misunderstood what i meant:

      if (Input.GetButton("Sprint") && isGrounded) 
      {
          speed = 36f;
      } else 
      {
          speed = 12f;
      };
      
      if (Input.GetButton("left control") && isGrounded) 
      {
          speed *= .5f;
      } 
avatar image Bunny83 Captain_Pineapple · Nov 16, 2021 at 09:32 AM 0
Share

Ideally both would be modifiers and the actual base speed is defined elsewhere

speed = baseSpeed; if (Input.GetButton("Sprint") && isGrounded) { speed = 3f; } if (Input.GetButton("left control") && isGrounded) { speed = 0.5f; } // use speed here

So if baseSpeed is 12 you get 12 when nothing is pressed. You get 36 when sprinting and 6 when "crouching". When you press both at the same time you get a value of 18 since we multiply the base speed by 3 and divide the result in half.

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

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

How to make camera position relative to a specific target. 1 Answer

Turning to face movement direction 0 Answers

OnMove Vector2 never returns to (0,0) 1 Answer

Why am I losing some inputs on Update function? 0 Answers

Making a hand move with the mouse 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