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 commodore · Sep 19, 2012 at 03:14 PM · physicsoncollisionenterorderexecution

OnCollisionEnter before Physic simulations?

Hello, my game relies on collision detection but every time I call OnCollisionEnter it's always a frame too late because OnCollisionEnter is called after FixedUpdate and all the physics simulations. Is there a way around this?

Comment
Add comment · Show 7
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 Fattie · Sep 19, 2012 at 03:19 PM 0
Share

I don't follow exactly but are you looking for LateUpdate() ? (see docs)

avatar image Khada · Sep 19, 2012 at 05:32 PM 0
Share

OnCollisionEnter should be called before the physics calculation are applied to the 2 colliding objects in question. OnCollisionExit is called the following frame.

avatar image commodore · Sep 19, 2012 at 08:09 PM 0
Share

Thanks for the responses. It says here that the OnCollision functions are called after the Physics simulations

http://docs.unity3d.com/Documentation/$$anonymous$$anual/ExecutionOrder.html

 All Awake calls
 All Start Calls
 while (stepping towards variable delta time)
     All FixedUpdate functions
     Physics simulation
     OnEnter/Exit/Stay trigger functions
     OnEnter/Exit/Stay collision functions 
 Rigidbody interpolation applies transform.position and rotation
 On$$anonymous$$ouseDown/On$$anonymous$$ouseUp etc. events
 All Update functions
 Animations are advanced, blended and applied to transform
 All LateUpdate functions
 Rendering 
avatar image AlucardJay · Sep 19, 2012 at 08:34 PM 1
Share

Have an empty gameObject as a child with a collider set to trigger, and slightly larger than the parent collider?

avatar image Khada · Sep 19, 2012 at 08:35 PM 0
Share

I highly suspect that list to be misleading and that the OnEnter callback would actually be called from within the physics simulation stage.

I could always be wrong.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by DanJC · May 06, 2013 at 05:49 PM

Just in case anyone else is looking along these lines, the short answer is no.

All of the OnCollision*() functions are called after the physics simulation is completed. When OnCollisionEnter() is called, transform.position will already reflect the new location.

The way around it is to manually look for upcoming collisions with your own code.

The further downside to the OnCollision*() functions is that they do not detect a collision in response to the physics simulation. They detect a collision in response to where the object was when it started FixedUpdate().

In other words, if an object is at point A, moves to point B, and collides at point B, the OnCollisionEnter() function will not catch this as a collision because at the start of FixedUpdate() the object was still at point A. This is regardless of anything that took place in the physics simulation, even if the collision is clearly apparent on screen, even if transform.position is clearly in a place where a collision must exist. OnCollision*() will not be called until after the FixedUpdate() (AND after that FixedUpdate()'s following physics simulation) in which the object started in a colliding position.

Which means that the collision at point B will be called after the object has moved to point C. Sounds crazy but that's how it works. Further discussion here:

http://answers.unity3d.com/questions/27138/oncollisionenter-event-seems-to-be-called-late.html

To make matters worse, when you use data from the Collision object passed in, that data is also from one FixedUpdate() ago. Collision data and transform data will, at the same time inside any OnCollision*() function, point to an object in 2 different places.

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
avatar image
0

Answer by jj_unity328 · Aug 17, 2018 at 07:34 AM

In the physic loop, FixedUpdate is first called before OnTriggerXXX and OnCollisionXXX.

But you'll also notice that yield return new WaitForFixedUpdate happens right after them, so if required, you might also move your code there.

Source: https://docs.unity3d.com/Manual/ExecutionOrder.html

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

13 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

Related Questions

How to check execution order from script? 0 Answers

Manage the script execution order, how? 1 Answer

Physic Material Friction Combine Mode lack of documentation? 2 Answers

Cancel a collision 2 Answers

,Fast Moving Gameobject Collision and Trigger Check Failed 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