- Home /
 
Player detecting collision when crouching
Hi, so I found this crouch script on internet:
 private var crouchHeight : float;
 private var standardHeight : float;
 private var crouching : boolean = false;
 private var controller : CharacterController;
 
 function Start () {
     controller = GetComponent(CharacterController);
     standardHeight = controller.height;
     crouchHeight = controller.height/2;
     crouching = false;
 }
 
 function Update () {
     if (Input.GetButtonDown ("Crouch")){
         if(crouching){
             controller.height = standardHeight ;
             controller.center = Vector3 (0, 0, 0);
             crouching = false;
             return;
         }
 
         if(!crouching)
             crouch();
         }
     if (Input.GetButtonUp ("Crouch")){
         if(crouching){
             controller.height = standardHeight ;
             controller.center = Vector3 (0, 0, 0);
             crouching = false;
             return;
         }
 
         if(!crouching)
             crouch();
         }
     }
 
 function crouch() {
     controller.height = crouchHeight;
     controller.center = Vector3 (0, -0.5, 0);
     crouching = true;
 }
 
               I sort of understand how it works (I´m new to scripting and Unity) but there is one problem I have and that is when I stand up again and if I´m under a GameObject the player stands up inside the GameObject! If there is any easy way to fix this, I've tried adding a second collider and have it as a trigger (Not activated by this script) but it didn´t really work out..
If anyone could help me?
//Elis
               Comment
              
 
               
              Answer by killer-zog · Jun 22, 2013 at 09:25 AM
you can add a trigger for that area. and use OnTriggerEnter to make the player stay crouch until he exit that area( use OnTriggerExit). here is a link hope it helps.
http://answers.unity3d.com/questions/424984/crouch-enabled-on-certain-areas.html
Your answer