Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
5
Question by Corbeau · Jun 17, 2011 at 11:41 PM · collisioncollidercolliderscollision detectionrigidbodies

Inconsistent Collision Detection

Greetings,

I've encountered a simple problem with collision detection. I have a target (a spaceship in this case) and a bullet. Both are rigidbodies and both have primitive colliders. The bullet is given an initial push, accelerating it to a moderately high velocity towards the target. Sometimes the bullet hits the target and physics appropriately take place. Sometimes there is no collision at all and the bullet continues on it's merry way.

The real problem is that this happens even if both objects are set to be any combination of Continuous or Continuous Dynamic collision detection. The problem is worst under Discrete collision detection, but the rate of failure is still completely unacceptable even if both bullet and target are set to use Continuous Dynamic collision detection.

I've tried adding multiple layers of primitive colliders to the target, which helps "catch" the bullets, but that still looks terrible when bullets often impact inside the model rather than on/near the surface (or even worse, impact the opposite side as they exit). I've tried using mesh colliders, with even worse results (and far worse performance). The velocity involved really isn't that high compared to the size of the target, so this is a substantial obstacle from a design standpoint.

The question is: what else can I do to improve the consistency of my collision detection?

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

3 Replies

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

Answer by kromenak · Jun 18, 2011 at 01:08 AM

Also check that your Fixed Timestep (under Project Settings > Time) is set to a pretty small value. We were using 0.03 for awhile, but ran into a lot of physics issues. Setting this to like 0.01 made physics behave better for the most part, at the cost of some performance.

Comment
Add comment · Show 8 · 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 Joshua · Jun 18, 2011 at 01:09 AM 0
Share

That's it! I was thinking of what I did when I had this problem a while back, and this was what solved it in the end. You don't want to use this for mobile devices though ;)

avatar image kromenak · Jun 18, 2011 at 01:21 AM 0
Share

Haha, yeah, about that. While we are aware that using a 0.01 timestep is not too great an idea on mobile devices, we also feel like our game is really buggy without that precision.

Our game is running pretty good right now on mobile devices with this setting. A further optimization you can make is to set the $$anonymous$$aximum Allowed Timestep to something smaller, like 0.05. This will only allow physics updates to happen so many times in a single frame. This allowed us to tell the physics to be precise, but to not take up too much time.

avatar image Corbeau · Jun 18, 2011 at 01:49 AM 0
Share

Thank you very much, $$anonymous$$romenak; reducing the fixed timestep solved the problem immediately. Have to readjust a bunch of values now, since almost everything was being done with physics forces via FixedUpdate, but it works even after readjusting the numbers for a similar feel.

avatar image Joshua · Jun 18, 2011 at 01:54 AM 0
Share

Just a heads up in that case, $$anonymous$$romenak, if you get a sudden performance hitch every few $$anonymous$$utes that draws on for several seconds it's because the garbage collector and FixedUpdate don't play well together. An explanation can be found here half-way the page: http://forum.unity3d.com/threads/18213-Zombieville-USA-performance-hitches/page3 , basically the garbage collector slows things down for a bit, and FixedUpdate slows things down a LOT trying to catch up on what it missed during the garbage collection.

So be warned ^.^

avatar image kromenak · Jun 20, 2011 at 05:03 PM 0
Share

Hey, good to know - thanks for the link! $$anonymous$$y question then: does setting the $$anonymous$$aximum Allowed Timestep alleviate this issue?

For example, if the garbage collector runs and the physics decides it needs to now run 30 iterations or something to catch up, this could do$$anonymous$$o into a big performance issue. But, if you set the Fixed Timestep to 0.01 and $$anonymous$$aximum Timestep to 0.05, doesn't that mean that your physics updates will do a maximum of 5 iterations to catch up?

So far, the result of limiting the number of physics iterations in this way has been that the physics sometimes seem to "slow down" a bit if the phone is chugging. While still undesirable, it is better than collision errors and interpenetration.

Show more comments
avatar image
1

Answer by Mikla · Feb 24, 2014 at 04:13 PM

I approached this in a mixed way since I needed bullet drop. In the script attached to the bullet, I checked at start and in every frame what the distance was to the target using raycast. If the distance was less than the bullet velocity deltaTime 2 (2 is just to make sure, but can be varied), then I hit the raycast collider gameobject. Not perfect, but better than just raycast or monkeying with Physic settings. Code:

 using UnityEngine;
 
 public class Bullet : MonoBehaviour
 {
     public float BulletLife = 5;
     private float _currentLife = 0;
     private RaycastHit _hitInfo;
     private const float CHECK_MULTIPLIER = 2f;
     // Use this for initialization
     private void Start()
     {
         if (IsCloseToTarget()) RegisterHit();
     }
 
     private void FixedUpdate()
     {
         _currentLife += Time.deltaTime;
         if (_currentLife > BulletLife) Destroy(this.gameObject);
 
         if (IsCloseToTarget()) RegisterHit();
     }
 
     private bool IsCloseToTarget()
     {
         Physics.Raycast(transform.position, this.gameObject.rigidbody.velocity.normalized, out _hitInfo);
         if (_hitInfo.collider != null && _hitInfo.collider.gameObject != null)
         {
             //Debug.Log("Bullet Distance:" + _hitInfo.collider.gameObject.name + "|" + _hitInfo.distance.ToString() + "|" +
             //          gameObject.rigidbody.velocity.ToString() + "|" + Time.deltaTime.ToString());
             var distChk = gameObject.rigidbody.velocity.magnitude * Time.deltaTime * CHECK_MULTIPLIER;
             return _hitInfo.distance < distChk;
         }
         return false;
     }
 
     private void RegisterHit()
     {
         Debug.Log("Bullet Hit " + _hitInfo.collider.gameObject.name);
         Destroy(this.gameObject);
     }
 }
 
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 Joshua · Jun 18, 2011 at 12:15 AM

The problem you're having is that movement in games isn't continuous. You're teleporting the bullet forward frame by frame, making it look like it's moving. If it's moving to fast though, it might go past an object without collision taking place.

There are several ways to prevent this. the easiest way is using this script, DontGoThroughThings.js, which basically checks where am I now, where will I be next frame, and will I have skipped over something then?

Comment
Add comment · Show 2 · 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 Owen-Reynolds · Jun 18, 2011 at 12:34 AM 0
Share

As the OP wrote, that's what Discreet Detection does -- check at a few spots (the spots it jumps to each frame.) Speed is meters/sec, so if the bullet moves at 50m/s, it goes 1m/physicsFrame, so should never miss a standing still target 2 meters wide, with normal (Discret) collision. The Continuous setting is supposed to do a a $$anonymous$$i-raycast each frame. It's exactly for this sort of problem, and the OP seems to be using it exactly the correct way, so odd it doesn't work.

avatar image Joshua · Jun 18, 2011 at 12:54 AM 0
Share

In my experience the 'continuous' setting on collision detection is a lot less reliable then doing the raycasting yourself.

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

10 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

Related Questions

Strange issue with collider 1 Answer

How to reliably break contact/collision? 0 Answers

Detect if a non-trigger collider is inside another collider 1 Answer

How to detect collision between 2 objects while checking are they the ones that need to collide? 1 Answer

How to get OnTriggerEnter effect without using isTriggered on Collider? 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