Why is this Crouching Script Not Working? Basic Question

I have tried every single crouching script, but with all of them i went through the floor, so i droped that method. Right now i have one that works that scales down the player, but i am trying to modify it so it only scales down it’s character controller(the collider thing, not the player), but with no results…What could be wrong with my script?, Why it still scales down the entire player making it a small capsule, with all the objects that it’s carring on???, how i could affect only it’s character controller(“collider”)?

1) The first script (Scales down the entire player)

function Update ()
{
 
if (Input.GetKeyDown ("c"))
{ 
    transform.position.y -= 3.5;
 
    transform.localScale.y = 1;
}
 
if (Input.GetKeyUp ("c"))
{ 
    transform.position.y += 5.5;
 
    transform.localScale.y = 5.0;
}
}

2) My attempt to scale ONLY the CharacterController (Not working, it still scales down the Player)

var controller : CharacterController;


function Update ()
{
 
if (Input.GetKeyDown ("c"))
{ 
    controller.transform.position.y -= 3.5;
 
    controller.transform.localScale.y = 1;
}
 
if (Input.GetKeyUp ("c"))
{ 
    controller.transform.position.y += 5.5;
 
    controller.transform.localScale.y = 5.0;
}
}

I would suggest changing the position of the Camera instead of the character controller. If you’re using the First Person controller that is. If you’re going for a crouching ( sneaking ) effect. Not really sure without more info.

I tried that but it’s not the solution for me, because i have some objects sticked in the player that do not follow the camera down. And when i bring the camera down, if i crouch under a table or a bed object, my CharacterControllerCollider don’t let me. You know what i mean?, as i can’t bring down my character without falling through the floor this is the only way i have left, and i think it’s possible, but somehow i’m not reffering to the character controller, but the whole player… Making it a small capsule…:(, Could you or anyone help me??

The player is scaled down because it’s parented to the controller. You should be able to shift the player models and camera down. Maybe parent them to an empty and parent the empty to the controller, so they all shift together when you shift the empty down.

Then maybe there could be a way to tell the script, “Do not scale down this and this things”, there are like 2 objects that i would like to keep with the same size always, do you know some way to do this??, maybe doing it and then making this objects variables that could be dragged into the script from the inspector? It’s a horror game so when i crouch under a bed, night vision camera, or the flashlight can’t look flat, as the player litterally is in that moment (when crouching)…Do you know some lines to add to this code to make this possible???:slight_smile:

I would parent them to an empty, then parent the empty to the controller. When you crouch, unparent the empty, resize the controller, then reparent at a lower position.

Maybe you can help me in something else that i can’t get done also haha, right now it’s on keydown/keyup, but i would like that if i press c, it crouches, and if i press c again, it stands up. I know it’s really easy but i don’t know how to do it without the “else” thing

One way to do it is to use a bool at the top of the page.

var isCrouched:bool = false;


if (Input.GetKeyUp ("c"))

{

 if(!isCrouched){
    controller.transform.position.y -= 3.5;

    controller.transform.localScale.y = 1;
   isCrouched = true;
 }
 else{//do other thing
 isCrouched = false;
 }
}

Thank you so much fire7side:), It means a lot that you teach me this things . Big progress on my crouching script today:). I’m trying to dissable the standig up function in “crouchAreas”, like under the table/bed, and slow down the speed of the player when crouching, Right now i have this if you whant to take a look, it’s heading in the right direction but still not working, guessing that i have to add the character motor somewhere to affect the speeds, if you or someone whants to point me where are the mistakes/flaws it will be really apreciated:), The crouching script so far:

var crouchSpeed : float = 3; // crouching speed


private var isCrouching : boolean = false;
private var isInCrouchArea : boolean = false; 


function Start ()   
{

isCrouching = false;
}


function Update () 
{   
if (Input.GetKeyDown ("c")) 

{  

isCrouching = true;
speed = crouchSpeed;
transform.position.y -= 3.5;       
transform.localScale.y = 1; 
}   

if (Input.GetKeyUp ("c")) 
{      

transform.position.y += 5.5;             
transform.localScale.y = 4.5;
 
} 
}


function OnTriggerStay( other : Collider )
{
    if ( other.gameObject.tag == "crouchArea"  isCrouching ) 
    {

       if ( !isInCrouchArea )
       {
isInCrouchArea = true;
isCrouching = true;

    }
}
}
 
function OnTriggerExit( other : Collider ) 
{
    if ( other.gameObject.tag == "crouchArea" ) 
    {
isInCrouchArea = false;
isCrouching = false;

       
    }
}
if (Input.GetKeyUp ("c"))
{  
if(!isCrouching){
isCrouching = true;
speed = crouchSpeed;
transform.position.y -= 3.5;      
transform.localScale.y = 1;
}
else{
isCrouching = false;
transform.position.y += 5.5;            
transform.localScale.y = 4.5;
speed= walkspeed;
}
}

Why do you need in crouch area?

To make to the player impossible to stand up when he is under a table/bed, right now i can stand up under this objects, even if they have a collider, so i made a empty objects to place a second collider(but this ones are triggers), placed them under beds/tables and tag them “crouchArea”, so if the player presses “c” OnTriggerStay, nothing will happen, he will be not able to stand up;)

Then you add another condition in the crouch:

if (Input.GetKeyUp ("c"))
{  
if(!isCrouching){
isCrouching = true;
speed = crouchSpeed;
transform.position.y -= 3.5;      
transform.localScale.y = 1;
}

else if (! inCrouchArea){
isCrouching = false;
transform.position.y += 5.5;            
transform.localScale.y = 4.5;
speed= walkspeed;
}
}

Oh I see you want to crouch under objects. Then do something like this?

var crouch : boolean;

function Update () {

	if(Input.GetKeyDown("c")) {

		crouch = !crouch;

		if(crouch) {
              //Use transform.localScale -= Vector3(0.1,0,0);   Adjust size accordingly 
		}
		else{
	      //Use transform.localScale += Vector3(0.1,0,0);   Adjust size accordingly 
		}
	}
}

Fire7side: With this last lines that you writed, this is what happens:
Pressing “c” to crouch and release it to stand up works. But if i crouch under a bed (crouchArea), and release the “c” key, i remain crouching(Thats Perfect, not an issue at all), but when advancing away from this area (to other place in the room), i remain crouching, so i’m crouching everywhere (under beds or not), and if i press “c” again, i go under the floor and start falling. So the problem is, it should only keep crouching when i leave the area under the bed if i keep pressing “c” key down. I’t’s ok that if i release the “c” key under the bed, i keep the crouch state, but when moving outside the underbed area it should stand up. And if i release “c” under the bed, and press it again, it should not go under the floor. Could you help me in this last thing?, I think i can fix all the other stuff (crouching speed, not jump if crouching, etc)
Please one last push for my script:):slight_smile:

You’ll have to post the code you have so far.

Here is the script so far:):

var crouchSpeed : float = 3; // crouching speed


private var isCrouching : boolean = false;
private var isInCrouchArea : boolean = false; 


function Start ()   
{

isCrouching = false;
}


function Update () 
{   
if (Input.GetKeyDown ("c")) 

{  

isCrouching = true;
speed = crouchSpeed;
transform.position.y -= 3.5;       
transform.localScale.y = 1; 
}   

if (Input.GetKeyUp ("c"))

{  
if(!isCrouching){

isCrouching = true;
speed = crouchSpeed;
transform.position.y -= 3.5;      
transform.localScale.y = 1;
}

else if (!isInCrouchArea){
isCrouching = false;
transform.position.y += 5.5;             
transform.localScale.y = 4.5;

}
}
}


function OnTriggerStay( other : Collider )
{
    if ( other.gameObject.tag == "crouchArea"  isCrouching ) 
    {

       if ( !isInCrouchArea )
       {
isInCrouchArea = true;
isCrouching = true;

    }
}
}
 
function OnTriggerExit( other : Collider ) 
{
    if ( other.gameObject.tag == "crouchArea" ) 
    {
isInCrouchArea = false;
isCrouching = false;

       
    }
}

There is only one input for the c key, you keep adding the other one, but there is only supposed to be one. Don’t add the other one, this is all the code right here.
code edited after post so make sure you have what’s here now.

    var crouchSpeed : float = 3; // crouching speed
    
    private var isCrouching : boolean = false;
    private var isInCrouchArea : boolean = false;
     
     
    function Start ()  
    {
     
    isCrouching = false;
    }
     
     
    function Update ()
    {  
   
    if (Input.GetKeyUp ("c"))
     
    {  
    if(!isCrouching){
     
    isCrouching = true;
    speed = crouchSpeed;
    transform.position.y -= 3.5;      
    transform.localScale.y = 1;
    }
     
    else if (!isInCrouchArea){
    isCrouching = false;
    transform.position.y += 5.5;            
    transform.localScale.y = 4.5;
     
    }
    }

    }
     
     
    function OnTriggerStay( other : Collider )
    {
        if ( other.gameObject.tag == "crouchArea"  )
        {
     
           if ( !isInCrouchArea )
           {
        isInCrouchArea = true;
     
        }
    }
    }
     
    function OnTriggerExit( other : Collider )
    {
        if ( other.gameObject.tag == "crouchArea" )
        {
    isInCrouchArea = false;   
   if(isCrouching){
   isCrouching = false;
    transform.position.y += 5.5;            
    transform.localScale.y = 4.5;

   }  
           
        }
    }

He should stand up when he gets out of the crouch area without having to press c. If outside a crouch area, he should crouch when c is pressed and stand up when pressed again.

Thank you fire7side!!, it works better now, the automatically standing up when leaving the area is not the best, but it’s way better that what i had. I will try to make it stand up only if i have released the “c” button, and if i haven’t, keep crouching. Also i will try to smooth both actions(crouching, and standing up). And maybe if i get things right, post the script here, to make an alternative for the people. Anyway thank you a lot:), this is the script so far if someone whants to see how it ended:

var crouchSpeed : float = 3; // crouching speed

private var isCrouching : boolean = false;
private var isInCrouchArea : boolean = false;

    function Start (){

    isCrouching = false;
    
    }

    function Update (){  

    if (Input.GetKeyDown ("c"))

    {  

    if(!isCrouching){

    isCrouching = true;
    speed = crouchSpeed;
    transform.position.y -= 3.5;      
    transform.localScale.y = 1;

    }

    else if (!isInCrouchArea){
    
    isCrouching = false;
    transform.position.y += 5.5;            
    transform.localScale.y = 4.5;

    }
    }
    }

    function OnTriggerStay( other : Collider )
    {
        if ( other.gameObject.tag == "crouchArea"  )
        {

        if ( !isInCrouchArea )
        {
        isInCrouchArea = true;

        }
    }
    }

     
    function OnTriggerExit( other : Collider )
    {
        if ( other.gameObject.tag == "crouchArea" )
        {
    isInCrouchArea = false;   
    
   if(isCrouching){
   isCrouching = false;
   transform.position.y += 5.5;            
   transform.localScale.y = 4.5;

   }  
   }
   }

Glad you are making progress with it. It’s just untested code I’m writing so it’s what I think will work. For smoothing you could use lerp, but it has to be in the update or using a coroutine. There are, of course, many ways to do it. These things are just opportunities to learn more about coding.