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 GodsStrength · Apr 21, 2020 at 09:00 PM · fps controllercrouchcrouching

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

Hi. I have done the code for my player to crouch when I press the C button and stand up when press C again. The problem is my player does this instantly and I dont know to put my crouchingSpeed variable and time.deltatime to my code to make my crouch be smooth. Here's the code. @dcordoba PLZ HELP !!

 public bool isCrouched = false;
 public CharacterController controller;
 public float crouchDistance = 0.9f;
 public float crouchingSpeed = 0.7f;

 // Start is called before the first frame update
 void Start()
 {
     controller = GetComponent<CharacterController>();
 }

 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.C))
     {
         if(isCrouched == false)
         {
             isCrouched = true;
             //transform.position = (transform.position + new Vector3(0, -crouchDistance , 0));
             transform.Translate(new Vector3(0, -crouchDistance , 0));
         }
         else if(isCrouched == true)
         {
             isCrouched = false;
             //transform.position = (transform.position + new Vector3(0, crouchDistance , 0));
             transform.Translate(new Vector3(0, crouchDistance, 0));
         }
     }
 }
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 DCordoba · Apr 21, 2020 at 09:44 PM 0
Share

3ps or fps?

if is 3ps, its a humanoid right?, you need to animate crouching, to be consistent I think you must use the same animator to walking, crouching and runing, also if you player fall, the falling, jumping etc animations

you can manage the animations trough conditions on the animator, and please put a bool here, that block the inputs while you are animating some things, like crouching or falling, so you cant walk(displace the character) while crouching or falling, just after you are crouched disable the bool, in short, enable the bool at the beggining of each animation, and disable it when animation end

if is fps and you never see the character you can just lerp camera to crouched position, put a counter int busyCount and increase it each frame, use that to calculate the lerp position, when you reach the max count you have crouched, then stop the lerp, enable the other controls and reset the counter to future usage, the same on the other direction to stand up

avatar image GodsStrength DCordoba · Apr 22, 2020 at 11:48 AM 0
Share

@DCordoba Thanks for your reply, $$anonymous$$y game is FPS and I'm a beginner and need help. Can you explain more about the code I should add ??

2 Replies

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

Answer by MaxLevelNoob · Apr 23, 2020 at 04:29 AM

My bad, I rushed the code previously and did not think you'd like to just change the transform Y-Position. Here is a revised one, done with more effort. Enjoy yourself

  public bool isCrouched = false;
     public CharacterController controller;
     public float crouchDistance = 0.9f;
     public float crouchingSpeed = 0.7f;
     public bool isChangingPosition = false;
 
     // Start is called before the first frame update
     void Start()
     {
         controller = GetComponent<CharacterController>();
     }
 
     // Update is called once per frame
     void Update()
     {
         //True if Key Pressed and Not changing positions currently
         if (Input.GetKeyDown(KeyCode.C))
             StartCoroutine(Crouch());
     }
 
     private IEnumerator Crouch()
     {
         //True if we're already changing position, then end this function
         if (isChangingPosition)
             yield break;
 
         //Acknowledge that we are changing position so above criteria will keep out multiple calls
         isChangingPosition = true;
 
         //Get our Starting Height
         float StartingHeight = transform.position.y;
 
         //Find our End Height
         float EndHeight;
         if (isCrouched)
             EndHeight = StartingHeight + crouchDistance; //We are crouched, hence we are trying to stand up (Go Up / Increase)
         else
             EndHeight = StartingHeight - crouchDistance; //We are standing, hence we are trying to crouch (Go Down / Decrease)
 
         Debug.Log("E" + EndHeight);
 
         //Keep changing the Y Value until we reach the end height
         while (transform.position.y != EndHeight)
         {
             //Here we check what action we're doing (Standing/Crouching)
             //Then we move according to the action
             //And finally we check if we have reached the End
 
             if (isCrouched)
             {
                 //We are crouched, hence we are trying to stand up (Go Up / Increase)
                 transform.Translate(Vector3.up * crouchingSpeed * Time.deltaTime);
 
                 //True if we reached our goal
                 if (transform.position.y >= EndHeight)
                 {
                     //Make sure we are EXACTLY at the EndHeight
                     transform.position = new Vector3(transform.position.x, EndHeight, transform.position.z);
                     Debug.Log("COMPLETE!");
                 }
             }
             else
             {
                 //We are standing, hence we are trying to crouch (Go Down / Decrease)
                 transform.Translate(Vector3.down * crouchingSpeed * Time.deltaTime);
 
                 //True if we reached our goal
                 if (transform.position.y <= EndHeight)
                 {
                     //Make sure we are EXACTLY at the EndHeight
                     transform.position = new Vector3(transform.position.x, EndHeight, transform.position.z);
                     Debug.Log("COMPLETE!");
                 }
             }
             yield return null;
         }
 
         //We have reached the end!
 
         //Flip the Action as we have succeeded in changing now
         isCrouched = !isCrouched;
 
         //We're finished changing!
         isChangingPosition = false;
 
         yield return null;
     }



Comment
Add comment · Show 2 · 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 GodsStrength · Apr 23, 2020 at 10:25 AM 0
Share

@$$anonymous$$axLevelNoob Thank you so much !!! It worked perfectly !!! Now I should mix these codes with walking, sprinting and jumping code, which I hope wont be that hard. How many points should I reward you ??

avatar image MaxLevelNoob GodsStrength · Apr 23, 2020 at 03:36 PM 0
Share

Don't know about any points. So it's all good, stay safe and happy developing :)

avatar image
1

Answer by Kwel · Apr 22, 2020 at 08:46 PM

You need to apply a time factor to the translation. You can use Vector3.Lerp as mentioned in this post:

https://answers.unity.com/questions/444978/c-smoothing-out-transformtranslate.html

Maybe you could also make a basic animation for that through the editor and customize there the speed curve of the movement graphically. Also you can then blend animations through the Animator controller's transitions.

https://www.youtube.com/watch?v=UfC2UBWikz0

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 MaxLevelNoob · Apr 23, 2020 at 04:32 AM 0
Share

This is awesome!

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

127 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

Related Questions

Need help with crouching,Problem with crouching 1 Answer

Camera jitters when getting up from croutch 1 Answer

How can i stop my Third person character from crouching from a weapon i placed in his hand? 0 Answers

Character Controller Crouching Jitter Issue 2 Answers

Horror Crouch Script Help 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