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

How to make a ball jump

Discussion in 'Scripting' started by Manou21, Mar 31, 2016.

  1. Manou21

    Manou21

    Joined:
    Mar 30, 2016
    Posts:
    4
    Hi, i started using Unity recently and i made the "Roll a Ball" Project. Then i tried to made some modifications such as make the ball jump. This is the code i have on my player script.
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;


    public class PlayerController : MonoBehaviour {

    public float speed;
    public Text countText;
    public Text winText;
    public float jump;

    private bool isFalling = false;
    private Rigidbody rb;
    private int count;


    void Start ()
    {
    rb = GetComponent<Rigidbody>();
    count = 0;

    jump = 9;
    SetCountText ();
    winText.text = "";
    }

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



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


    rb.AddForce(movement * speed);


    }



    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag ( "Pick Up"))
    {
    other.gameObject.SetActive (false);
    count = count + 1;
    SetCountText ();
    }
    }

    void SetCountText ()
    {
    countText.text = "Count: " + count.ToString ();
    if (count >= 9)
    {
    winText.text = "You Win!";
    }
    }
    }

    Then i saw on some other forums on the internet this modification in FixedUpdate that was supposed to work but it didn't for me.Plus some bollean ton ensure that i can only jump when i'm on ground.I get an error about addforce

    Can anyone help?


    • if(Input.GetKeyDown(KeyCode.Space)){
    • this.rigidbody.AddForce(newVector3(0,jump,0));
    • }
     
  2. IL4Mi3y

    IL4Mi3y

    Joined:
    Dec 29, 2014
    Posts:
    38
    What error do you get?
    This works for me
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof (Rigidbody))] // force to use rigidbody
    5. public class Jump : MonoBehaviour {
    6.  
    7.     public Vector3 direction = new Vector3(0.0f, 1.0f, 0.0f); // Jump direction
    8.     public float force = 10.0f; // Force
    9.  
    10.  
    11.     // Use this for initialization
    12.     void FixedUpdate () {
    13.         if (Input.GetButtonDown("Fire1")) // Button trigger
    14.             this.jump ();
    15.     }
    16.  
    17.     private void jump()
    18.     {
    19.         var rb = this.GetComponent<Rigidbody> ();
    20.         if (rb != null)
    21.         {
    22.             rb.AddForce(this.direction * this.force, ForceMode.Impulse); // Use "force" as scalar for the directional vector
    23.         }
    24.     }
    25. }