problem with gravity on character controller

my ball isn’t jumping normally, it just instantly changes it’s position in mid-air like gravity isn’t applied to it at all, any idea why it isnt working?

problem:ui77i9

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerMovement : MonoBehaviour
{
    // jump event system(if jump button is pressed)
    public JumpButtonEvent jumpButtonEvent;
   
    //character controller

    public CharacterController playerController;
    public Collider playerCollider;
    public Vector3 playerVector;
    public float gravityDown;
    public float neutralGravity;
    public float movementSpeed = 15f;
    public float defaultSpeed = 15f;
    public float jumpForce = 50f;
    public bool isJumping = false;
    public FixedJoystick joystickMove;
    float x;
    float y;   
    float z;

    public const float gravity = -5;
   
  //  public Rigidbody playerRb;   
  //  private Vector3 movement;
 
   
   

    public Transform gameCamera;


    // raycast hit ground
   
    public bool isGrounded = false;
    RaycastHit hitInfo;
    RaycastHit slopeHit;
    Vector3 raycastDirection;   
    public LayerMask raycastLayers;
    public float sphereRadius = 0.1f;   
    public float castDistance = 0.01f;
    public float slopeCastDistance = 1f;
    public float slopeSpeed = 1f;   
    Vector3 slopeMoveDirection;
    Vector3 slopeJumpDirection;

    private void Awake()
    {       
        playerController.detectCollisions = true;
        raycastDirection = Vector3.down;
    //    playerRb = GetComponent<Rigidbody>();
        playerCollider = GetComponent<SphereCollider>();
    }

    // Start is called before the first frame update
    void Start()
    {       
       
    }

     public void Movement()
      {
        x = joystickMove.Horizontal;
        y = verticalMovement;
        z = joystickMove.Vertical;

        playerVector = new Vector3(x, y, z); // the playervector input will have the x and z values. y is added later     
       
               

              
       playerVector = playerVector * movementSpeed; // multiplies the playervector with the movementspeed variable;
       playerController.Move(playerVector);

        Debug.Log(y);
    }

   

    public void Jumping()
    {
       
        if (isJumping == false && IsGrounded() == true && jumpButtonEvent.jumpBtnClicked == true) // if it meets the 3 requirements, the player will jump
        {
            neutralGravity = neutralGravity + jumpForce;           
        }

       

        /*  if (isJumping == false && IsGrounded() == true && OnSlope() == true)
          {
              slopeJumpDirection = Vector3.ProjectOnPlane(Vector3.up, slopeHit.normal); // detects the normal vector of the surface player is on
              playerRb.AddForce(slopeJumpDirection * jumpForce, ForceMode.Impulse); // makes the player jump on the normal direction
          } */
    }

    // Update is called once per frame
    void Update()
    {
    }

    private void FixedUpdate()
    {
        Jumping();
        Movement();      
        IsGrounded();
      //  OnSlope();



    }




    private void MovementButtonRelease()
    {
        if (joystickMove.Horizontal == 0 && joystickMove.Vertical == 0) // se largar os botoes que mexem o player, a velocidade do rigidbody pára (o player pára de se mexer)
        {

        }
    }



   

   public bool IsGrounded() // cria uma funçao bool que verifica se o player está no chão ou não
    {
        if (Physics.SphereCast(playerCollider.bounds.center, sphereRadius, raycastDirection, out hitInfo, castDistance, raycastLayers))
        {          

            if (hitInfo.collider.gameObject.layer == 7 || hitInfo.collider.gameObject.layer == 8) // se o layer do objeto que o raycast bateu for 7(Ground) então retorna um valor TRUE(significando que o play esta no chao)
            {               
                Gizmos.color = Color.green;
                Debug.Log(hitInfo.collider);
                Debug.Log("TOUCHING GROUND!");               
                gameObject.transform.parent = null;
                isJumping = false;
                neutralGravity = 0;
                return true;
            }

            else if (hitInfo.collider.gameObject.layer == 6) // se o raycast colidir com o objeto cujo layer é igual a 6(movingPlatforms) o player vai ser child object do cubo que se mexe
            {
                Gizmos.color = Color.green;
                Debug.Log(hitInfo.collider);
                Debug.Log("TOUCHING GROUND!");
                gameObject.transform.SetParent(hitInfo.collider.gameObject.transform, true);
                isJumping = false;
                neutralGravity = 0;
                return true;
            }

            else if (hitInfo.collider == null) // se o raycast nao detetar qualquer collider, o player nao está no chao
            {
                Gizmos.color = Color.red;
                gameObject.transform.parent = null;
                isJumping = true;
                neutralGravity = gravityDown; // if player is jumping the gravity will pull the player down
                return false;
            }

            else // se o raycast nao detetar qualquer collider, o player nao está no chao
            {
                gameObject.transform.parent = null;
                isJumping = true;
                neutralGravity = gravityDown; // if player is jumping the gravity will pull the player down
                return false;
            }
        }

       
        gameObject.transform.parent = null;
        isJumping = true;
        neutralGravity = gravityDown; // if player is jumping the gravity will pull the player down
        return false; // if the player isn't touching the ground he is jumping and not on any platform


    }

    public bool OnSlope()
    {

        if (Physics.Raycast(playerCollider.bounds.center, Vector3.down, out slopeHit, slopeCastDistance))
        {
            Debug.Log(slopeHit.normal);

            if (slopeHit.normal != Vector3.up)
            {
               
                slopeMoveDirection = Vector3.ProjectOnPlane(playerVector, slopeHit.normal);  // creates a vector3 that projects on plane surface from playerVector and hits on surface normal creating a perpendicular vector
              //  playerRb.AddForce(playerVector + slopeMoveDirection, ForceMode.Force); ; // the velocity will be the playervector + slopemovedirection, which means the vector will move in slope direction
                return true;
            }

        }
        return false;
    }


    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawRay(playerCollider.bounds.center, Vector3.down * castDistance);
        Gizmos.DrawSphere(playerCollider.bounds.center, sphereRadius);

        Gizmos.color = Color.yellow;
        Gizmos.DrawRay(playerCollider.bounds.center, Vector3.down * slopeCastDistance);
    }

    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        Debug.Log(hit);
    }

}

There’s a lot going on up there… one thing I note is you use FixedUpdate() and you really shouldn’t; FixedUpdate() is only for physics stuff involving a Rigidbody. While Raycasting is physics-y, it does not have any need to be FixedUpdate(), and CharacterController is not physics at all.

I see a ton of states related to selecting gravity… the bug might lie in there… might be a good place to start putting Debug.Logs().

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Here’s my super-simple use of CC with gravity: you can see how I avoid wiping out the Y velocity so that gravity properly accumulates.