Adding jumping to the roll-a-ball tutorial.

Hello, I would like to add jumping to the roll a ball tutorial. I have decided to expand on the game, and jumping is what I want to do next.

void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);

        rb.AddForce (movement * speed);

How would I add jumping to this?

Add an if statement to check for whatever key you want to use as the jump & add an impulse force vector3.up*[whatever jump force you want]

You will probably need a large jump force to get it to show, maybe start with a force of 5000 to see how it goes. You will also want to include a check that the ball is grounded in your if statement otherwise it will jump every time you press the jump key even if the ball is in midair.

    if (other.gameObject.CompareTag("Ground"))
    {
      grounded = true;
    }
    else
    {
      grounded = false;
    }

I have done this to check for grounded yet it does not work. I am working on the jump now

Where are you doing that check? In OnCollisionStay ?

I was actually doing it on OnTriggerEnter, similar to the pickups. I’ll try that now, thanks for putting up with me

      void OnCollisionStay(Collision other)
      {
          if (other.gameObject.CompareTag("Ground"))
          {
              grounded = true;
          }
      }

      void OnCollisionExit(Collision other)
      {
          if(other.gameObject.CompareTag("Ground"))
          {
              grounded = false;
          }
      }

This isn’t working, it says its always grounded

Its the collision exit code, it doesn’t run it (Checked with print in console)

Thats because you are checking for a floor collision when you are no longer touching the floor!
Try just removing the if check and just setting grounded to false on collision exit.

I ended up just doing it with Raycasting, I’ll post the code I use

        if (Input.GetKeyDown("space"))
        {
            RaycastHit hit;
            if(Physics.Raycast(transform.position,-Vector3.up, out hit, 0.5f))
            {
                grounded = true;
            }
            else
            {
                Debug.Log("Nothing Below");
                grounded = false;
            }

            if (grounded == true)
            {
                rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
                jump.Play();
1 Like