Jump using Rigidbody not working (C#)

I don’t really see a problem with the code, and there aren’t any errors in the console. But whenever I try to jump, nothing happens. Any suggestions or help would be awesome! :slight_smile:

private CharacterController controller;
    public float speed = 6.0f;
    private Vector3 moveDir = Vector3.zero;
    public float jumpSpd = 200.0f;
    public Rigidbody rb;
    private float gravity = 9.81f;

    void Start(){
        controller = GetComponent<CharacterController>();
        rb.GetComponent<Rigidbody>();
    }
   
    void Update () {
        Move ();
    }

    void Move(){
        //makes the Vector3 use the WASD inputs
        moveDir = new Vector3(Input.GetAxis("Horizontal"), 0,Input.GetAxis("Vertical"));
        //creates a transform out of the input
        moveDir = transform.TransformDirection(moveDir);
        //controls how high or low the transform #'s are
        moveDir *= speed;
        //push down at a constant rate
        moveDir.y -= gravity;
        //If I jump the gravity will still be in effect
        if (Input.GetKey (KeyCode.Space)){
            //push up for 10 "steps" at the jump speed
            rb.AddForce(Vector3.up * jumpSpd, ForceMode.Impulse);
        }
        //move the controller
        controller.Move(moveDir * Time.deltaTime);
    }

Since you are already using Move function you could modify Y axis of the MoveDir to make your character jump!

if (Input.GetKey(KeyCode.Space)) {
moveDir.y = jumpSpd;
}

Well, I’m not sure about why but I’m wondering why do you do gravity manually ?

You’re moving your object down -9.81f every update, without even using Time.deltaTime.

        //push down at a constant rate
        moveDir.y -= gravity;

If your GameObject has a RigidBody attached to it, you should be able to enable “Use Gravity”. That will do the work for you and it will do it correctly.

So, if “Use Gravity” is enabled, then I don’t have to worry about it in code? awesome.
Also,

I am now going to work towards using only the rigidbody and physics for movement. I’ll post the updated code when I can if anyone wants to continue monitoring the thread.
Thanks Guys!