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
1
Question by SynisterJoker · Apr 11, 2016 at 04:47 PM · c#charactercharactercontroller

How to crouch a FPS Controller?

Hi there can somebody help me? I want to crouch the FPS Controller (default FPS Controller from standard assets of Unity 5.3) but I can't find a way out... I tried this:

     if (Input.GetKeyDown (KeyCode.C)) {  
         CameraWithTorch.transform.position = Vector3.Lerp (CameraWithTorch.transform.position,crouchPosition.position,Time.deltaTime * smooth);
         check = true;
     }
     else{
         if (check == true) {
             CameraWithTorch.transform.position = Vector3.Lerp (CameraWithTorch.transform.position,cameraposition.position,Time.deltaTime * smooth);
             check=false;
         }            

     } 

but this is incomplete/not working because it works only on the Camera of the Controller... Can someone give me a tip?

ps sorry for the English

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

2 Replies

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

Answer by b1gry4n · Apr 11, 2016 at 06:41 PM

GetKeyDown is only true for the first frame the key was pressed down. Lerp will require continuous updates to smoothly transition between your positions. I suggest you take the logic of your crouch out of the input section of your code.

         if (Input.GetKeyDown(KeyCode.C))
         {
             if (crouching )
             {
                 crouching = false;
             }
             else {
                 crouching = true;
             }
         }
 
         if(crouching) {
             CameraWithTorch.transform.position = Vector3.Lerp(CameraWithTorch.transform.position, crouchPosition.position, Time.deltaTime * smooth);
         }
         else {
             CameraWithTorch.transform.position = Vector3.Lerp(CameraWithTorch.transform.position, cameraposition.position, Time.deltaTime * smooth);
         }


You will have to resize your character controller to your "crouch height" using height

http://docs.unity3d.com/ScriptReference/CharacterController-height.html

When you resize its height, it will probably seem like it jumps into the air (its because its resizing it from its center), you can offset this by adjusting the transforms position at the same time. For example...

         if (crouching)
         {
             character.height = 1.0f;
             transform.position = new Vector3(transform.position.x, transform.position.y - 0.5f, transform.position.z);
         }
         else {
             character.height = 2.0f;
             transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
         }

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 SynisterJoker · Apr 15, 2016 at 02:58 PM

Thank you very much, with character.height it was easier to solve the problem. This is my script now :

 if (check)
         {
             character.height = 1.61f; //crouch height
             transform.position = new Vector3(transform.position.x, transform.position.y - 0.4f, transform.position.z);
         }
         else {
             character.height = 3.22f; //player height
             //transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
         }

I "commented" this line :

 //transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);

because of the resizing the character looks like it jumps (like what you said)..

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

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

150 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

Related Questions

MoveTowards is curving for no reason 0 Answers

Character Controller not going up terrain hill 1 Answer

Character Rotation 0 Answers

Setup an animation script, but now mouselook is not working? 0 Answers

how to move only one object ? 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