My sphere can't jump

Hello guys, my sphere on the floor can’t jump ,here is my code

using UnityEngine;
using System.Collections;

public class jump : MonoBehaviour {
    public float speed;
    public float jumppower;
    bool grounded = false ;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
        if (Input.GetKeyDown(KeyCode.Space)){
            if (grounded){
                GetComponent<Rigidbody2D>().velocity = new Vector2(
                GetComponent<Rigidbody2D>().velocity.x, jumppower);
              } 
          }
     }
   void OnTriggerEnter2d()
    {
        grounded = true;
    }
    void OnTriggerExit2d()
    {
        grounded = false;
    }
}

I think you code is not working because of the way you manage colliders.

  • Colliders should only be marked as "Is Trigger" when you don't want that collider to bounce with others, neither interact with other colliders phisically. "trigger colliders" are useful to control when an object (with a normal collider attached) enters inside a zone, for example.
  • The "ground" colliders mustn't be triggers. In addition, you have to use the following functions rather than `OnTriggerEnter2D` and `ÒnTriggerExit2D`
  • void OnCollisionEnter2d(Collision2D coll)
    {
        grounded = true;
    }
    void OnCollisionExit2d(Collision2D coll)
    {
        grounded = false;
    }
    

    OnTriggerEnter2D and OnTriggerExit2D are triggered when a collider goes into a “trigger collider”, so they are not suitable for your needs.

    Please notice the arg “coll” declared in the function header. It’s inclusion is optional, but, if you include it, you can obtain from coll the collision points and the relative velocity at the moment the collision is produced. But, if you don’t need those data, not including this parameter is the right choice due to the code will avoid unnecessary calculations this way.

    You can find more information about this in the related Unity docs.


    It seems there are also errors in this fragment of your code:

    if (grounded){
        GetComponent<Rigidbody2D>().velocity = new Vector2(
        GetComponent<Rigidbody2D>().velocity.x, jumppower);
    }
    

    The first GetComponentline is incomplete, so it would trigger a SyntaxError (maybe it is just a typo in the question itself?). But even if it was correct in your actual script, you would be modificating velocity twice! Now I see what you did there. It might work, but it is an ugly way to achieve it.

    In addition, trying to modificate velocity directly looks weird for me. I am not sure, but I guess velocity is just a Read-only property.

    This should work:

    if (grounded){
        //   If you roll the ball using AddForce or similar, you don't need to include
        //  horizontal velocity in the jump vector
        Vector2 jump_force = new Vector2(0, jumppower);
        GetComponent<Rigidbody2D>().AddForce(jump_force, ForceMode2D.Force);
    }
    

    I hope this helps.