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

Question Character never touches ground

Discussion in 'Scripting' started by FatsAndRoses, Feb 15, 2023.

  1. FatsAndRoses

    FatsAndRoses

    Joined:
    Dec 24, 2022
    Posts:
    10
    Hi I have the following code to handle jump:

    Code (CSharp):
    1.  
    2. //I check state is in the enter state and calculate the vertical velocity to push the player
    3.             if (_isGrounded && _velocity.y == 0)
    4.             {
    5.                 _velocity.y += Mathf.Sqrt(_jumpHeight * -2.0f * _gravity);
    6.             }
    7.  
    8.             //Add gravity
    9.             _velocity.y += _gravity * Time.deltaTime;
    10.             //Add vertical drag
    11.             _velocity.y /= 1 + _drag * Time.deltaTime;
    12.             //move the player
    13.             _controller.Move(_velocity * Time.deltaTime);
    14.  
    15.  
    16.             //to stop the velocity from falling
    17.             //Velocity starts becoming negative at the top of the jump, so I reset it when the player is grounded
    18.             if (_isGrounded && _velocity.y < 0)
    19.             {
    20.                 _velocity.y = 0f;
    21.             }
    22.  
    It works fine, but the player never touches ground, floating over the terrain, even if velocity value = -1.23f and _isGrounded = False;

    upload_2023-2-15_11-20-47.png

    Any clues?
    I'm using CharacterConroller and a UMA character.
     
  2. FatsAndRoses

    FatsAndRoses

    Joined:
    Dec 24, 2022
    Posts:
    10
    Ok, if I change the charcter's controller colider position, it works, but now the collider covers only the upper half of the body...
    upload_2023-2-15_11-36-39.png
     
  3. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    Check the skin width on the character controller....

    Unity - Manual: Character Controller component reference (unity3d.com)

    You could also make the model a child of the object that carries the CharacterController, then adjust the models position for a more fine tuned output...

    ALSO - I think a CharacterController should have a small downward force applied even when grounded to keep it's own inner Grounded state set to true.
     
    Last edited: Feb 15, 2023
    FatsAndRoses likes this.