Character Controller Wont Jump With Low Input Levels

I followed a Brackeys tutorial for First Person Movement (

) and it all works except the jumping function. The jumping function does not work with my current amount to change the velocity.y but when I change velocity.y = 50 it sends me flying into the air. When velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity) and I jump the only thing that happens is that the camera raises up 1 pixel and then returns to normal. Thanks in advance for any help!

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

public class PlayerMovement : MonoBehaviour
{

    public CharacterController controller;

    float speed = 12f;
    float gravity = -19.62f;
    float jumpHeight = 3f;
   
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    public Vector3 velocity;
    public bool isGrounded;
    // Start is called before the first frame update

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

        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if(isGrounded)
        {
            velocity.y = -2f;
        }

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);

        if(Input.GetButtonDown("Jump") && isGrounded)
        {
           
            velocity.y =  Mathf.Sqrt(jumpHeight * -2f * gravity);
           
        }

        if(Input.GetKey(KeyCode.LeftShift))
        {
            speed = 24f;
        }
        else speed = 12f;
        velocity.y += gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);
    }

   
}

The above code is derived from the defective Unity example code.

I highly recommend discarding it and getting good code instead or you will constantly have ground detection issues.

I wrote about this before: the Unity example code in the API no longer jumps reliably.

I reported it to Unity via their docs feedback in October 2020. Apparently it is still broken as of October 2022:

Note how the example code calls .Move() twice in one frame… that’s bad.

Here is a work-around:

I recommend you also go to that same documentation page and ALSO report that the code is broken.

When you report it, you are welcome to link the above workaround. One day the docs might get fixed.

If you would prefer something more full-featured here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

That one has run, walk, jump, slide, crouch… it’s crazy-nutty!!

1 Like