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.
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.
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.
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.
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