Search Unity

Can't make 2D Sprite jump

Discussion in '2D' started by FractusA, Mar 13, 2021.

  1. FractusA

    FractusA

    Joined:
    Feb 4, 2021
    Posts:
    1
    Hello,

    I've tried using OnCollisonEnter as well as OnTriggerEnter but to no avail. I am simply unable to make my player successfully jump. I believe I've narrowed it down to the Colliders not working.

    I got Box Colliders and Rigidbody2D on both my player character and the ground.

    I'm using the following code:

    Code (CSharp):
    1.  
    2. void Update()
    3.     {
    4.         playerMovement();
    5.  
    6.         if(Input.GetKeyDown(KeyCode.Space) && isOnGround)
    7.         {
    8.             playerRb.AddForce(Vector2.up, ForceMode2D.Impulse);
    9.             isOnGround = false;
    10.         }
    11.            
    12.     }
    13.  
    Code (CSharp):
    1. void OnCollisionEnter2D(Collision collision)
    2.      {
    3.         if (collision.gameObject.tag  == "Ground")
    4.         {
    5.             isOnGround = true;
    6.             Debug.Log("Player has jumped");
    7.         }
    8.      }
     
  2. Wezai

    Wezai

    Joined:
    Dec 30, 2016
    Posts:
    74
    It could be the case that the force you are using is too little. Experiment multiplying it to 10 or 100 and see if it works.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Sounds like you're just guessing randomly which is never going to end well. OnCollisionEnter and OnTriggerEnter are for 3D physics so they'll only be called for that.

    Have you done basic debugging by putting in a breakpoint here or even outputting something to the console log? I suspect not because the method signature is wrong.

    As you can see in the docs, OnCollisionEnter2D is passed a Collision2D not a Collision (3D) one. The function you've got above won't be called by Unity, it's just a custom function you've created.