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 Vinternatt · May 20, 2016 at 02:03 PM · collisioncolliderscollision detectiononcollisionenter

OnCollisionEnter being called without the colliders actually colliding

Hi,

I'm having an issue with OnCollisionEnter being called without the colliders actually colliding, i.e. it's like a bigger collider than defined for the objects. The physics however work when they should - when my club visually hit my ball it starts moving. It's just that there's like an area around the ball or club where OnCollisionEnter is called independent if the club actually collide with the ball or not.

In my code below I make the head of the club black to see when it OnColliderEnter is called.

 void OnCollisionEnter(Collision coll){
     if (coll.gameObject.CompareTag ("Ball")) {

         Material headMaterial = GameObject.Find ("Head").GetComponent<Renderer> ().material;
         headMaterial.color = Color.black;

         }
 }

There was another question about this with no answer which illustrates the situation: http://answers.unity3d.com/questions/1067291/oncollisionenter-is-called-before-the-actual-colli.html

Any ideas?

Comment
Add comment · Show 12
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 Baste · May 20, 2016 at 02:46 PM 1
Share

Are you sure that there's only one object with the tag "Ball" on it?

avatar image Vinternatt Baste · May 20, 2016 at 02:57 PM 0
Share

Yes, there's only one object

avatar image FortisVenaliter · May 20, 2016 at 02:54 PM 1
Share

Can you post a screenshot or video of the problem?

avatar image Vinternatt FortisVenaliter · May 20, 2016 at 02:58 PM 0
Share

It's in VR so I need som help to do that so can't do that today. Howver the link: http://answers.unity3d.com/questions/1067291/oncollisionenter-is-called-before-the-actual-colli.html illustrates the same issue. You can see that before the collision the OnCollisionEnter is called.

avatar image Baste · May 20, 2016 at 03:40 PM 2
Share

There's an answer in the post you linked that's probably correct.

Collisions happens in the fixed timestep. Unity has probably calculated that the collision's going to happen, and has called the method.

What's your interpolation settings? If you have "none", your transform is going to jump a step forward on every physics frame, ins$$anonymous$$d of moving smoothly from one to the next. Try to set it to Extrapolate, and see if that helps. Also make sure that your Collision Detection is set to continuous dynamic.

avatar image Vinternatt Baste · May 21, 2016 at 11:32 AM 0
Share

Apparently my phone didn't send the reply.

I'm using continuous dynamic and tried extrapolate but no difference.

I'm a noob so I'm not sure but I don't think it has to do with the fixed time step. As with the example in the link I can moce the club in and out of the detection area very very slowly so it won't do any kind of guessing that a collision will happen.

avatar image Baste Vinternatt · May 23, 2016 at 01:49 PM 1
Share

Hmm, is any of your objects scaled strangely? With negative scale or something?

Also, what kind of colliders are you using? If you're using mesh colliders, I'd try setting them to convex, or replace them with (compound) primitive colliders.

Show more comments
avatar image Nomenokes · May 24, 2016 at 07:40 PM 1
Share

Unsure if this is related, but I heard at some point that colliders are slightly larger than their actual scale. I think it's an editable Physics thing.

avatar image Vinternatt Nomenokes · May 28, 2016 at 12:47 PM 0
Share

I couldn't find anything like that while googling =/ but to me it would make sense since it is very small objects.

1 Reply

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

Answer by ninja_gear · May 25, 2016 at 02:54 PM

The answer given at:

http://answers.unity3d.com/questions/1067291/oncollisionenter-is-called-before-the-actual-colli.html

explains the situation exactly, perfectly, and without error.

Your game code cycles and renders (draws), and the two are not in sync. In fact, your game can cycle several times before a frame is drawn. The Update() callback is called once before each frame is going to be drawn. However, FixedUpdate() is called when the code reaches the end of its cycle and starts up anew. OnTriggerEnter() is called during Update(). OnCollisionEnter() is called during FixedUpdate(). This is why your game 'looks' like nothing is touching when OnCollisionEnter() is called. Because the game collided them between the drawing of frames.

The biggest issue isn't on your end. Unity should rename the OnCollisionEnter() callback because everyone thinks that where you put collider logic. You shouldn't. FixedUpdate(), Rigid-Bodies, and OnCollisionEnter() are very intense (because they are processed several times between drawing frames to increase accuracy) and should be used sparingly. Except in your case where realistic objects need to behave as if they CAN NEVER OVERLAP IN THE GAME WORLD FOR ANY REASON because they are supposed to be solid, not-penetrable objects.

If you want the ball to not move until the game draws then use OnTriggerEnter() at small scales, and at larger scales, use OnColliderEnter().


TL;DR: The game is predicting they are going to touch before the screen is drawn again, and makes sure they start 'colliding' before it draws another frame, so they are not drawn overlapping.

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 Vinternatt · May 28, 2016 at 12:53 PM 0
Share

Thank you for your reply. I believe your explanation is correct and sorry if I'm stupid but I have problems with it since I can e.g. move the club in a direction that it will not hit the ball but go past it. Still it gives an alert when it comes within a certain distance that it has become a collision.

I however understand your explaination enough to move away from OnCollisionEnter() and ins$$anonymous$$d have made a child to the ball that is atrigger ins$$anonymous$$d (if I make the ball a trigger it will to work well with the physics I assume) and play around to make that work ins$$anonymous$$d.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision detection when no collision happens,OnCollision is called without a collision 1 Answer

How to detect if target is inside an "arc" area? 0 Answers

my thing keeps going through walls, but ridged bodies can touch the collider 0 Answers

How to make objects collide once 1 Answer

How to detect correct collision from multiple game objects? Separate collisions 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