Unity Rigidbody Collision Detection Modes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello coders this is Jared with Renaissance coders if you want to see more videos like this one hit that like button and subscribe to the channel the fancy YouTube algorithms will rank as higher and enable us to keep making great content for you thank you for your time now let's get into the video okay coders last week I created a video called repeating rotations and in that video we made our base platform rotate back and forth over time now one of our Renaissance coders community members has asked for more information on how to make this look better than it does because we have some fairly obvious issues with the way that our script or our level is currently functioning so let's take a look at what we're currently working with I'm just going to press the play button here and as you can see we've got some balls being instantiated we've got some wind pushing the balls off and they're all sort of functioning normally but when I start adding in more balls we'll start to see that some of them like that black one there just fell straight through the platform and that's not really a desirable behavior for something like this we want all of our balls to react to this platform normally so let's go ahead and stop that playback okay so I should point out that I've spent a lot of time experimenting with this to try and get the best effects and while we still don't have a perfectly functioning system we can improve on our current system before we go into this though let's review what we know about collision detection modes for rigid bodies we have three available settings for the collision detection mode the three modes are discrete continuous and continuous dynamic and to find those if we click on an object with a rigid body give to the rigid body you can see collision detection and if we click on the collision detection setting we can see the three modes that are available to us okay so let's learn a little bit more about what each of these settings is meant for and what it basically does so the discrete setting basically means that continuous collision detection is off for this rigid body but we need a little more information than that right well the discrete setting is the default setting for a rigid body so when you add a rigid body to an object it will always be set to discrete and this setting is the fastest computational mode available to us so this one will run much more quickly than the other two but collisions for this Collider will only be checked at the contents time fix Delta time so what does that mean well basically this means that this mode of collision will not work very well for fast-moving objects such as a bullet because a collision could occur between two different frames of fix Delta time and if a collision were to occur between two of those fixed Delta times then the object would pass through the other Collider so now let's move on to the continuous detection mode the continuous detection mode is used when we need our object to collide with static mesh geometry now with this mode setting collisions will be detected for any static mesh geometry in the path of this rigid body even if the collision should occur between two fixed updates steps now the first thing we need to answer is what is a static mesh geometry well a static mesh geometry is any mesh Collider that does not have a rigidbody component attached to it so our platform here is actually a static mesh geometry this setting also prevents rigid bodies with the continuous dynamic setting from passing through this rigid body so let's say we had two objects with rigid bodies on them one set to continuous dynamic and one set to continued continuous then those would still collide with each other even if they're moving at a fast speed so what does all this mean well this setting could be useful for something moving quickly like a bullet and our final setting is the continuous dynamic setting and the continuous dynamic setting means that continuous collision detection is on for colliding with both static and dynamic geometry this setting prevents the rigid body from passing through static mesh geometry and through other rigid bodies which have continuous detection enabled this is definitely the slowest of the modes computationally speaking and should only be used for fast moving objects so you wouldn't want all of your objects to be set to continuous dynamic because if you did then you would definitely notice some drops in framerate and performance now that we know a little more about the collision detection modes that are available to us in unity we can make a more educated decision on what settings we should use for this scenario but that is not the renaissance coders way we want to show you what each method would like so let's try that out first we are going to change our prefabs to use the continuous detection and see how well that functions so we need to go to our prefabs folder here I'm just going to select all of my prefabs and we're going to change the collision detection to continuous okay now that we have that changed let's play back our scene here and see how well it functions well off the bat we can see that none of the balls are passing through we the ball seem to be reacting a little more organically with the platform but what happens when I start adding in a lot of balls well as you you might have noticed we already had one pass through this object so this is still not functioning perfectly when we have a lot of these balls inside of our scene but this is definitely functioning a little bit better so let's go ahead and in that play back and we're actually going to change all of our spheres now to the continuous dynamic and see what happens so let's press play okay so again we can see that the balls are reacting with the platform like we would expect them to we've definitely get some framerate issues as you can see with some of the balls they're there stat or stuttering a little bit and some of the balls are still passing through our platform right there we had two or three pass through so this doesn't really seem to be a huge improvement upon the original but that may be because the framerate issues we're having with the setting and all of these game objects you will notice that when I just have a couple of game objects in the scene that they're acting they are reacting to the platform fairly naturally and they're reacting to each other fairly naturally which is good now I do want to go through one more scenario and that is what what would happen if we were to add a rigidbody to our platform because right now if I click on it you can see that we don't have a rigidbody on this platform we just have a box Collider so let's actually go ahead and add a rigidbody now we're going to need to disable gravity and we're actually going to freeze the position and rotation of this rigid body here so that way it doesn't move on contact and let's see what happens now if I press play you'll notice that I left it the collision detection to discrete and again we've got balls passing through you know it seems to have way more passing through now our frame rate has dropped drastically with this setting this black ball that's closest to us is clearly stuttering on the screen when I drop a lot in several of them are going to pass through and you know so this setting is does not look very well it's not functioning very well so let's stop that and now we're going to go back and let's change this to continuous because if you remember from our earlier review I said that continuous and continuous dynamic settings work pretty well together now I don't know how well it's going to function with this many objects being spawned and being maintained in our scene but let's find out so we can immediately see that we're getting some collisions where some things are passing through here and this is not really functioning all that well you know immediately we can see this isn't functioning very well honestly I think this is worse than our initial setting as you can see right there we had like five pass through immediately and that's just really not what we want so I think that the best setting for this was actually without the rigidbody and with our spheres set to continuous so if we go back now press play we can look at this one more time and we may still have some balls passed through with this setting you know if I try to spawn just a ton of them we may see some try and pass through yeah I think I just saw one there went one that will pass through but this is by far the best setting that we have seen you know when we had two rigid bodies they didn't really work all that well together and I think this may be a an issue with the unity engine you know as far as and the physics that are being calculated and things like that but we have managed to experiment with several settings here now another thing that we could do to dive a little deeper into this is look at the profiler and see what sort of impact these different settings are having so if I go to my window and open up my profiler I'm just going to dock it on the bottom down here now let's see what this setting is doing to our game so it looks like we're running pretty well we're averaging about I'm going to say 100 frames we're definitely devastating our memory here we've got a lot of textures being rendered our physics is the important thing we want to look at here right so we're really really taxing our physics we've got a lot of active dynamic objects taking place and as you can see we're spiking pretty heavily with that ok so what happens though if I in that and we change this back to discrete whoops yes so they're also selected so let's go back to our profiler and let's see if this has any you know bearing or impact on our physics so we do have well nope it just spiked so it looks like we're definitely still going to be spiking as you can see when I started instantiated more it ramped up big-time right here and then it's dropping back off so again I think that the important thing here is you know when you're building a game like this in which you need to have a lot of physics going on and a lot of complex collisions occurring then you may want to experiment with the different settings and see what works best for you you know when we when we were playing with things like this it's a deep subject and there's a lot of things that go into it that honestly or couldn't be answered in a very short tutorial but I think just going through and looking at the different settings we have available to us and trying out different scenarios then we can get a better grasp on what we can do within unity ok guys that's going to do it for this tutorial I hope you learned something new I know I certainly you know learned a little more about the different collision modes in unity by creating this tutorial and I hope I answered the community members questions regarding this if you did learn something new be sure to LIKE and subscribe and as always thanks for watching
Info
Channel: Renaissance Coders
Views: 21,124
Rating: undefined out of 5
Keywords: unity, unity3d, rigidbody, collision detection, physics in unity, game design, Game Development, rigidbodies, unity collisions, unity game development, rigid body, rigidbody collision detection modes, detect 2d colllisions in unity, unity 5.6.1 how to collide with gameobjects c#, unity collision detection, unity collision script c#, tutorial, unity 3d, continuous dynamic detection mode, continuous detection mode, discrete detection mode, which rigidbody detection mode, physics
Id: ieeOsVmnoGs
Channel Id: undefined
Length: 10min 57sec (657 seconds)
Published: Tue Jun 06 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.