Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 klay03 · Jan 16 at 09:18 AM · character controllerissuefps controllercrouchjittering

Character Controller Crouching Jitter Issue

hello guys, i've run a bit into a problem, basically when my character goes to crouching, everything goes smoothly, but when I un-crouch, I get this jittery motion going back up, which is kinda annoying because, visually it's really bad...

although i've searched online for potential fixes, and there are answers regarding about character controller center and "camera positions", obviously im not the guy that just straight copy pastes everything, so I find it hard what is like the efficient way to do it... here's the code

     void Crouch()
     {
         if (canCrouch == true)
         {
             if (Input.GetKey(crouchKey))
             {
                 isCrouching = true;
                 walkingSpeed = crouchingSpeed;
                 controller.height = Mathf.MoveTowards(controller.height, 1.0f, Time.deltaTime / crouchTimeDown);
                 Debug.Log("Crouching");
             }
             else if (!Input.GetKey(crouchKey) && touchedCeiling)
             {
                 isCrouching = true;
                 walkingSpeed = crouchingSpeed;
             }
             else
             {
                 isCrouching = false;
                 controller.height = Mathf.MoveTowards(controller.height, 2.0f, Time.deltaTime / crouchTimeUp);
             }
         }
     }


UPDATE: So, upon trying things out, the jitter problem was lessen visually, or that's what I think happened by trying to meddle with the center Y axis of the character controller.

     void Crouch()
     {
         if (canCrouch == true)
         {
             if (Input.GetKey(crouchKey) && isGrounded)
             {
                 isCrouching = true;
                 walkingSpeed = crouchingSpeed;
                 controller.height = Mathf.MoveTowards(controller.height, standingHeight / 2, (5.0f * Time.deltaTime));
                 controller.center = Vector3.MoveTowards(controller.center, crouchingCenter, (5.0f * Time.deltaTime));
                 Debug.Log("Crouching");
             }
             else if (!Input.GetKey(crouchKey) && touchedCeiling)
             {
                 isCrouching = true;
                 walkingSpeed = crouchingSpeed;
             }
             else
             {
                 isCrouching = false;
                 controller.height = Mathf.MoveTowards(controller.height, standingHeight, (5.0f * Time.deltaTime));
                 controller.center = Vector3.MoveTowards(controller.center, standingCenter, (5.0f * Time.deltaTime));
             }
         }
     }
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 AnnabelleParisian · Jan 17 at 05:46 AM 1
Share

Hey, thanks for the post i was posting the same issue as you and i jumped to this post i am still waiting for the answer.

2 Replies

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

Answer by klay03 · Jan 17 at 10:45 AM

FINAL UPDATE:

Alas!, I have fixed it, what was needed to be done was not I was really expecting. @AnnabelleParisian


Upon searching the internet for potential fixes, I came across this Youtube video by Hero 3D, which talks about how to do smooth crouching: https://www.youtube.com/watch?v=Ksy42wRaaA4


So to save you from reading a bunch of nerdy stuff, I'm not gonna explain everything, watch the video if you want to fully understand the code itself. Upon trying out his code the first try, it didn't work, so what I did was tweak his script a little so that it would work with my script. First, here are the needed variables:


 public Transform playerCamera;

 private float crouchingSpeed = 2.5f;
 private float timeToCrouch = 5.0f;
 private float standingHeight = 2.0f;
 private float crouchHeight = 1.0f;

With those variables set up, let's view the Crouch() function.


     void Crouch()
     {
         float crouchedHeight = isCrouching ? crouchHeight : standingHeight;
 
         if (controller.height != crouchedHeight)
         {
             AdjustController(crouchedHeight);
 
             Vector3 camPosition = playerCamera.transform.localPosition;
             camPosition.y = controller.height;
 
             playerCamera.transform.localPosition = camPosition;
         }
 
         if (isCrouching)
         {
             walkingSpeed = crouchingSpeed;
         }
     }

So what this Crouch() function does is it handles if we are crouching or not, with the first line: float desiredHeight = isCrouching ? crouchHeight : standingHeight: we are going to put a condition that if the player is indeed crouching, then we are getting the crouhHeight variable, then on the "if " statement below, if the height of the controller does not equal to our crouchedHeight, we then adjust the controller to our said crouchedHeight.


     private void AdjustController(float height)
     {
         float center = height / 2;
 
         controller.height = Mathf.LerpUnclamped(controller.height, height, timeToCrouch * Time.deltaTime);
         controller.center = Vector3.LerpUnclamped(controller.center, new Vector3(0, center, 0), timeToCrouch * Time.deltaTime);
     }



So here is the actual function of which the action of crouching takes place. You can see that it is in LerpUnClamped, because we all know that Lerp only clamps its "t" between 0 and 1, but since in my case, the crouch was too slow, so I changed it into LerpUnClamped so that the "t" for this Lerp would exceed between 0 and 1.


I hope that wasn't confusing at all xd, well I was really confused myself too but I found a way to fix it myself.


P.S You should set the center Y axis of the character controller to 1, with a height of 2.


Comment
Add comment · 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
0

Answer by PrabodhPrasad · Jan 16 at 04:22 PM

@klay03, You are dividing Time.deltaTime with crouchTimeUp and crouchTimeDown. Try multiplying it instead. I'm not sure but it's worth giving it a try.

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 klay03 · Jan 16 at 04:32 PM 0
Share

i just tried that earlier this day, but it was still the exact problem, the player will still jitter going back up...

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

133 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

Related Questions

Is there a way to make a wallrunning system using the character controller? 1 Answer

How to make player crouch smoothly, PLZ HELP !!!! 2 Answers

Character having character controller is very jittery/shaking 4 Answers

Help with crouch script! 2 Answers

Extreme Jittering of character having character controller in unity 0 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