Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by prototyped · Jul 28, 2020 at 02:22 AM · unity 2dcollider2dgroundedground detection

inconstant ground check on two identical platforms

so I have two Identical platforms both using a tilemap collider, using the oncollisonenter and exit2d the player is checking for if they are grounded. the player is fine on the first collider but the second collider they arent grounded and both platforms are identical. UPDATE this only happens when i run onto the next platform now when I jump onto it.

Code:

     private void OnCollisionEnter2D(Collision2D collisionInfo)
     {
         if (collisionInfo.collider.tag == "Ground")
         {
             isGrounded = true;
             animator.SetBool("isGrounded", true);
 
         }
     }
 
     private void OnCollisionExit2D(Collision2D collisionInfo)
     {
         if (collisionInfo.collider.tag == "Ground")
         {
             isGrounded = false;
             animator.SetBool("isGrounded", false);
 
         }
     }

also im using an edge collider that goes accrose the bottom of the players feet if tht info is helpful at all alt text

player.png (23.6 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · Jul 28, 2020 at 05:25 AM

OnCollisionEnter() triggers only when your character first steps onto a platform. When you step onto an adjacent platform, you will (likely) actively be standing on both of them at the same time.

When you step off of the first platform entirely, and are now standing only on the second platform, you don't have to leave the ground; you're *still* on the second platform, and are no longer on the first. At that time, you will no longer count as "grounded" because you only checked at that one moment.

Luckily, there are multiple solutions to this.

The first, (likely) more robust option is to use OnCollisionStay() instead of OnCollisionEnter(). This way, you'll rapidly verify whether you're on the ground. Then, when you're not on any ground, you will no longer be "grounded".

 private void OnCollisionStay2D(Collision2D collisionInfo)
 {
     isGrounded = true;
 }


The second solution should work barring random, bad luck of receiving an unequal number of messages about collisions. By keeping count of the number of active collisions, you should be able to update it only during OnCollisionEnter() and OnCollisionExit() instead.

 private int groundedCount = 0;
 public bool isGrounded // or private, whatever you were using
 {
     get
     {
         return groundedCount > 0; // you're only airborne when 0 (no ground contact)
     }
     set
     {
         groundedCount += value ? 1 : -1; // +1 when true, -1 when false
     }
 }

 private void OnCollisionEnter2D(Collision2D collisionInfo)
 {
     isGrounded = true; // count +1
 }

 private void OnCollisionExit2D(Collision2D collisionInfo)
 {
     isGrounded = false; // count -1
 }

It may seem a little odd to say isGrounded = false; and still see it report true (when still in contact with other surfaces), but this approach would allow you to use the same boolean-based formatting, especially when reading the value.

Comment
Add comment · Show 1 · 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
avatar image prototyped · Jul 28, 2020 at 05:51 PM 0
Share

okay with on collision stay its not changing the is grounded when the player jumps, but it does fix the platform changing problem.

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

141 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

Related Questions

How to jump on only "ground" colliders 1 Answer

Grounded check not working on sloped ground. 1 Answer

Platform Collider Not Working When Enemy Collides With It 1 Answer

2D Isometric (2018.3b) - Collider 0 Answers

Trigger Triggers Without Collision 1 Answer


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