Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by btCharlie · May 18, 2021 at 07:05 PM · 2dcollisioncollider2dplatformerdropdown

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;
 }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image btCharlie · May 20, 2021 at 03:21 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

325 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

One-way platform's side causes collision with Platform Effector 1 Answer

Make 2D projectile collide only from the outside 1 Answer

Collision not working for 2d platformer 2 Answers

Is there a way for 2D Box Colliders to ignore another? 4 Answers

Player color change on collision -1 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges