Search Unity

Increase Height of CharacterController Without Jitter?

Discussion in 'Physics' started by f0ff886f, Jun 3, 2017.

  1. f0ff886f

    f0ff886f

    Joined:
    Nov 1, 2015
    Posts:
    201
    Hi, I'm having a problem with crouching in an FPS game.

    I've been adjusting the CharacterController.height in FixedUpdate(), and to crouch the results are OK. On downward motion the camera smoothly falls down as the actual capsule collider is made smaller then quickly floats down.

    However, I have a big problem with increasing the height (standing back up): if I try a 1 frame approach (just height = standingHeight;) then the capsule will intersect with the floor collider, and "jump" out of it, giving me an extremely quick and abrupt (and unusable) standing motion. If I try to lerp the height up to standingHeight, then I get the same behaviour, just with maybe 10 smaller/jerkier steps. I've tried to increase the transform by (0, standingHeight / 2, 0) on the frame of height increase to move up the capsule without snapping through the floor, but that doesn't work.

    Does anyone know a good strategy for implementing crouching? I need to actually shrink my capsule collider because crouched walk lets you get into smaller places.

    Thanks!
     
  2. Firesoft

    Firesoft

    Joined:
    Dec 20, 2016
    Posts:
    308
    Same Problem
     
  3. f0ff886f

    f0ff886f

    Joined:
    Nov 1, 2015
    Posts:
    201
    The way I fixed my problem: I have my camera on a child of the FPC / Capsule Collider. I set a box collider trigger for "lower areas", and in that area I ease the camera gameobject down to crouching height, and out accordingly.

    I know its stupid, and it sucks, but it was the quick easy way to achieve good visual results. Maybe other games do the same, I'm not sure. Good luck.
     
    Firesoft likes this.
  4. ThomasEgan

    ThomasEgan

    Joined:
    Dec 17, 2013
    Posts:
    21
    You need to adjust CharacterController.center as well when you adjust height (set it to half height + any foot offset you might be using)
     
    Zebbi likes this.
  5. modernator24

    modernator24

    Joined:
    Apr 7, 2017
    Posts:
    203
    Same problem here. I just try to update "center" of CharacterController but nothing changed.
     
  6. musolo

    musolo

    Joined:
    Sep 12, 2014
    Posts:
    238
    Can you expand a bit on the solution you`ve created. Could you describe it in more detailed way.How do you "ease" camera? Could you share details of your solution?
    Thanks!
     
    Last edited: Aug 25, 2020
  7. f0ff886f

    f0ff886f

    Joined:
    Nov 1, 2015
    Posts:
    201
    OK so my first approach was to shrink the Player's collider so that it fits in small corridors, so it would be physically correct. I had issues with doing that properly (like this thread says), so the workaround was to ignore changing the collider shape at all.

    Instead, move the camera to a child of the Player GameObject, and create volumes in your level where the player should crouch by putting a box collider and set it to a trigger.

    When your player enters that trigger, call Crouch() or Uncrouch() or whatever you have on your FPC. In that method, just animate/tween the vertical height of the Camera child object. That way the player has the visual response that they are crouching, but in reality the collider and all is the exact same.

    It's simple, and it worked fine, even though at the end our game never had crouching in it... just gamedev things :)

    Good luck!
    Matt
     
  8. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Can I just mention that this is a really great answer, thank you, I never knew why my crouching always had that jitter.