Search Unity

Question What influences the movement of a character controller?

Discussion in 'Physics' started by Huxii, Dec 22, 2023.

  1. Huxii

    Huxii

    Joined:
    Sep 15, 2018
    Posts:
    37
    Hello all,

    I am on implementing a character controller, having a state machine and a problem. When walking while crouching, already in the first frame for whatever reason, the character controller of my player is pushed up which causes the player not being grounded anymore. It happens after the controller.Move.
    I took into consideration the skin width, but that’s not the problem. I am having since several weeks, and really cannot find it out. Please help.

    That's the code:

    Code (CSharp):
    1. public class CrouchWalkState : ProcanimState
    2. {
    3.     public override void OnEnable()
    4.     {
    5.         data.isCrouching = true;
    6.     }
    7.  
    8.     public override void Update()
    9.     {
    10.         float x = Input.GetKey(KeyCode.D) ? 1 : (Input.GetKey(KeyCode.A) ? -1 : 0);
    11.         float z = Input.GetKey(KeyCode.W) ? 1 : (Input.GetKey(KeyCode.S) ? -1 : 0);
    12.  
    13.         // Calculating the horizontal direction, in which the player is moving.
    14.         data.horizontalDirection = data.transform.right * x + data.transform.forward * z;
    15.         data.speed = 0.75f;
    16.  
    17.         // Calculating the speed, with which the player is moving.
    18.         Vector3 movementSpeed = data.horizontalDirection.normalized * data.speed;
    19.  
    20.         // Important for ramp walking, else the player is stuttering
    21.         if (data.isGrounded && data.controller.velocity.y < 0.001f)
    22.         {
    23.             data.verticalVelocity = -5f;
    24.         }
    25.         else
    26.         {
    27.             data.verticalVelocity -= data.gravity * Time.deltaTime;
    28.         }
    29.  
    30.         movementSpeed.y = data.verticalVelocity;
    31.         float posY1 = data.controller.transform.position.y; // debug
    32.  
    33.         data.controller.Move(movementSpeed * Time.deltaTime);
    34.  
    35.         float posY2 = data.controller.transform.position.y; // debug
    36.  
    37.         if(data.controller.transform.position.y > 0.035f) // debug
    38.         {
    39.             // Breakpoint is here
    40.         }
    41.     }
    42. }
    This is what the debugger shows: Just before the controller.Move the controller position is 0.03, and just after it 0.039, even thought in the controller.Move the controller is pushed down. How can this happen?



    Anybody some ideas, why the controller is pushed up? What circumstances can cause, that the controller is behaving this way?

    Thank you very much!
    Huxi
     
  2. Huxii

    Huxii

    Joined:
    Sep 15, 2018
    Posts:
    37
    I finally found the root cause, I got the idea for it by reading the nvidia CCT SDK description: Character Controllers — NVIDIA PhysX SDK 3.4.0 Documentation

    So when the player is crouching down, I was adjusting the character controller center and height. At the end of the crouching the controller was a little tiny bit above the skin width, so that it never reached the ground and was constantly in the falling animation. So I have moved the controller a little bit up and now it works. :).