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 CalisIAm · Oct 26, 2015 at 06:55 PM · movement3dcharactercontrollercrouch

Character Controller - My movement speed changes dont stick.

I am trying to make my crouch script change the movement speed. The way I do it now, it only works for the 0.0001 second or so that the keycode is clicked. However I do need crouching to stay: -Click C while standing = Crouch -Click C again while crouching = Stand back up

Any ideas?

Crouching if (Input.GetKey(KeyCode.C) && characterController.isGrounded) { Crouch(forwardSpeed,sideSpeed); }

 public void Crouch(float forwardSpeed, float sideSpeed) {
         if (!isCrouching) //Crouch
         {
             isCrouching = true;
             isProne = false;
             isStanding = false;
             characterController.height = crouchHeight;
             
             forwardSpeed = Input.GetAxis ("Vertical") * crouchingSpeed;
             sideSpeed = Input.GetAxis ("Horizontal") * crouchingSpeed;
             
             Vector3 speed = new Vector3 (sideSpeed, verticalVelocity, forwardSpeed);    
             speed = transform.rotation * speed;
             characterController.Move (speed * Time.deltaTime);
             
             //        characterController.center = Vector3(0, -0.5, 0);
             //        tempCamera.transform.localPosition.y -= crouchHeight;
         }
         else //Stand back up
         {
             isCrouching = false;
             characterController.height = charHeight;
             isStanding = true;
             isProne = false;
             //        characterController.center = Vector3(0, 0, 0);
             //        tempCamera.transform.localPosition.y += crouchHeight;
             forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
             sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
             
         }
     }
          **Normal Speed**
    forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
         sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;

         Vector3 speed = new Vector3 (sideSpeed, verticalVelocity, forwardSpeed);    
         speed = transform.rotation * speed;
         characterController.Move (speed * Time.deltaTime);

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
0
Best Answer

Answer by Socapex · Oct 26, 2015 at 07:35 PM

You are calling the method only once, when the user presses the key. Instead you can set a bool to true when ctrl+c, and then check that in update. Pseudo code:

 // Start standing.
 bool isCrouching = false;
 
 if (Input.GetKey(KeyCode.C) && characterController.isGrounded) { 
 isCrouching = !isCrouching;
 }
 
 void Update() {
 if (isCrouching) {
 Crouch(forwardSpeed,sideSpeed); 
 }
 }
Comment
Add comment · Show 4 · 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 CalisIAm · Oct 27, 2015 at 02:41 PM 0
Share

You are not calling any function for when its false, hence I cant stand back up. I had it similar like this before, and yes the speed changes and so work, but I cannot figure out how to make it stand up again. Every attempt I did, screwed it all up again.

avatar image Socapex CalisIAm · Oct 27, 2015 at 11:16 PM 0
Share

That is pseudo-code, you shouldn't copy-paste it as-is. Code a different method for when its false.

avatar image CalisIAm Socapex · Oct 29, 2015 at 12:31 AM 0
Share

Oh my bad, ill look into it then. Had no idea what pseudo-code ment.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Crouching makes my player unable to move. 2 Answers

What is the best way to move a character for an FPS? 1 Answer

Continuously rotate a gameobject on 1 axis perpendicularly to another gameobject's normal? 1 Answer

How do I make a jumping and ledge-grabbing system, similar to TLoZ OOT in unity 3d? 0 Answers

Custom C# Crouch Script 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