Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need help, simple C# jump script with rigid body

Discussion in 'Scripting' started by Artifacta, Mar 31, 2011.

  1. Artifacta

    Artifacta

    Joined:
    Mar 14, 2011
    Posts:
    22
    Ok, I have a game, where the player is in control of a ball, I wan't to be able to make this ball jump. Currently I'm using the following script to make my ball jump and move:

    Code (csharp):
    1. public float jumpSpeed = 100.0f;
    2.  
    3.     void Update ()
    4.     {
    5.         float amountToMove = movementSpeed * Time.deltaTime;
    6.         Vector3 movement = (Input.GetAxis("Horizontal") * -Vector3.left * amountToMove) + (Input.GetAxis("Vertical") * Vector3.forward * amountToMove);
    7.         rigidbody.AddForce(movement, ForceMode.Force);
    8.  
    9.         if (Input.GetKeyDown("space"))
    10.         {
    11.             rigidbody.AddForce(Vector3.up * jumpSpeed);
    12.         }
    13.  
    14.     }
    This script makes my ball jump exactly the way I wan't it to. But the problem is that I'm able to jump even though the ball is in the air. I have tried with a raycast, in my jump code so it looks like this:

    Code (csharp):
    1.         if (Input.GetKeyDown("space")  Physics.Raycast(transform.position, -transform.up,2))
    2.         {
    3.             rigidbody.AddForce(Vector3.up * jumpSpeed);
    4.         }
    But if I do this I'm often unabel to jump, both while moving and standing still. Does anyone have a smarter way of doing this, or checking if the player is touching the ground?

    Thanks ins advance.
     
    mateja_unity likes this.
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I would use a variable called "Grounded" at the end of your Update, set it to false. In the OnCollisionStay event, I would set it to true. So if it is colliding with something, it is "Grounded" but if it is not, then well... it is not.
     
  3. Artifacta

    Artifacta

    Joined:
    Mar 14, 2011
    Posts:
    22
    Thank you, works perfect!
     
  4. xxIaMsOePiCxx

    xxIaMsOePiCxx

    Joined:
    May 8, 2016
    Posts:
    2
    This doesn't work for me:

    using UnityEngine;
    using System.Collections;

    public class Jump : MonoBehaviour {

    public float jumpSpeed = 100.0f;
    private bool onGround = false;


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update()
    {
    float amountToMove = movementSpeed * Time.deltaTime;
    Vector3 movement = (Input.GetAxis("Horizontal") * -Vector3.left * amountToMove) + (Input.GetAxis("Vertical") * Vector3.forward * amountToMove);
    rigidbody.AddForce(movement, ForceMode.Force);

    if (Input.GetKeyDown("space"))
    {
    rigidbody.AddForce(Vector3.up * jumpSpeed);
    }

    void OnCollisionStay ();
    {
    onGround = true;
    }
    }
    }

    Please help
     
  5. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    there are a few errors in your script, i modified it a bit and it should work:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(Rigidbody))]
    5.  
    6. public class Jump : MonoBehaviour {
    7.  
    8.     public float jumpSpeed = 100.0f;
    9.     private bool onGround = false;
    10.  
    11.     Rigidbody rb;
    12.     // Use this for initialization
    13.     void Start ()
    14.     {
    15.         rb=GetComponent<Rigidbody>();
    16.     }
    17.  
    18.     float movementSpeed;
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         float amountToMove = movementSpeed * Time.deltaTime;
    23.         Vector3 movement = (Input.GetAxis("Horizontal") * -Vector3.left * amountToMove) + (Input.GetAxis("Vertical") * Vector3.forward * amountToMove);
    24.         rb.AddForce(movement, ForceMode.Force);
    25.    
    26.         if (Input.GetKeyDown("space"))
    27.         {
    28.             rb.AddForce(Vector3.up * jumpSpeed);
    29.         }
    30.    
    31.  
    32.     }
    33.  
    34.     void OnCollisionStay ()
    35.     {
    36.         onGround = true;
    37.     }
    38. }
     
    Last edited: May 11, 2016
    ChunkyToads likes this.
  6. ChunkyToads

    ChunkyToads

    Joined:
    Jun 21, 2017
    Posts:
    8
    @jaasso You are a lifesaver... THANK YOU so much for posting this :)
     
  7. TeraSpeed

    TeraSpeed

    Joined:
    Apr 8, 2022
    Posts:
    1
    You have no idea how long i have been looking for something like this