Please help me with the juuuuumppppp

I am making an fps and have basic movement in - besides the jumping. I browsed stack and unity forums for hours and nothing works.

My code looks like this:

public class PlayerController: MonoBehaviour

{

[SerializeField] Transform playerCamera = null;
[SerializeField] float mouseSensitivity = 3.5f;
[SerializeField] bool lockCursor = true;
[SerializeField] float walkSpeed = 6.0f;
[SerializeField][Range(0.0f,0.5f)] float moveSmoothTime = 0.3f;
[SerializeField][Range(0.0f,0.5f)] float mouseSmoothTime = 0.03f;
[SerializeField] float gravity = -13.0f;
float cameraPitch = 0.0f;
float velocityY = 0.0f;
CharacterController con = null;
Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;
Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;
public Rigidbody rb;
public float jumpSpeed = 1.0f;
// private float gravity = 14.0f;

void Start()
{
    // rb.velocity += jumpSpeed * Vector3.up;
    con = GetComponent<CharacterController>();
    if (lockCursor){
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
}

// Update is called once per frame
void Update()
{
    UpdateMouseLook();
    UpdateMovement();
}
void FixedUpdate()
{
    if(con.isGrounded){
        Debug.Log("grounded");

        if(Input.GetKey(KeyCode.Space)){
            Debug.Log("WOOOOOOOOOOOOOOOOOOOORK");
            rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
        }
        else{

        }
    }
    
    else{
        Debug.Log("airtime boiiiiii");
        // gravity = -13;
    }
}

void UpdateMouseLook(){
    Vector2 targerMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

    currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targerMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);
    
    cameraPitch -= currentMouseDelta.y * mouseSensitivity;

    cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);

    playerCamera.localEulerAngles = Vector3.right * cameraPitch;

    transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
}

void UpdateMovement(){
    Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    targetDir.Normalize();

    currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);

    if (con.isGrounded){
        velocityY = 0.0f;
    }
    
    velocityY += gravity * Time.deltaTime;

    Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + Vector3.up * velocityY;

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

}

Any help would be appreciated! Thanks in advance.

Hello. Try using

rb.AddForce(rb.transform.up * jumpSpeed);

instead

rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);

on line 43

Hi,

Thanks for trying, but it doesn’t work. It detects the space bar being pressed, but I don’t move up. Is there anything else I could try?

Thanks!

Hello friend try to use this

rb.velocity = Vector2.Up * jumpSpeed;

in place of this

 rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);

if this rb.velocity = Vector2.Up * jumpSpeed trow you a error use this

 rb.velocity = Vector2.Up * jumpSpeed * TIme.deltaTIme;

remember “The truth is out there”

ok from what I can see your jump speed is on 1 so it can very likely be that my second guess is that there is something wrong withrb.velocity = new Vector2(rb.velocity.x, jumpSpeed); i always use forcemode.inpulse