Search Unity

crouching in c# with raycast

Discussion in 'Scripting' started by Dsavage13, Nov 26, 2015.

  1. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    hello all, i need help with my code. im trying to crouch in my fps, everything is ok, except i stand up when im under an object in the game. ive tried raycasting on my own but i cant understand it, can anyone help? heres what i have so far:


    //crouching code


    if (charController.isGrounded && isCrouching == false && Input.GetKeyDown ("c")) {
    isCrouching = true;


    }

    if (isCrouching == true && checkCeiling == false && Input.GetKeyUp ("c")) {
    isCrouching = false;
    }

    if (isCrouching == false)
    {


    }



    if (isCrouching == true)
    {

    h = 1;
    movementspeed = crouchspeed;

    }
    float lastHeight = charController.height;
    charController.height = Mathf.Lerp (charController.height, h, Time.deltaTime*7);
    pos.x = Player.position.x;
    pos.y = Player.position.y + (charController.height - lastHeight) / 2;
    pos.z = Player.position.z;
    Player.position = pos;
    Vector3 speed = new Vector3( sidespeed, verticalVelocity, forwardspeed );

    speed = transform.rotation * speed;


    charController.Move ( speed * Time.deltaTime);
    }


    any tips on simplifying my code are appreciated :)
     
  2. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    anyone?
     
  3. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Where & why are you ray casting? You seem to be setting your player height based on isCrouching to set h=1 but don't use it.
    Wouldn't you just alter the height of the camera & character controller/capsule collider when crouching & standing based on the key stroke.
     
  4. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    if i knew how.. lol
     
  5. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Just as an idea, try childing your camera to your player then when the player crouches change the player controller transforms Y-scale to 1/2 then when they stand back up set it to 1.

    Have you worked your way through the tutorials in the learn section? It will ease you into stuff & explain how it works as they introduce each bit & saves the headache from banging your head against the desk (I speak from experience)
     
  6. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    id need a link, ive gotten it worked out, but for some reason its not detecting the enviroment,

    Code (CSharp):
    1. if (isCrouching == true)
    2.         {
    3.        
    4.         Ray Ceiling = new Ray(transform.position, Vector3.up);
    5.         RaycastHit hit;
    6.             if (Physics.Raycast(Ceiling, out hit, 1.5f))
    7.             {
    8.  
    9.                     if (hit.collider.tag == "enviroment")
    10.                 {          
    11.  
    12.                         checkCeiling = true; // stops the player from standing
    13.                 }
    14.             }
    15.          
    16.             h = 1;
    17.             movementspeed = crouchspeed;
    18.         }
    19.  
    20.  
    21.         if (isCrouching == false)
    22.         {
    23.             h = 2;
    24.  
    25.         }
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570