Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnCollisionEnter2D not working 100% when jumping up and down....

Discussion in '2D' started by cst42, Sep 1, 2014.

  1. cst42

    cst42

    Joined:
    Mar 10, 2014
    Posts:
    12
    Okay so I have a 2D platformer and when my character jumps from one platform to another I witness a strange effect.

    When he jumps and I still have the jump button held down, as he is making contact with the platforms Collider, the OnCollisionEnter2D method doesn't get called, all the time.

    Instead there are these sweet spots where continually holding down the jump button allows my character to immediately jump up off the platform the second it makes contact. Thus not calling the collision method.

    It's only when I apply a slight change in my characters horizontal movement while jumping, or come to rest on the platform, that the collision is detected.

    What I am not understanding is, since my character is able to jump on the surface of my platforms then obviouslty my character has made contact with the platform. So why is it the OnCollisionEnter2D does not get called every single time, that occurs?


    My Set-up

    Character:
    - has Rigidbody2D
    - Circle Collider2D for the feet
    - isTrigger is false
    - no double jumping is allowed.

    Platform:
    - EdgeCollider2D
     
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Two possible reasons come immediately to mind: First, that you're allowing the jump before contact occurs, second, that the jump is disengaging contact before the OnCollisionEnter2D is called, thereby preventing it from getting called. Just speculation, though, since you didn't post any code.
     
    cst42 likes this.
  3. cst42

    cst42

    Joined:
    Mar 10, 2014
    Posts:
    12
    Hi thanks for your reply.

    Here is my code segment related to jumping. I can only jump if my grounded bool is true.




    Code (CSharp):
    1.  
    2. grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
    3.  
    4. interactiveGrounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Interactive GameObjects"));
    5.  
    6.  
    7.  
    8. if ( Input.GetAxis("Vertical") > 0 )
    9. {
    10.         if (grounded || interactiveGrounded)
    11.         {
    12.                 rigidbody2d.AddForce(Vector2.up * keyboardUpForce);
    13.         }
    14. }
     
  4. cst42

    cst42

    Joined:
    Mar 10, 2014
    Posts:
    12
    Assuming you're correct about the jump disengaging contact before the OnCollisionEnter2D is called, what can I do to circumvent this?

    The platform is an endless runner in the vertical direction and in my OnCollisionEnter2D method, depending on which platform my character is colliding with, it will rearrange the platforms my character has yet to collide with.
     
  5. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    You check if the player is grounded and allow him to jump again in the Update, using your groundCheck GameObject, maybe (almost 100% sure) your groundCheck hits the ground before the collider and allow the player to jump again before the contact occur.
    Try to move your groundCheck up a little.
     
    cst42 likes this.
  6. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    You could have the OnCollisionEnter2D itself enable the jump, perhaps in addition to whatever you're using now.
     
    cst42 likes this.
  7. cst42

    cst42

    Joined:
    Mar 10, 2014
    Posts:
    12

    Thanks for your suggestion. I tried this and it still didn't work. I even placed the groundcheck above the head of my character, but still nothing.
     
  8. cst42

    cst42

    Joined:
    Mar 10, 2014
    Posts:
    12
    Thanks, but I tried this as well and it doesn't even allow me to jump or move laterally.

    My problem is pretty simple to check btw. Just make a new scene with one platform and have a cube jump up and down. And have a debug.log(coll.gameobject), in your OnCollisionEnter2D. You will see it doesn't get called 100% of the time. Not sure why.
     
  9. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Attempted to replicate with a box collider jumping on an edge collider. Could not; it worked perfectly. Every single landing generated a log.

    Here's my test code:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class JumpScript : MonoBehaviour {
    5.  
    6.     public float JumpForce;
    7.     bool JumpFlag = false;
    8.  
    9.     void Update () {
    10.         if (Input.GetButtonDown ("Spacebar"))
    11.             JumpFlag = true;
    12.     }
    13.  
    14.     void FixedUpdate () {
    15.         if (JumpFlag) {
    16.             Debug.Log ("Jump at " + Time.timeSinceLevelLoad.ToString ());
    17.             rigidbody2D.AddForce (new Vector2 (0.0F, JumpForce));
    18.             JumpFlag = false;
    19.         }
    20.     }
    21.  
    22.     void OnCollisionEnter2D(Collision2D coll) {
    23.         Debug.Log ("Collision at " + Time.timeSinceLevelLoad.ToString ());
    24.     }
    25. }
     
    cst42 likes this.
  10. cst42

    cst42

    Joined:
    Mar 10, 2014
    Posts:
    12
    Thanks, Pyrian. You're script adjustment, works 100%.