Search Unity

Problem with a jump script

Discussion in 'Scripting' started by Vivaldi1988, Oct 22, 2017.

  1. Vivaldi1988

    Vivaldi1988

    Joined:
    Oct 22, 2017
    Posts:
    26
    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) :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class ControllingSphere : MonoBehaviour {
    7.  
    8.     public float speed;
    9.     private int count = 0; // Number of cubes collected
    10.     public int counttotal = 99; // Number of cubes in the scene
    11.     public int goToSceneNumber = 99; // Go to this scene when all the cubes are collected
    12.     public bool onGround; // Sphere grounded or not
    13.     private Rigidbody rb;
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody>();
    19.         onGround = true;
    20.     }
    21.  
    22.  
    23.  
    24.     private void Update()
    25.     {
    26.         if (Input.GetKeyDown(KeyCode.R))
    27.         {
    28.             SceneManager.LoadScene(goToSceneNumber - 1); // Retry the level when R is pressed
    29.         }
    30.         if (onGround == true) // If the sphere is grounded
    31.         {
    32.            
    33.             if (Input.GetKeyDown(KeyCode.Space)) // And if the player press Space
    34.             {
    35.                
    36.                 rb.velocity = new Vector3(0f, 5f, 0f); // The sphere will jump
    37.                 onGround = false; // The sphere can't jump if it's in the air
    38.             }
    39.  
    40.         }
    41.  
    42.  
    43.     }
    44.  
    45.  
    46.     private void FixedUpdate()
    47.     {
    48.         float moveHorizontal = Input.GetAxis("Horizontal");
    49.         float moveVertical = Input.GetAxis("Vertical");
    50.  
    51.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    52.  
    53.         rb.AddForce(movement * speed);  // Controlling the character
    54.  
    55.     }
    56.  
    57.     private void OnCollisionEnter(Collision other)
    58.     {
    59.         if (other.gameObject.CompareTag("Ground")) // If the sphere is touching the floor (tagged Ground)
    60.         {
    61.             onGround = true; // The sphere can jump again
    62.         }
    63.     }
    64.  
    65.     void OnTriggerEnter(Collider other)
    66.     {
    67.  
    68.         if (other.gameObject.CompareTag("Pickups")) // If the sphere collect a cube
    69.         {
    70.             other.gameObject.SetActive(false); // This cube is disabled
    71.  
    72.             count++;
    73.         }
    74.         else if (other.gameObject.CompareTag("Classical")) // If the sphere collect a classical cube
    75.         {
    76.             other.gameObject.SetActive(false); // This cube is disabled
    77.             transform.localScale += new Vector3(0.1F, 0.1F, 0.1F); // Make the sphere bigger
    78.  
    79.             count++;
    80.         }
    81.         else if (other.gameObject.CompareTag("Smaller")) // If the sphere collect a smaller cube
    82.         {
    83.             other.gameObject.SetActive(false); // This cube is disabled
    84.             transform.localScale += new Vector3(-0.1F, -0.1F, -0.1F); // Make the sphere smaller
    85.             count++;
    86.         }
    87.         if (count == counttotal)
    88.         {
    89.             SceneManager.LoadScene(goToSceneNumber); //If the sphere collected all the cubes, we go to the next level
    90.            
    91.            
    92.         }
    93.     }
    94. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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.
     
  3. Vivaldi1988

    Vivaldi1988

    Joined:
    Oct 22, 2017
    Posts:
    26
    Thanks for your answer, so instead of :
    Code (CSharp):
    1. rb.velocity = new Vector3(0f, 5f, 0f);
    I added :

    Code (CSharp):
    1. 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
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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.

    Code (csharp):
    1.  
    2. rb.AddForce(0f, 10f, 0f, ForceMode.Impulse);
    3.  
    That should get it hopping.
     
    Vivaldi1988 likes this.
  5. Vivaldi1988

    Vivaldi1988

    Joined:
    Oct 22, 2017
    Posts:
    26
    that's working ! Thanks a lot ! :):)
     
    GroZZleR likes this.