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 superluigi · Jun 21, 2013 at 04:14 AM · javascriptcharactercontroller

help with character controller crouching

Hi I am using a character controller for a 2D game and have come across a problem I am sure many have had. I currently have the "s" setup to make my player crouch. What I did was make the controller.height 1.5 when I crouch and 2 when I don't. The problem is that when I let go of "s" to stand up I fall through the floor. When my controller grows back to 2 his capsule goes through the floor a little so unity just makes him fall right through. What is the best method to solve this problem while making sure it doesn't look bad during gameplay. Thank you in advance for any response.

 if (Input.GetKey("s"))
     {
         crouching = true;
         controller = GetComponent(CharacterController);
         controller.height = 1.5;
     }
     else if (Input.GetKeyUp("s"))
     {
         crouching = false;
         controller = GetComponent(CharacterController);
         controller.height = 2.0;
     }
Comment
Add comment · Show 6
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 AlucardJay · Jun 21, 2013 at 04:31 AM 0
Share

The best resource for crouching and running with the Character Controller : http://answers.unity3d.com/questions/164638/how-to-make-the-fps-character-controller-run-and-c.html

While GetComponent is now running faster, you really should store a reference to the controller at start (ins$$anonymous$$d of every frame while S is pressed or released)

 var controller : CharacterController;
 function Start()
 {
     controller = GetComponent(CharacterController);
 }
avatar image superluigi · Jun 21, 2013 at 04:42 AM 0
Share

I actually have var controller : CharacterController; up top at the very beginning of my script in line 1. It is currently not in any function. Do you recommend moving it to Start(). I tried to put it in Start once but couldn't,

avatar image AlucardJay · Jun 21, 2013 at 04:51 AM 0
Share

Find and store a reference to the Character Controller at Start, ins$$anonymous$$d of in the input conditionals :

 var controller : CharacterController;
 
 function Start()
 {
     controller = GetComponent(CharacterController);
 }
 
 function DoStuff()
 {
     if (Input.Get$$anonymous$$ey("s"))
     {
         crouching = true;
         
         controller.height = 1.5;
     }
     else if (Input.Get$$anonymous$$eyUp("s"))
     {
         crouching = false;
         
         controller.height = 2.0;
     }
 }

The problem you're having is when the collider is scaled up, it needs to be raised up by half the scaleY value. So the collider is not penetrating the collider below it when it is scaled up. Check the link in my first comment, answer by Aldo, Edited 2.

avatar image superluigi · Jun 21, 2013 at 04:54 AM 0
Share

cool thanks that's what I needed to learn about character controller at start. Now to check out that link.

avatar image AlucardJay · Jun 21, 2013 at 04:58 AM 0
Share

This is the line that is doing all the work, to raise the character controller up when it is scaled up, keeping the collider above the collider it is standing on :

 tr.position.y += ( ch.height - lastHeight ) / 2; // fix vertical position

You should also look at how the scale is lerped rather than being instantly changed.

Show more comments

1 Reply

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

Answer by AlucardJay · Jun 21, 2013 at 05:25 AM

Answer from my comments :

The best resource for crouching and running with the Character Controller : http://answers.unity3d.com/questions/164638/how-to-make-the-fps-character-controller-run-and-c.html

While GetComponent is now running faster, you really should store a reference to the controller at start (instead of every frame while S is pressed or released)


Find and store a reference to the Character Controller at Start, instead of in the input conditionals :

 var controller : CharacterController;
 
 function Start()
 {
     controller = GetComponent(CharacterController);
 }
 
 function DoStuff()
 {
     if (Input.GetKey("s"))
     {
         crouching = true;
         
         controller.height = 1.5;
     }
     else if (Input.GetKeyUp("s"))
     {
         crouching = false;
         
         controller.height = 2.0;
     }
 }

The problem you're having is when the collider is scaled up, it needs to be raised up by half the scaleY value. So the collider is not penetrating the collider below it when it is scaled up. Check the link in my first comment, answer by Aldo, Edited 2.


This is the line that is doing all the work, to raise the character controller up when it is scaled up, keeping the collider above the collider it is standing on :

 tr.position.y += ( ch.height - lastHeight ) / 2; // fix vertical position

You should also look at how the scale is lerped rather than being instantly changed.


I have stripped out the parts you want, and renamed some of the variables :

 #pragma strict
 
 private var ch : CharacterController;
 private var startHeight : float; // initial height
 
 function Start() 
 {
     ch = GetComponent(CharacterController);
     startHeight = ch.height;
 }
 
 function Update() 
 {
     var newH : float = startHeight; // new height variable
     
     // press C to crouch
     if ( Input.GetKey("c") )
     { 
         // calculate new height
         newH = 0.5 * startHeight;
     }
     
     // crouch/stand up smoothly
     var lastHeight = ch.height; 
     
     // lerp CharacterController height
     ch.height = Mathf.Lerp( ch.height, newH, 5.0 * Time.deltaTime );
     
     // fix vertical position
     transform.position.y += ( ch.height - lastHeight ) * 0.5; 
 }

Here is the process :

new height = start height

if crouching, new height = start height * 0.5

last height is the current character controller height

lerp the character controller height between the current height and the new height (crouch/stand smoothly)

add to the position Y : the current lerped height - the height before lerping, divided by 2

so if the character grows 0.1 in height, add 0.05 to the position Y

or if the character shrinks by 0.1 in height, minus 0.05 to the position Y

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 superluigi · Jun 21, 2013 at 05:33 AM 0
Share

thanks a lot brother you just added the final piece to my player script puzzle.

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

16 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

Related Questions

AI movement using Character Controller, turning problem 1 Answer

CharacterController unexpectedly moving up 0 Answers

Pick-Up Object With Mouse Hold 0 Answers

My CharacterController does not walk and face in the same direction 3 Answers

OnCollisionEnter Push Object Problem 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