My Jump Does not work,My Jump does not work

My character will not jump at all. I have tried many solutions but to no avail. (this is 3D)
How to I fix this?
I have tried using RigidBody gravity and the Rigidbody.AddForce but it does not work either.

void Update()
{
    Vector3 velocity = Vector3.zero;

    if (Input.GetKeyDown(KeyCode.Space))
    {
        Debug.Log("jumping");
        velocity.y = _jumpForce;
    }
    else 
    {
        // Apply gravity
        velocity.y -= _gravity;
    }
    

    

    // Move the controller
    _controller.Move(velocity * Time.deltaTime);
}

This works for other people but not for me. I have an attached CharacterController.
,After trying many solutions, I am not able to make my character jump. I had simplified my code, and tried to debug the issue but to no avail. The script also logs “jumping”, showing that it iterates through the section where the jump code is.

// Update is called once per frame
void Update()
{
    Vector3 velocity = Vector3.zero;

    if (Input.GetKeyDown(KeyCode.Space))
    {
        Debug.Log("jumping");
        velocity.y = _jumpForce;
    }
    else 
    {
        // Apply gravity
        velocity.y -= _gravity;
    }
    

    

    // Move the controller
    _controller.Move(velocity * Time.deltaTime);

This is someone else’s code that seems to work, but not for me.
I tried using the RigidBody gravity system and using rb.AddForce but that doesnt work either.
Even after removing the rigidbody component, I am still unable to jump.
I have a CharacterController Attached.

Hey there,

first up, Rigidbodys will not help if you are using a character controller.

The issue that you specifically face here is that your jump basically works - but it is so small and tiny that you cannot see it.

Why is that?

Becaus you set the velocity to something positive for 1 frame only. That beeing the frame in which you press space. The idea to then substract the gravity is alright but since you initialize the velocity vector to zero each frame you get a result like this over the frames:

velocity = -gravity

velocity = -gravity

velocity = -gravity

velocity = -gravity

velocity = jumpforce

velocity = -gravity

velocity = -gravity

so get rid of this your velocity variable should not be a local variable but a member of your class.

In addition you will have to check when you hit the ground. You should only ever accellerate downwards if you actually are in the air.