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. Dismiss Notice

Question Double jumping with Platform Effector

Discussion in '2D' started by aardvark2012, Jul 5, 2023.

  1. aardvark2012

    aardvark2012

    Joined:
    Nov 7, 2021
    Posts:
    6
    Hi

    I'm a new Unity user. As a learning project I'm attempting a 2D platformer based, at least initially, on the classic Manic Miner (because how hard could it be to remake a game that fits into 48KB, right?)

    I'm having difficulty getting two mechanics to work nicely together: jumping up through platforms (via a Platform Effector), and no double jumping. The problem is that basic GroundChecks (via an OverlapCapsule) allow for a second jump when the player is half-way jumped up through a platform. I've tried several ways of getting around this, and the one I've settled on is to (a) make the platform colliders thinner, and (b) trigger a ground check from a platform (tile or game object) when the player collides with the top part of the platform's collider:

    Code (CSharp):
    1. private void OnCollisionEnter2D(Collision2D collision)
    2.     {  
    3.         ContactPoint2D contactPoint = collision.GetContact(0);
    4.         Bounds bounds = GetComponent<TilemapCollider2D>().bounds;
    5.         if (collision.gameObject.TryGetComponent(out Player player) &&
    6.             contactPoint.point.y >= bounds.max.y - 0.05)
    7.         {
    8.             player.DoGroundCheckAndProcessWhateverPlayerIsStandingOn();
    9.         }
    10.     }
    11.  
    This seems to work fairly well. It's useful, in particular, because sometimes (a) the player needs to know what it's standing on (eg: conveyor belt), and (b) the platform needs to know that it's being stood on (eg: collapsing platforms). (It also keeps GroundChecks to a minimum... not that they're particularly expensive, but on general principle I'd prefer to minimize unnecessary calls.)

    But I'd like a reality check before I fully commit to this approach:
    1. Should I derive all platform classes from a base class that implements the above OnCollisionEnter2D, or should I be looking at interfaces, or something else?
    2. Can that OnCollision function be made more robust? It seems to work well so far, but I wouldn't want to swear to it being bug free.
    3. Is there's a better/cleaner/more standard way of doing this?
    4. Anything else that I don't know I don't know?
    Thanks for your time!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    So many ways to do stuff like this. If you're going for a particular classic feel to the jumping / motion you will almost certainly need to do the physics yourself.

    This shouldn't even be on your horizon. Focus on getting the feel you want. That's the hard part.

    Layers, separate colliders, checking the relative positions of objects, considering collider contact normals are just some of the kinds of things can be used to determine if you actually put your feet on something that should allow you to jump again.

    But all of these things are intimately tied into the design of your levels.

    If you just want yet-another-example, here's my take on a multi-jump, coyote-jump, jump-buffered 2D controller.



    Full source here: proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/