Problem with a jump script

Hello,

I liked the Roll a ball tutorial you can find here : https://unity3d.com/fr/learn/tutorials/projects/roll-ball-tutorial

I wanted to work more on this project and decided to add levels, some effects…

And since yesterday, I want to add a jump script. So i found how to jump with tutorials. But the problem is I want the jump to consider the speed of the sphere. If i go fast, i’ll jump further. The problem at the moment is : if I move fast, stop pressing the arrows (but the ball is still rolling) and press the jump key, it’ll jump straight. If i keep pressing the arrow, it’s like the speed will start from 0 (and not the speed i had when the ball was rolling). I did a video on YouTube to show you :

(first jump is when i stop pressing the arrows, second is when i’m still pressing them). What can I do ?

Here is my code (i kept everything in the code, even if it’s not the jump script) :

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

public class ControllingSphere : MonoBehaviour {

    public float speed;
    private int count = 0; // Number of cubes collected
    public int counttotal = 99; // Number of cubes in the scene
    public int goToSceneNumber = 99; // Go to this scene when all the cubes are collected
    public bool onGround; // Sphere grounded or not
    private Rigidbody rb;


    private void Start()
    {
        rb = GetComponent<Rigidbody>();
        onGround = true;
    }



    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            SceneManager.LoadScene(goToSceneNumber - 1); // Retry the level when R is pressed
        }
        if (onGround == true) // If the sphere is grounded
        {
           
            if (Input.GetKeyDown(KeyCode.Space)) // And if the player press Space
            {
               
                rb.velocity = new Vector3(0f, 5f, 0f); // The sphere will jump
                onGround = false; // The sphere can't jump if it's in the air
            }

        }


    }


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

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

        rb.AddForce(movement * speed);  // Controlling the character

    }

    private void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.CompareTag("Ground")) // If the sphere is touching the floor (tagged Ground)
        {
            onGround = true; // The sphere can jump again
        }
    }

    void OnTriggerEnter(Collider other)
    {

        if (other.gameObject.CompareTag("Pickups")) // If the sphere collect a cube
        {
            other.gameObject.SetActive(false); // This cube is disabled

            count++;
        }
        else if (other.gameObject.CompareTag("Classical")) // If the sphere collect a classical cube
        {
            other.gameObject.SetActive(false); // This cube is disabled
            transform.localScale += new Vector3(0.1F, 0.1F, 0.1F); // Make the sphere bigger

            count++;
        }
        else if (other.gameObject.CompareTag("Smaller")) // If the sphere collect a smaller cube
        {
            other.gameObject.SetActive(false); // This cube is disabled
            transform.localScale += new Vector3(-0.1F, -0.1F, -0.1F); // Make the sphere smaller
            count++;
        }
        if (count == counttotal)
        {
            SceneManager.LoadScene(goToSceneNumber); //If the sphere collected all the cubes, we go to the next level
           
           
        }
    }
}

You’re setting the velocity directly in your jump, which will override all other forces acting on the ball.

Try using AddRelativeForce() with just a Y component instead.

Thanks for your answer, so instead of : rb.velocity = new Vector3(0f, 5f, 0f);
I added :

rb.AddRelativeForce(0f, 5f, 0f);

And that didn’t work. That didn’t even jump while i was rolling.

I also tried :
rb.AddRelativeForce = new Vector3(0f, 5f, 0f);

But that gave me an error.

Maybe i did something wrong ? Sorry I’m a beginner

EDIT: My mistake, you should be using AddForce not AddRelativeForce, as the ball will be rolling over on itself and we want to use world space.

Nothing wrong with being a beginner, you don’t have to apologize. You’re just not applying enough force to counteract gravity (-9.8 by default). Increase the force and try changing the force mode from the default force (which is continuous) to impulse, which has a more “front loaded” effect.

rb.AddForce(0f, 10f, 0f, ForceMode.Impulse);

That should get it hopping.

1 Like

that’s working ! Thanks a lot ! :):slight_smile:

1 Like