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 Long2904 · Aug 03, 2020 at 05:54 AM · physics2dcollision detectionfastmovinggameobject

How to accurately detect collision?

So I had a throwable fire grenade in my game that will bounce off the wall and explode when touching the ground. For my grenade, I set the rigidbody's collision detection to continuous and have a boxcast under it to check if it touches the ground. But sometimes when it moved so fast it would bounce back instantly in the next frame so my boxcast wouldn't work.

First frame: (the red box below the grenade is the boxcast, the grenade is falling) alt text Next frame: (the grenade bounce back off without actually touch the ground, so the boxcast isn't triggered)alt text

bug.png (28.2 kB)
nextframe.png (29.9 kB)
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 mzaidan1996 · Aug 03, 2020 at 07:03 AM 0
Share

A simple fix that I know of is that you can set the objects like the ground and the wall with continuous collision detection but its pretty costly, I'll try finding a better fix right now, ill reply if I find something.

avatar image Long2904 mzaidan1996 · Aug 03, 2020 at 09:55 AM 0
Share

@mzaidan1996 I don't think that is the solution. Sorry for not describe the problem clearly (I've just updated the question). The problem is in the first frame the object is falling very fast and about to touches the ground. In the next frame, the physics engine will calculate and decide that the object should already hit the ground and bounce it back off. I think the collision at this time still gets detect and some function like OnTriggerEnter, OnCollisionEnter... still get called but the problem is the boxcast as you see above hasn't hit the ground so it will not trigger.

avatar image cwalshwarder Long2904 · Aug 04, 2020 at 02:37 AM 0
Share

$$anonymous$$ake sure you do the box cast in FixedUpdate() rather than Update(), and use Physics2D.BoxCast rather than Physics.BoxCast if it's 2d.

You could try reducing the fixed timestep in project settings. It might be slow in the editor if you set it to like 1/180 but physics is a lot faster in builds for me.

Show more comments

2 Replies

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

Answer by Long2904 · Aug 06, 2020 at 06:17 AM

Decrease the fixed time step to something like 0.01 or 0.005 and change the yield return null to yield return WaitForFixedUpdate().

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 tuinal · Aug 04, 2020 at 03:38 AM

Simple option in edit>project settings>physics reduce 'fixed time step'. This runs the physics simulation more quickly at the cost of performance.

I think the best solution is to ditch the boxcast and use OnCollisionEnter2D instead. In OnCollisionEnter2D, if you want it to only explode when it hits the floor, do if(Vector2.Angle(Vector2.Up, collision.contacts[0].normal)<10f); or similar.

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 Long2904 · Aug 04, 2020 at 12:43 PM 0
Share

@tuinal Firstly, I thought the fixed time step is for the fixed update and my boxcast got called and updated in a coroutine that yield return null so I don't think this will fix anything. Secondly, I create and access the grenade gameobject through code (from another gameobject) in runtime so access to its update or OnCollsionEnter2D function is not possible. Do you have any other ideas? Edit: Also the timestep setting is in Time, not Physics, and I'm currently using Physics2D.

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

140 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

Related Questions

Objects pass through mesh collider without collision (Help me pls) 0 Answers

After Collision Matrix change in runtime, effects not immediatly visible 1 Answer

Detect Contact between Kinematic and Non-kinematic bodies 1 Answer

Small overlap in ColliderDistance2D after moving collider to RaycastHit2D.centroid 0 Answers

Code-moving objects dont affect Physics2D objects 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