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
1
Question by DadPool · Jan 14, 2015 at 09:49 AM · capsule collider

How do you Change the Height of a Capsule Collider in C#?

I've been trying to work on a Crouching Mechanic for an FPS style game. I cannot seem change the height of my capsule collider to reflect this. Here is the control portion of my code. This is all in C#

 using UnityEngine
 
 using System.Collections;
 [RequireComponent (typeof(CharacterController))]
 
 public class FPSCtrlr : MonoBehaviour {
     
     public float CCStandHeight = 2.0f;
     public float CCCrouchHeight = 1.0f;
     bool crouch = false;
     
     CharacterController characterController;
     CapsuleCollider playerCollider;
     // Use this for initialization
     void Start () 
     {
         Screen.lockCursor = true;
         characterController = GetComponent<CharacterController>();
         playerCollider = GetComponent <CapsuleCollider>();
     }    
     // Update is called once per frame
     void Update ()
     {
         //Stand to Crouch
         if (Input.GetButtonDown ("Crouch") && crouch == false)
         {
             playerCollider.height = CCCrouchHeight;            
             crouch = true;
         }
         //Crouch to Stand
         if (Input.GetButtonDown ("Crouch") && crouch == true)
         {
             playerCollider.height = CCStandHeight;            
             crouch = false;
         }
         
 }

All other portions of the code work fine so I only included the part giving me problems.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by HarshadK · Jan 14, 2015 at 09:52 AM

You don't need a separate capsule collider on your object with Character Controller attached to it since Character Controller already has a capsule collider with it.

Remove your capsule collider component. And you can set the height of the capsule collider on the Character Controller using CharacterController.height.

Also to let you know in order to handle the collisions for Character Controller you need to use CharacterController.OnControllerColliderHit(ControllerColliderHit) and not OnCollisionEnter.

Comment
Add comment · Show 13 · 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 Owen-Reynolds · Jan 14, 2015 at 05:53 PM 2
Share

Hah! Probably the capsule was shrinking, but since there were two overlapping, it didn't look that way.

In my experience it's stronger than "don't need ... capsule collider." With a charController, you can't have any other colliders or you get stuck on them (and they don't correctly limit you, anyway.)

avatar image HarshadK · Jan 15, 2015 at 05:44 AM 1
Share

Can you share your new code?

avatar image HarshadK · Jan 16, 2015 at 09:06 AM 1
Share

There were few things that were needed to be changed in that script.

The updated script is:

 bool isCrouched;
 
  // Use this for initialization
  void Start () 
  {
 //     Screen.lockCursor = true;
      characterController = GetComponent<CharacterController>();
     isCrouched = false;
  }    
  // Update is called once per frame
  void Update ()
  {
 
      speed$$anonymous$$ultiplier = 1.0f;
      // Look Vertical (Up-Down)
      lookUDRot -= Input.GetAxis ("$$anonymous$$ouse Y") * lookVertSens;// (Up-Down Camera Rotation Input)
      lookUDRot = $$anonymous$$athf.Clamp (lookUDRot, -rangeUpDown, rangeUpDown);//(Restrict Up-Down Look)
      Camera.main.transform.localRotation = Quaternion.Euler (lookUDRot, 0, 0);// ($$anonymous$$ouse Y Axis Rotates around X)
      // Lateral Rotation (Left-Right)
      float lateralRotation = Input.GetAxis("$$anonymous$$ouse X")* latRotSens;//(Rotate Player Left-Right Input)
      transform.Rotate (0,lateralRotation,0);// ($$anonymous$$ouse X Axis rotates around Y Axis)
      // Linear and Lateral $$anonymous$$ovement
      float linear$$anonymous$$ove = Input.GetAxis("Vertical")* linearSpeed;// (Linear $$anonymous$$ovement Front-Back)
      float lateral$$anonymous$$ove = Input.GetAxis ("Horizontal")* lateralSpeed;// (Lateral $$anonymous$$ovement Left-Right)
      //Jump
      if(characterController.isGrounded && Input.GetButtonDown("Jump"))// (Jump Conditions)
          {
              vertVelocity = jumpSpeed;// (Jump)
          }    
 
      if (Input.GetButtonDown("Crouch"))
      {
          //Stand to Crouch
          if(isCrouched == false)
         {
              characterController.height = 1.0f;
              speed$$anonymous$$ultiplier = CrouchSpeed;
              isCrouched = true;
         } else
         // Crouch to stand
         {
              characterController.height = 2.0f;
              speed$$anonymous$$ultiplier = StandSpeed;
              isCrouched = false;        
         }
      }
 
      vertVelocity += Physics.gravity.y * Time.deltaTime;// (Gravity)
      Vector3 movement = new Vector3 (lateral$$anonymous$$ove * speed$$anonymous$$ultiplier , vertVelocity, linear$$anonymous$$ove * speed$$anonymous$$ultiplier);// (Lateral $$anonymous$$ovement = X, Vertical $$anonymous$$ovement = Y, Linear $$anonymous$$ovement = Z)
      movement = transform.rotation * movement;// (Changes character movment from world coordinates to character rotaion based coordinates)
      characterController.$$anonymous$$ove (movement * Time.deltaTime);
      }
      
 }
avatar image HarshadK · Jan 16, 2015 at 09:06 AM 1
Share

The changes that are made are:

  • Took declaration of isCrouched outside of Update method as its scope was limited for one frame only which was undesired.

  • Set the isCrouched to false at first in Start since it was setting isCrouched to false each frame because being in Update loop.

  • Changed the logic for getting Button Input and processing it based on value of isCrouching since because of previous logic it was always executing the crouch to stand loop at the end and hence stand to crouch was not working.

avatar image HarshadK · Jan 16, 2015 at 09:16 AM 1
Share

Also note one thing that when you change form crouch to stand the height of the collider is increased but it will intercept the ground and fall off the ground so before you change the height you also need to set the center to be up than the current center as per the crouch height.

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

2 People are following this question.

avatar image avatar image

Related Questions

Starter Asset Capsule gets stuck on corners 0 Answers

Odd Capsule Collider Behavior 0 Answers

My Capsule Collider Seems To 'Drop' During Runtime 0 Answers

Why does this happen to my capsule collider at runtime? 2 Answers

Why does my Capsule Collider's radius have to be smaller than the height? 3 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