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 Fox-Handler · May 21, 2014 at 10:39 AM · charactercontrollercrouch

Crouching Script

Can anyone explain to me the code in the update function as I am having a hard time making sense of it.

 #pragma strict
 
 var walkSpeed : float = 7; 
 var crouchSpeed : float = 3; 
 
 private var charMotor : CharacterMotor;
 private var charController: CharacterController;
 private var theTransform : Transform;
 private var charHeight: float; 
 
 function Start () 
 {
     charMotor = GetComponent(CharacterMotor);
     theTransform = transform; 
     charController = GetComponent(CharacterController); 
     charHeight = charController.height; 
 }
 
 function Update () 
 {
     var h = charHeight; 
     var speed = walkSpeed;
     
     if (Input.GetKey("c"))
     {
         h = charHeight *0.5; 
         speed = crouchSpeed; 
     }
     
     charMotor.movement.maxForwardSpeed = speed;
     
     var lastHeight = charController.height; 
     charController.height = Mathf.Lerp(charController.height, h, 5*Time.deltaTime);   
     theTransform.position.y += (charController.height - lastHeight)/2; 
 }
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 Andres-Fernandez · May 21, 2014 at 10:55 AM 0
Share

What part don't you understand?

avatar image Fox-Handler · May 21, 2014 at 11:03 AM 0
Share

particularly the last 2 lines.

1 Reply

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

Answer by AlucardJay · May 21, 2014 at 11:47 AM

line 33 lerps the character controller height from its current height to h (h is either standing height or crouched height).

line 34 repositions the character so it doesn't fall through the floor when scaling to a larger height, and doesn't appear floating when scaling to a smaller height.

Edit :

Try and imagine it with real values.

From Standing to Crouching :

line 32 : lastHeight is 1

line 33 : imagine this set charController.height to 0.5

line 34 : (charController.height - lastHeight)/2 = (0.5 - 1) / 2 = -0.25

theTransform.position.y += -0.25 , so it lowers the position Y so that the bottom of the collider is still on the ground

From Crouching to Standing :

line 32 : lastHeight is 0.5

line 33 : imagine this set charController.height to 1

line 34 : (charController.height - lastHeight)/2 = (1 - 0.5) / 2 = 0.25

theTransform.position.y += 0.25 , so it raises the position Y so that the bottom of the collider is still on the ground

Comment
Add comment · Show 7 · 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 Fox-Handler · May 21, 2014 at 01:32 PM 0
Share

Thank you for the explanation. I understand how lerp works now, but i'm going to look a bit further into the last line as it seems somewhat complex.

avatar image Fox-Handler · May 21, 2014 at 02:14 PM 0
Share

so when the collider sinks into the ground the position of it is subtracted so that the part thats intersecting is the new bottom?

avatar image AlucardJay · May 21, 2014 at 02:49 PM 0
Share

Here is a script to demonstrate the process. In a new scene, create an empty gameObject, and attach this script. Hit play, and move the slider, you'll see the bottom of the collider is always in the same place :

 #pragma strict
 
 private var myCapsule : Transform;
 private var myCollider : CapsuleCollider;
 
 private var charHeight: float; 
 private var modifier : float = 1; 
 
 private var graphic : Transform;
 
 function Start() 
 {
     var go : GameObject = GameObject.CreatePrimitive( PrimitiveType.Capsule );
     go.name = "Capsule Collider";
     myCapsule = go.transform;
     myCapsule.renderer.enabled = false;
     
     myCollider = myCapsule.GetComponent( CapsuleCollider );
     charHeight = myCollider.height; 
     
     // graphic
     go = GameObject.CreatePrimitive( PrimitiveType.Capsule );
     go.name = "Capsule Graphic";
     go.GetComponent( CapsuleCollider ).enabled = false;
     graphic = go.transform;
 }
 
 function Update() 
 {
     var h : float = charHeight * modifier;
     
     var lastHeight : float = myCollider.height;
     
     myCollider.height = $$anonymous$$athf.Lerp( myCollider.height, h, 5 * Time.deltaTime );
     
     myCapsule.position.y += ( myCollider.height - lastHeight ) / 2; 
     
     // graphic
     graphic.position.y = myCapsule.position.y;
     graphic.localScale.y = myCollider.height * 0.5;
 }
 
 function OnGUI() 
 {
     modifier = GUI.HorizontalSlider( Rect( 20, 20, 200, 25 ), modifier, 0.5, 1.0 );
 }
avatar image Fox-Handler · May 21, 2014 at 03:14 PM 0
Share

ah ok I see what your saying. the math adds up. I was getting confused because it looks like the capsule is sinking into the ground. The graphics threw me off. Thank you very much for the explanation. I understand what its doing now.

avatar image AlucardJay · May 21, 2014 at 03:43 PM 0
Share

I understand, you can see in my test script I compensated for this.

The best way to watch the collider is in the hierarchy, click on the character controller, in the scene view just watch the green wireframe of the collider.

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

22 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

Related Questions

Crouch Script with transform.localScale 0 Answers

Camera jitters when getting up from croutch 1 Answer

Detect collision when changing character controller height 2 Answers

Crouching makes my player unable to move. 2 Answers

Character moves slightly while crouching? 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