Gravity not working on CharacterController

Hi

I need some help here with the code.
I am following a tut on Udemy for building a fps controller and now at the part of adding gravity to my player controller, but for some reasons the gravity is not working.
My character don’t fall off an object when I walk off the edge, I just keep floating in the air.
When I look in the debug mode, the Y value is also not moving,

Is this a bug, or am I missing something here?

My code so far.

public class PlayerControllerScript : MonoBehaviour
{
    //Public
    [Header  ("Player Gravity")]

    public float PlayerGravityModifier;

    [Header  ("Player Movement")]
    public float PlayerMoveSpeed;
    public CharacterController PlayerControllerSlot;
    public float PlayerClampMagnitude = 8f;

    [Header  ("Player Camera")]
    public Transform PlayerCamTransform;
    public float MousecamSensitivity;
    public bool invertX;
    public bool invertY;
   
     //private
    private Vector3 PlayerMoveInput;


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

    // Update is called once per frame Horizontal
    void Update()
    {    //old Player movement code
        //PlayerMoveInput.x = Input.GetAxis("Horizontal") * PlayerMoveSpeed * Time.deltaTime;
        //PlayerMoveInput.z = Input.GetAxis("Vertical") * PlayerMoveSpeed * Time.deltaTime;

        // Store y Velocity
        float yStore = PlayerMoveInput.y;

        //Player movement input
        Vector3 vertMove = transform.forward * Input.GetAxis("Vertical");
        Vector3 HoriMove = transform.right * Input.GetAxis("Horizontal");

        PlayerMoveInput = HoriMove + vertMove;

        PlayerMoveInput = PlayerMoveInput * PlayerMoveSpeed ;
        //PlayerMoveInput.Normalize();

        PlayerMoveInput = Vector3.ClampMagnitude(PlayerMoveInput, PlayerClampMagnitude);

        PlayerControllerSlot.Move(PlayerMoveInput * Time.deltaTime);

        //Add Gravity to the player
        PlayerMoveInput.y = yStore;
   

        PlayerMoveInput.y += Physics.gravity.y * PlayerGravityModifier * Time.deltaTime;



    // Control the camera rotation
        Vector3 MouseInput = new Vector3(Input.GetAxisRaw("Mouse X"),Input.GetAxisRaw("Mouse Y")  ) * MousecamSensitivity;

        if(invertX)
        {
            MouseInput.x = -MouseInput.x;
        }
         if(invertY)
        {
            MouseInput.y = -MouseInput.y;
        }

        transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + MouseInput.x,transform.rotation.eulerAngles.z);
        PlayerCamTransform.rotation = Quaternion.Euler(PlayerCamTransform.rotation.eulerAngles + new Vector3(-MouseInput.y, 0f,0f));

       
    }
}

You’re pulling the y component out of the PlayerMoveInput vector:

        float yStore = PlayerMoveInput.y;
        //Player movement input
        Vector3 vertMove = transform.forward * Input.GetAxis("Vertical");
        Vector3 HoriMove = transform.right * Input.GetAxis("Horizontal");
        PlayerMoveInput = HoriMove + vertMove;

Then moving:

PlayerControllerSlot.Move(PlayerMoveInput * Time.deltaTime);

So the y is 0 when you call Move, resulting in no vertical motion.

1 Like

@dreamhead :
Just to be more explicit here. After the “PlayerMoveInput” vector is applied to the CharacterController you add gravity to that vector but you don’t do anything after than with that modified vector. The next frame you recreate the PlayerMoveInput vector from just the horizontal movement vectors (forward and right). So as Praetorblue said, you never apply gravity to the CC.

1 Like