- Home /
Dropping down from 2D platforms: Character gets "sprung back" if collision resumes mid-collider
Hey! When implementing a drop down behavior from floating platforms, I run into an issue with collision (I think). When a drop down sequence is initiated, Player's layer collision with the layer for these platforms is disabled momentarily or until the given input is released. If this release happens too fast, the collision is re-enabled while the character's collider is within the platform collider, causing weird behavior. Namely, if the majority of the character is above the collider, it gets "sprung back" into starting position.
I tried working around it in two main ways: 1) adding a timer after release, which keeps the collision disabled longer, so the character has a chance to pass low enough to avoid the "spring back", and 2) applying some additional downward force in the moment of initiating the drop. Together, these somewhat alleviate the issue, but introduce other problems. The timer can't be too short to work and too long to allow passing through lower platforms if the player doesn't want to, and the downward force can be only so strong before it starts looking unnatural.
In tutorials and guides I've looked into, they don't really deal with this issue. Also, these floating platforms have a PlatformEffector2D and a PolygonCollider2D components.
My main question is: is there a way to prevent this behavior? Or is there some better approach to dropping down from platforms? Thanks!
My dropping down code:
void DropDown()
{
if (movementInputVert < 0)
{
Physics2D.IgnoreLayerCollision(
LayerMask.NameToLayer("Player"),
LayerMask.NameToLayer("OneWayPlatform"),
true
);
dropDownResetTimer = dropDownResetTimerLimit;
return;
}
else if (dropDownResetTimer <= 0)
{
Physics2D.IgnoreLayerCollision(
LayerMask.NameToLayer("Player"),
LayerMask.NameToLayer("OneWayPlatform"),
false
);
return;
}
dropDownResetTimer -= Time.fixedDeltaTime;
}
Just to make it clearer - I realize this is most probably the correct and desired behavior when two colliding colliders intersect, I'm looking specifically for solutions to the dropping down spring back.
Answer by btCharlie · May 31, 2021 at 08:23 PM
I figured a solution after a few more days of testing and reading further into the docs.
TL;DR: I swapped Physics2D.IgnoreLayerCollision for Physics2D.IgnoreCollision.
Longer version: I achieved my goal through combination of
• Physics2D.IgnoreCollision within the OnCollisionStay2D method of the character
• downward force impulse to give the character a headstart
• coroutine to set a collision reset timer after a number of seconds
By using the IgnoreCollision method instead, I can manage the collision detection with each separate platform, which means I can afford making the collision reset timer long enough to avoid the spring back effect. This solution achieves multiple things, which I realized are kind of hard to find in virtually all guides and tutorials:
• only the character drops down
• the character drops only from the platform they started from, unless the flag to drop down isn't reset
• the timer doesn't affect collision with other pass-through platforms, so it can be longer
• by doing the check in OnCollisionStay2D, the character is stopped momentarily every time it hits a new platform, even if the drop down flag is set to true, which feels a bit more natural anyway
So, here's my code:
void OnCollisionStay2D(Collision2D other)
{
if (dropDownFlag & LayerMask.NameToLayer("OneWayPlatform") == other.gameObject.layer)
{
Physics2D.IgnoreCollision(capsuleCollider, other.collider, true);
rb.AddForce(Vector2.down * 100, ForceMode2D.Impulse);
StartCoroutine(ResetDropdownCollision(other.collider, dropDownResetTimerLimit));
}
}
IEnumerator ResetDropdownCollision(Collider2D otherCollider, float delayTime)
{
yield return new WaitForSeconds(delayTime);
Physics2D.IgnoreCollision(capsuleCollider, otherCollider, false);
}
I'm setting the dropDownFlag in my input method using the "new" input system. capsuleCollider is obviously the character collider. I have set the dropDownResetTimerLimit to 0.3 seconds. rb is the RigidBody2D component of the character.