Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by SeigneurNecron · Oct 28, 2016 at 07:45 PM · 2dcollision detectionplatforms

Best way to check collisions with diferent types of platforms on each side of the player in a 2D game.

Hi Unity Community, I'm new to Unity (and C#, but not to Object Oriented Programing, or programing in general). Also, I'm not a native english speaker, so I apologize for any english mistakes I could do.

I'm making a 2D platformer, and I'm wondering what's the best way to detect platforms on each side of my square-shaped player. I've read lots of threads about similar topics but I have some specific questions for which I could not find answers. I will try to quickly describe the gameplay I want to create, then I will ask the question itself.

My game contains both fixed platforms (no rigidbody) and moving/rotating platforms (kinematic rigidbody). These platforms can also be sticky and stop player movement/fall until the jump key is pressed. When the player is against a wall he doesn't loose velocity if he is going upward, but fall speed is reduced, and the player can wall jump. The game also contains water, which slows down movement.

So each time I go through the FixedUpdate function of my player script, I need to know if my player has platforms on each side, and if these platforms are sticky (sticky platforms are on a separate layer and are only detected on the center on each side of the player), and also if the player is under water to decide how to adjust his velocity.

Now the question is : what's the best way to do these repetitive tests? Here are a few ideas and an image to illustrate :

alt text

Solution 1 : using Physics2D.OverlapBox in FixedUpdate to update flag properties.

 this.OnGround = Physics2D.OverlapBox(worldPosition, boxSize, 0, layerMask);

This is the solution I'm currently using, but I feel like I'm doing unoptimized things I shouldn't do myself because the engine can probably do it better.

Solution 2 : children box colliders.

Here I add children to my player object with only a (non trigger) BoxCollider2D and no Rigidbody2D. I set the size of these children so they are perfectly aligned with the border of the player objet.

Solution 3 : children box triggers.

Here I do almost the same thing than in solution 2, except that the children go beyond the edges of the player object and their BoxCollider2D is set to trigger. These children actually test the same areas that I'm testing with solution 1.

In solution 2 and 3, I let the engine detect collisions (player Rigidbody2D has "Collision Detection" set to "Continuous"), but I'm wondering if this technique could fail, especially with solution 2. Also, in this case, do I have to increment/decrement a property for each collider each time On[Collision/Trigger][Enter/Exit]2D fires, or does this property already exist somewhere?

I've read about PhysicsMaterial2D and Friction, but since the physics in my game are not really realistic (especially wall slides), I didn't opt for that option. I've also read about PlatformEffector2D, but I doubt it would work well with rotating platforms.

What solution would you use? 1, 2, 3, something else, maybe totally different? why?

playercolliders.png (6.9 kB)
Comment
Add comment · Show 2
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 hexagonius · Oct 28, 2016 at 11:21 PM 0
Share

have you come across performance issues? have you ran the unity profiler on all three versions?
If performance is fine, don't bother of it works.

avatar image SeigneurNecron hexagonius · Oct 29, 2016 at 12:44 AM 0
Share

No, I don't have performance issues yet. A scene can contain at most two players, so there should be no problem. That's why I stick to solution 1 for now. I was just wondering if someone knew a better way to do this. It could be useful if I have more entities with that kind of behavior in the future. It could also help someone else with a similar problem. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Relan42 · May 11 at 05:55 PM

There's a function called OnCollisionEnter2D that is triggered whenever the player is touching anything and allows you to get information about the collision. This is what I would do.

 public bool isGrounded;
 
 //an array of contact points
 ContactPoint2D[] contacts;
 
 //Fill the array of contact points
 private void OnCollisionStay2D(Collision2D collision)
 {
               contacts = collision.contacts;
 }
 
 void Update()
 {
                if (contacts != null)
         {
 //this checks every contact point and sees if it is touching a horizontal surface from bellow
             foreach (ContactPoint2D contact in contacts)
             {
                 if (contact.normal == new Vector2(0, 1))
                 {
                     isGrounded = true;
                     break;
                 }
                 else
                 {
                     isGrounded = false;
                 }
 
 //if there are no contact points it isn't grounded
             if (rb.GetContacts(contacts) == 0)
             {
                 isGrounded = false;
             }
       }
 }
 
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

112 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

Related Questions

OnCollisionEnter2D issues in Unity 5.2 1 Answer

2D Collision 0 Answers

Collision between two 2D objects. 1 Answer

Compatible collision constraint methods 0 Answers

2d platformer 2 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