Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

standing up after crouching?

Discussion in 'Scripting' started by Treasureman, Jun 27, 2016.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a crouching script that just changes the player's hight. It's really simple. My one problem is, when I try standing up while underneath an object, you phase right through it and cant move. How can I make my game so that if your head will touch, you wont be able to stand back up? Here's my script...
    Code (JavaScript):
    1. var LeftTexture :GameObject;
    2. var RightTexture :GameObject;
    3. var player : CharacterController;
    4. var hitBox : CapsuleCollider;
    5. var standHeight : float = 2.5;
    6. var crouchHeight : float = 1.5;
    7. var smooth : float = 0;
    8. var crouchTimer : Light;
    9. var soundSpeed : float = 0;
    10. var cam : GameObject;
    11. var standCamera : float = 0;
    12. var crouchCamera : float = -1.5;
    13. var footsteps : AudioSource;
    14. function Update () {
    15.     if (Input.GetButtonDown ("Crouch"))
    16.         if( LeftTexture.activeSelf){
    17.             RightTexture.SetActive (true);
    18.             LeftTexture.SetActive (false);
    19.         }
    20.         else if (RightTexture.activeSelf){
    21.             LeftTexture.SetActive (true);
    22.             RightTexture.SetActive (false);
    23.         }
    24.     if (LeftTexture.activeSelf)
    25.     {
    26.         player.height = crouchHeight;
    27.         hitBox.height = crouchHeight;
    28.     }
    29.     if (RightTexture.activeSelf)
    30.     {
    31.         player.height = player.height = Mathf.Lerp(player.height,standHeight,Time.deltaTime*smooth);
    32.         hitBox.height = hitBox.height = Mathf.Lerp(hitBox.height,standHeight,Time.deltaTime*smooth);
    33.     }
    34.  
    35.     if (crouchTimer.GetComponent.<Light>().intensity > 0)
    36.     {
    37.         footsteps.enabled = false;
    38.     }
    39.  
    40.     if (crouchTimer.GetComponent.<Light>().intensity == 0)
    41.     {
    42.         footsteps.enabled = true;
    43.     }
    44.        
    45.     if (Input.GetButtonDown ("Crouch"))
    46.     {
    47.         crouchTimer.GetComponent.<Light>().intensity = 1;
    48.     }
    49.        
    50.    
    51.     crouchTimer.GetComponent.<Light>().intensity -= 0.1 * Time.deltaTime / soundSpeed;
    52. }
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Before you change the height of your capsules, you should check first by using Physics.CheckCapsule.
    https://docs.unity3d.com/ScriptReference/Physics.CheckCapsule.html

    If the capsule check clears and there is enough room you go ahead with changing the height. If the capsule check fails and something is in the way, then don't change the height.
     
    the_motionblur likes this.
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I looked at the page. How can I make my code have those be checked. Like...
    Code (CSharp):
    1. if(blankety blankety blank)
    2. {
    3. crouchOn.SetActive(true);
    4. }
    What you I put behind the if statement?