- Home /
Crouching help needed
I'm trying to get my player to crouch and I'm having some difficulty doing so. You see, I tried making it so that my game object would simply translate downwards, but it refuses to go through the floor (surprisingly). I would like to point out that the player has a character controller component on as well as the following code for crouching:
public GameObject player;
void Update () {
//crouching
if (Input.GetButton ("Fire1")){
player.transform.Translate(0, -0.5f, 0);
}
By the way, I made sure that the button associated with Fire1 is the same as in the input manager.
Answer by Dave29483 · Nov 18, 2015 at 05:57 PM
You won't be able to simply move the character controller down as it has a physics component to it. Also this would cause you to fall through geometry. The best way is to adjust the height of the character controller via Lerp.
If you do this in FixedUpdate, the controller won't jiggle or fall through collision hulls.
You may want to consider using raycast to determine if your current location has enough clearance for you to stand up again, before allowing it.
Answer by Dinosaurs · Nov 18, 2015 at 06:03 PM
This question is a little vague, and it's unclear what the desired outcome of the crouch is, but in most FPS games the goal would be to make your player collider smaller to fit behind cover. In that case, I'd suggest scaling down the players Y scale down rather than moving them into the floor.
It's not surprising that the character controller won't move into the floor; the character controller shouldn't be able to walk through floors or walls, so naturally trying to move into the floor will cause it to collide and stop.
http://docs.unity3d.com/ScriptReference/CharacterController.html
It's also worth noting that you should probably be performing the player translation in FixedUpdate rather than Update; because the character controller will interact with rigidbodies in the collision system, this will help ensure that it collides smoothly with objects in the world.
http://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html
Answer by Slendyberkay · Nov 18, 2015 at 08:40 PM
You can probably use 2 different collides for your character. Move the camera down by 0.9 tiles and disable top part of collider. Use raycasting to check if player can get up. Etc
Your answer
![](https://koobas.hobune.stream/wayback/20220612045547im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script error 1 Answer
Scripting relating to FIRST PERSON SHOOTER 1 Answer
Animation script gravity problem 0 Answers