Coruching effects player speed.

Hi,i have made crouch script,but i cant figure out : How can i make my player walks slower when im crouched,how to do that? Can anyone please help me? Can someone modify this script that it works like i want? :slight_smile:

P.S. Im using character controller,i dont have character motor…

Here is my script:

    var controller : CharacterController; //ADD in Inspector
        var isCrouched : boolean = false;
        var checkCeiling : boolean;
       
        function Update () {
            if(Input.GetKey("c")){
                if(!isCrouched) crouch();
            }else{
               if(isCrouched  !checkCeiling) stand();
            }
           
            if(isCrouched){
                if(Physics.Raycast (transform.position, Vector3.up, 2.0)){
                    checkCeiling = true;   
                }else{
                    checkCeiling = false;
                }
            }      
        }
       
        function crouch(){
            controller.height /= 2;
            isCrouched = true;
        }
     
        function stand(){
            gameObject.transform.position.y += controller.height/2;
            controller.height *= 2;
            isCrouched = false;
        }

Anyone? :confused:

It’s really easy, I’m assuming that’s why no one’s replied, how are you moving your character? Your own script or Unity’s Character Controller?
Use your own script using transform to move the player and have a variable for speed and cut that in half as you crouch.

You need to add CrouchedSpeed and StandingSpeed variables and then refer to the Character Motor script and change the values of this variables that are inside it:

// The maximum horizontal speed when moving
	var maxForwardSpeed : float = 10.0;
	var maxSidewaysSpeed : float = 10.0;
	var maxBackwardsSpeed : float = 10.0;

I’m of course assuming that if you asked that question you’re using the character motor script.

login4donald Can you please write EXAMPLE? :slight_smile:

private CharacterMotor playerMovementSpeeds;

public void Awake()
{
  playerMovementSpeeds = this.transform.GetComponent(typeof(CharacterMotor)) as CharacterMotor;
}

public Update()
{
    if(Input.GetKeyDown(controls.crouch)  !switchingViews  !inDelay)
	{
		crouching= !crouching;
	}
   if(crouching)
	{
		sprinting = false;
		modPos = new Vector3(0, -0.5f, 0);
		playerMovementSpeeds.movement.maxForwardSpeed = 2f;
		playerMovementSpeeds.movement.maxSidewaysSpeed = 1.5f;
		playerMovementSpeeds.movement.maxBackwardsSpeed = 1f;
	}
}