Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

CharacterController collision issue

Discussion in 'Scripting' started by UserNobody, Dec 21, 2020.

  1. UserNobody

    UserNobody

    Joined:
    Oct 3, 2019
    Posts:
    144
    Hello,
    I have added an ability to crouch in the game. The way I've done that is by changing the controller height and center y value. The issue is when I crouch to go underneath a platform the player still collides with the platform even though there's clearly enough space.

    CharacterController Issue.jpg

    If I set the controller height to the crouch height as the default height the player is able to go underneath the platform without trouble. Although, if I use the center offset the collision issue occurs.

    Code (CSharp):
    1. bool  isCrouching; float defaultHeight;
    2.  
    3. private CharacterController controller = null;
    4.  
    5. void Start()
    6. {
    7.     controller = GetComponent<CharacterController>();
    8.     defaultHeight = controller.height;
    9. }
    10. void Update()
    11. {
    12.     isCrouching = Input.GetKey(KeyCode.C);
    13. }
    14. void FixedUpdate()
    15. {
    16.     DoCrouch();
    17. }
    18. void DoCrouch()
    19. {
    20.     var height = isCrouching ? defaultHeight / 2 : defaultHeight;
    21.  
    22.     controller.height = Mathf.Lerp(controller.height, height, 5 * Time.deltaTime);
    23.     controller.center = Vector3.down * (defaultHeight - controller.height) / 2.0f;
    24. }
    How do I fix this issue? Thank you
     
    Last edited: Dec 21, 2020
  2. AbandonedCrypt

    AbandonedCrypt

    Joined:
    Apr 13, 2019
    Posts:
    69
    Can you provide your code ?
     
  3. UserNobody

    UserNobody

    Joined:
    Oct 3, 2019
    Posts:
    144
    Thanks for the reply! Of course, I added the code to the question. It's not the actual script, but just the way I handle crouching functionality.