Character Crouching setup in FPS or Third Person Adventure Game

Any person who have used Unity have also tried their hand in making a FPS/TPS game. Unity giving the inbuilt FPS Character Controller makes it as easy as just creating a plane and draging the Character Controller from the Standard Assets folder to your screen and voila we have a working game with our character in it who is free to move anywhere on the screen.
Even though Unity has an inbuilt Controller Movement Script but at the later point of time you might want to use your own character Script for the character movements.

As you do that you might achieve the basic character movements, jump, gravity etc but the problem where I faced was on the crouching or sliding ability of the character.
I had all the animation setup done for the character such as Walk, Jump, Sprint, Crouch/Slide but when tested, all the setup was working properly except Crouch. When I looked into the problem I noticed that it was still colliding with the enviornment which is above the charcter when in crouching mode and after some brain storming I found out that it is because we need to adjust the Character Controller to shrink in respect with the character animation.

So this Post is on how to overcome that situation and what you need to adjust in order to make the character crouch/slide in order to suit your game.

I will not cover the whole character Controller setup here but just the Crouching Controller Setup.
(Incase any one wants me to cover the whole Controller setup do comment and I will definitely consider writing it)

This is not a full Beginner guide and I assume that you all know how to setup a Character Mesh and add a character controller to you Character Mesh.

After we add the Character Controller to the CharacterMesh we have to adjust its Radius and Height and also the Position of the Controller according to our mesh.

01> After Adjusting the the Controller Setup, Right click on the Project view and make a new JavaScript. Name that script ControlSetup (or you are free to use and edit your existing Script)

02> I will be writing the Entire Script below and then explain everything line by line

——————————————————————————-


var charMoveSpeed : float = 20.0;
var charJusmSpeed : float = 5.0;

private var gravity : float = 20.0;
private var jumping : boolean = true;
private var crouching: boolean = true;
private var controller : CharacterController;

function Start()
{
   controller = GetComponent(CharacterController);
}

function Update()
{
   if(controller.isGrounded)
   {
      //Character Movement to be written here

      //Character Jump to be written here

      //Character Crouching
      if(Input.GetKeyDown("down"))
      {
         if(crouching)
         {
            Debug.Log("Crouching");
            Crouch();
         }
      }
      if(!Crouching)
      {
         DefaultController();
      }
   }
   transform.y -= gravity * Time.deltaTime;
}

function Crouch()
{
   controller.height = 3; //Controller Height Adjustment according to the Crouched height
   controller.center = Vector3(0,3.5,0); //Controller Position according to the Crouched Height
   If(Input.GetKeyUp("down"))
   {
      transform.position.y = 1; //AdjustingPositionY value to make sure character stays in ground
      crouching = false;
   }
}

function DefaultController()
{
   Debug.Log("Not Crouching!");
   controller.height = 10; //Original Controller Height
   controller.center = Vector3(0,5,0); //Original Controller Position
   crouching = true;
}

——————————————————————————-
03> Explanation is as follows:

a> Variable charMoveSpeed is the Movement speed of the Character
b> Variable charJumpSpeed is the Jump Speed of the Character
c> Private Variable Gravity isfor the Gravity of the Character
d> Jumping is a boolean variable to check iftrue or false so that to control double jumping
e> Crouching is a boolean variable to check for crouching on/off
f> Making a CharacterController Variable
g> Assigning the CharacterControllerin the controller variable for easy acess of the component
h> Checking if the character is grounded or not so as to preven jump or movement when the character isin air
i> Assigning the “Down Arrow key”for Crouching
j> Checking if the Crouching is True then to run the “Crouch”function
k> Checking if the Croucing is not True or false then run the “DefaultController”function
l> Assigning the Gravity to the Player
m> Making a New Function called “Crouch” which is called when the player push the Down Arrow Key
n> We will be making some adjustment in the CharcterController now. As we know that when the character is crouching the charcter is suppose to go down. We have the same thing in our animation but in reality crouching does’nt adjust the controller automatically. So we have to do so by the help of our script
o> At first we have to manually determine the value of Controlelr Height and Position when the character is crouching and which does not collide with the enviornments
p> After doing so, We Assign the value of the height using “controller.height” and Position by “controller.center” Please note that only Y Value is to be changed in Center as we want the controller to go down.
q> After that we want to check that as the player leaves the down button the Charcter should get up. So We do that by checking “GetKeyUp” command
r> We Change the crouching value to false and also cheat little bit here by positioning the player value litttle up than ground. Dont worry the charcter will come down on the ground on it own due to gravity
s> Next we check if the Crouching isset to false we will make the charcterController get to its original position and so we set the Height and Position to default position we adjusted in the Inspector
t> We set the crouching value back to True so that the charcter is able to crouch again
u> This is the gravity for the Character

After you write and run the game using this script you will see that when your character is crouching the controller setup will also crouch with the character and the character will behave as it should behave when crouching.

I hope I was able to explain the problem faced in crouching and if anyone feels me to explain in a more detailed manner please feel free to comment and I will do the same.
Incase of any question/suggestion do comment or drop me a mail and I will try to reply ASAP
Any kind of feedback is appreciated 🙂

  1. No trackbacks yet.

You must be logged in to post a comment.