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

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

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.

1 Like

Hi thanks for your reply.

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

grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));

interactiveGrounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Interactive GameObjects"));



if ( Input.GetAxis("Vertical") > 0 )
{
        if (grounded || interactiveGrounded)
        {
                rigidbody2d.AddForce(Vector2.up * keyboardUpForce);
        }
}

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.

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.

1 Like

You could have the OnCollisionEnter2D itself enable the jump, perhaps in addition to whatever you’re using now.

1 Like

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.

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.

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:

using UnityEngine;
using System.Collections;

public class JumpScript : MonoBehaviour {

    public float JumpForce;
    bool JumpFlag = false;

    void Update () {
        if (Input.GetButtonDown ("Spacebar"))
            JumpFlag = true;
    }

    void FixedUpdate () {
        if (JumpFlag) {
            Debug.Log ("Jump at " + Time.timeSinceLevelLoad.ToString ());
            rigidbody2D.AddForce (new Vector2 (0.0F, JumpForce));
            JumpFlag = false;
        }
    }

    void OnCollisionEnter2D(Collision2D coll) {
        Debug.Log ("Collision at " + Time.timeSinceLevelLoad.ToString ());
    }
}
1 Like

Thanks, Pyrian. You’re script adjustment, works 100%.