Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 OriginalSINe · Mar 01, 2017 at 07:23 AM · collisioncolliderrigidbody2dfixedupdateoncollisionexit

Why is OnCollisionExit2D late?

I am having a problem with the "OnCollisionExit2D" callback. It seems to be occurring one physics update too late.

According to the documentation, it simply calls when a collider is no longer touching another. According to the documentation on execution order, it occurs after the physics update.

So if I move a Rigidbody2D in FixedUpdate, it would seem that this is the sequence of events that should happen:

  1. Set Rigidbody2D velocity in FixedUpdate

  2. Internal physics update (rigidbody moves)

  3. OnCollisionExit2D is called

However, the ACTUAL sequence of events is something like this:

  1. Set Rigidbody2D velocity in FixedUpdate

  2. Internal physics update (rigidbody moves)

  3. OnCollisionStay2D is called

  4. FixedUpdate

  5. OnCollisionExit2D is called

Here is some test code I wrote to confirm this problem:

     int timesteps = 0;
 
     Rigidbody2D _rigidbody;
 
     // Use this for initialization
     void Start()
     {
         _rigidbody = gameObject.GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         timesteps++;
 
         Vector3 velocity = new Vector3();
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             Debug.LogFormat("Moved {0}", timesteps);
             velocity.y += 10000;
         }
 
         velocity.y -= 1;
 
         _rigidbody.velocity = velocity;
     }
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         Debug.LogFormat("Enter {0}", timesteps);
     }
 
     private void OnCollisionStay2D(Collision2D collision)
     {
         Debug.LogFormat("Stay {0} {1}", timesteps, transform.position);
     }
 
     private void OnCollisionExit2D(Collision2D collision)
     {
         Debug.LogFormat("Exit {0}", timesteps);
     }

Here is the logging for this code: alt text

Why is this happening? Shouldn't the physics engine be aware that it stopped colliding? It seems like it is calling the OnCollisionExit2D on the physics update AFTER it stops colliding, rather than the same one. Is this always the case? Is there some way I can change this behavior? My game requires that the characters always know if they are on the ground after the physics update. Can/ should I use raycasting instead?

unity.png (8.8 kB)
Comment
Add comment · Show 3
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 Masterio · Mar 01, 2017 at 03:00 PM -1
Share

I think raycast is better.

avatar image Tortuap Masterio · Oct 25, 2017 at 04:20 PM 0
Share

Well.. yes, using Physics2D.Raycast can give you RaycastHit2D which contains the same information than Collision2D.

avatar image Tortuap · Oct 25, 2017 at 04:19 PM 0
Share

Hi I have the exact same issue.

  1. FixedUpdate : I'm changing velocity

  2. (internal physics update)

  3. FixedUpdate : I'm getting an updated rigidbody position, it has moved away from the touched collider, but Physics still 'think' it collides (tested using IsTouching).

  4. (internal physics update)

  5. OnCollisionExit2D : we now get the information that it has moved away from the touched collider

  6. FixedUpdate : Now IsTouching returns false, it's one FixedUpdate too late !

In the FixedUpdate where your rigidbody position has been updated, the collision state is wrong (by listening to callbacks Enter2D/Exit2D, or by calling IsTouching method) because not updated yet (while the position has been !).

I have reproduced this behavior in a new project.

That's really annoying.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tortuap · Oct 25, 2017 at 07:48 PM

I have created a ticket for that issue : https://fogbugz.unity3d.com/default.asp?962743_hdc18gmnt0jcl55j

Also I found those possible workarounds :

  • method OverlapCollider gives a correct result

  • method Distance gives a correct result

  • method Raycast gives you RaycastHit2D which contains the same info than Collision2D

Also note that when teleporting the rigidbody using rigidbody.position, the issue does not happen. While issue is produced when moving the rigidbody using rigidbody.AddForce, rigidbody.velocity or rigidbody.MovePosition.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Player appears on top of an Kinematic object or underneath it when walking forward. 0 Answers

How do I get collisions between Tilemap Collider 2d and a Kinematic Rigidbody 2d? 1 Answer

Rigidbody2D.attachedColliderCount question C# 0 Answers

Reset Rigidbody2D rotation after collision 2 Answers

Buggy touch controls? 0 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