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
2
Question by GD-DavidStrife7 · Nov 04, 2015 at 08:05 AM · collisioncolliderscollision detectionframes per secondframeskipping

Bullet/Projectile Collision Question

alt text

In the image above, I've illustrated (very crudely in Paint) a problem I anticipate within Unity3D (or any game engine to be honest), regarding projectile/bullet collision.

If I have a bullet prefab game object instantiated at the tip of a gun, and it travels at 2 metres per second, and an enemy is standing 3 metres away, what would happen if my framespersecond was at 1fps?

If my game was running so slowly, would that collision happen? In my mind, the game is running at 1 frame per second. So in Frame 1 the bullet is just in front of the gun. Frame 2 the bullet is at 2metres, at frame 3 the bullet is at 4 metres, etc. So if the game was running at 1fps, and the bullet moving at 2 metres per second, then any enemies standing in odd numbered distance intervals (3,5,7,9 etc) would not trigger a collision?

Or does Unity's FixedUpdate method deal with physics in such a way, that it detects the collision, regardless of how many visual frames it renders per second? I think I remember reading somewhere, the fixedupdate happens regardless of your FPS, it occurs at fixed intervals in the game and is not affected by your FPS output?

I'm trying to understand how Unity deals with collisions, and how accurate/reliable this is for someone on a PC with low FPS. I'm sure no one would even play a game at 1fps, but I'm wondering what would happen if maybe they had an FPS spike too low for a brief moment, would they be cheated/robbed of a potential kill because of this 'bug' in my game?

bulletexample.png (20.4 kB)
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

2 Replies

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

Answer by rutter · Nov 04, 2015 at 08:19 AM

If my game was running so slowly, would that collision happen?

Nope! Not by default, anyway.

You've discovered the difference between discrete and continuous collision.

In discrete collision, the position of each object is checked once per frame, and it's entirely possible for fast-moving objects to go completely through colliders without ever "touching" them. This is the default behavior for most colliders in most engines, because it's very performant.

Continuous collision is good and bad. It's good because it's more accurate and will catch things like a fast-moving bullet that hits a wall. It's bad because it's more work for the computer, and that can bog down performance if you're not careful.

From the Unity manual, you can apply several collision detection models to a rigidbody:

  • Discrete: Use Discreet collision detection against all other colliders in the scene. Other colliders will use Discreet collision detection when testing for collision against it. Used for normal collisions (This is the default value).

  • Continuous: Use Discrete collision detection against dynamic colliders (with a rigidbody) and continuous collision detection against static MeshColliders (without a rigidbody). Rigidbodies set to Continuous Dynamic will use continuous collision detection when testing for collision against this rigidbody. Other rigidbodies will use Discreet Collision detection. Used for objects which the Continuous Dynamic detection needs to collide with. (This has a big impact on physics performance, leave it set to Discrete, if you don’t have issues with collisions of fast objects)

  • Continuous Dynamic: Use continuous collision detection against objects set to Continuous and Continuous Dynamic Collision. It will also use continuous collision detection against static MeshColliders (without a rigidbody). For all other colliders it uses discreet collision detection. Used for fast moving objects.

So, for example, you might set bullets to continuous dynamic and walls to continuous.

Side note: when it comes to bullets, lasers, and similarly instantaneous movement, it is sometimes simpler to simulate all of this behavior by raycasting.

Comment
Add comment · Show 6 · 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 Dray · Nov 04, 2015 at 09:33 AM 0
Share

Yup. Raycasting is the best conclusion for (normal) bullets. I wouldn't worry about performance here since your bullets calculation can be done in one frame like this and raycasting in Unity still is very performant. I think raycasting once per bullet is much cheaper than calculating every bullet in an update loop.

Of course it doesn't work for slow projectiles like rockets though. For things like this, using Fixed Update is the best direction.

avatar image Cherno Dray · Nov 04, 2015 at 09:47 AM 0
Share

It's also possible to combine the two methods: Have the bullet be a real GameObject with rigidbody, and move it forward continuously. At the same time, cast a short ray from the bullet's last position to it's current one; this ray will catch any collisions that happened between frames.

avatar image fct 509 Cherno · Nov 04, 2015 at 11:22 AM 0
Share

Wouldn't increasing the tail end of the collider mesh work too.

Show more comments
avatar image meat5000 ♦ · Nov 04, 2015 at 11:31 AM 0
Share

entirely possible for fast-moving objects to go completely through colliders without ever "touching" them

This even happens in Continuous Dynamic. I did a bunch of testing previously and discovered that only a $$anonymous$$esh Collider can detect collisions over ~10000units/sec (roughly). Of course that is pretty damn quick movement, so shouldn't be a problem.

But in instances where collisions are intermittent, watch the speed and test against a mesh collider to narrow down the problem.

The key phrase in the docs is

For all other colliders it uses discreet collision detection (Under Continuous Dynamic) which in my view defeats its purpose.

avatar image GD-DavidStrife7 · Nov 06, 2015 at 07:50 PM 0
Share

Thanks everyone for the answers! I have a good understanding of what happens now, and I have the keywords to go do more research.

Thanks :)

avatar image
0

Answer by Sdvsdv · Mar 16, 2017 at 08:24 PM

Hello. I find so far this is bit more accurate than colliders for me atm. its bit late but it might help other ppl like me.

 private RaycastHit myHit;
 private Vector3[] oldFramesVector = new Vector3[5];
 float testVelocity = 45f;

 void Update() {
     // simple motor
     transform.Translate(Vector3.forward * testVelocity * Time.deltaTime);
     //hit detection for fast moving objects
     if (oldFramesVector[4] != Vector3.zero) {
         for (int i = 0; i < oldFramesVector.Length; i++) {
             if (Physics.Linecast(transform.position,
                 oldFramesVector[i], out myHit)) {

                 if (myHit.transform.gameObject.name != 
                     "object who created this one") {
                     print("did hit " + myHit.transform.name +
                         " coll check succed on : " + i + " frame ");
                     break;
                 }
             }
         }
     }

     if (oldFramesVector[0] != transform.position) {
         for (int i = oldFramesVector.Length; i > 0 - 1; i--) {
             if (i + 1 < oldFramesVector.Length) { 
                 oldFramesVector[i + 1] = oldFramesVector[i]; 
             }
             if (i == 0) {
                  oldFramesVector[i] = transform.position;
             }
         }
     }
 }
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

9 People are following this question.

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

Related Questions

get only one colliding body 3 Answers

How to get OnTriggerEnter effect without using isTriggered on Collider? 1 Answer

(Virtual reality) Collision fighting when tunneling - Any ideas? (gif included) 0 Answers

Gameobject not detecting collison from other Box Collider 2D [SOLVED?] 2 Answers

Makign an object collide continously 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