- Home /
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.
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.
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.)
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);
}
}
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.
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.
Your answer
Follow this Question
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