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
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.
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)
id need a link, ive gotten it worked out, but for some reason its not detecting the enviroment, Code (CSharp): if (isCrouching == true) { Ray Ceiling = new Ray(transform.position, Vector3.up); RaycastHit hit; if (Physics.Raycast(Ceiling, out hit, 1.5f)) { if (hit.collider.tag == "enviroment") { checkCeiling = true; // stops the player from standing } } h = 1; movementspeed = crouchspeed; } if (isCrouching == false) { h = 2; }