Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Implementing Fallthrough Platform Using 2D Physics

Discussion in 'Physics' started by Hyblademin, Mar 21, 2015.

  1. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    I'm working on standard aerial platforms that the player can jump through from below, and also hop down from using the down and jump keys.

    To do this, I've added layers for "Fallthrough" and "FallthroughSolid" and disabled collisions between the Player and Fallthrough layers (the FallthroughSolid layer was created instead of using the Wall layer because certain projectiles should never collide with aerial platforms). In the platform's behavior script, the position of the player object is observed, and the platform switches layers depending on whether the foot of the player object is above the top of the platform.

    This works pretty well, but it seems that there's something I'm missing about changing layers: sometimes, when the player jumps just high enough that they should be able to land on the platform, they will still fall through. This is not a numerical error-- the layer of the platform can be seen changing correctly in the inspector, but no physics collision happens unless the player jumps sufficiently high enough above the platform, implying that somehow the physics engine isn't seeing the change for at least a few frames.

    If I attempt to compensate for this by moving the minimum height to change layers, the player may instead collide with the edge of the platform when jumping in from the side, stopping horizontal movement. Both of these things would be frustrating for the player and would take all the fun out of the game. Is there some obscure detail or principle regarding collision layers that I don't know about preventing this method from working quite right?

    NOTES:

    -As stated, this only happens sometimes, probably one out of every three instances where the conditions are right.

    -I've seen "solutions" like the one linked below, wherein the player's collider is temporarily disabled, but I'm unsatisfied with this as there would not be any other physics interactions allowed for a short time. Too sloppy: http://forum.unity3d.com/threads/forcing-a-character-to-fall-through-a-platform.222020/

    -Something I've noticed is that the physics engine ignores changes in collider layers between two colliding objects until they exit their current collision. This shouldn't mean anything for my issue, because all colliders are clear of each other before the player falls through, but I did need to make the character hop slightly to get him to fall back down intentionally from standing atop the platform.

    -An example of the layer changing code is shown below.

    Code (JavaScript):
    1.  
    2. var player    :    Transform;
    3. var boxCollide    :    BoxCollider2D;
    4.  
    5. function Start()
    6. {
    7.  
    8.     boxCollide = GetComponent(BoxCollider2D);
    9.  
    10. }
    11.  
    12. function FixedUpdate()
    13. {
    14.  
    15.     if (player.position.y - 1.25*player.localScale.y >= transform.position.y + boxCollide.size.y*transform.localScale.y/2)
    16.     {
    17.         gameObject.layer = 14;
    18.     }
    19.     else
    20.     {
    21.         gameObject.layer = 13;
    22.     }
    23.  
    24.     if (Input.GetAxis("Vertical") < 0 && Input.GetButton("Jump"))
    25.     {
    26.         gameObject.layer = 13;
    27.     }
    28.  
    29. }
    In the above, the player transform is added in the inspector. The first conditional in FixedUpdate() compares the bottom of the player object with the top of the platform object. Layer 14 is FallthroughSolid, 13 is Fallthrough.

    -I originally had the layer changing in Update(). Moving them to FixedUpdate() didn't seem to do anything.

    -I can't get this effect to happen while moving the character horizontally. It only seems to happen while jumping straight up.
     
    Last edited: Mar 21, 2015