Buoyancy with Unity Rigidbodies - Part 1 / Planar Buoyancy

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to this billion bullions hello and welcome to this water physics tutorial so today we're going to see how we can have some rigid body floats on water in the first part of this tutorial we're going to see how we can have objects float on still water so without any waves and then we're going to expand on that code in order to have rigid bodies react to ocean movement so since this is going to be pretty long let's get started straight ahead let's create a plane which is going to be all water remove the collider and let's just scale it a bit then let's add a rigid body let's choose a cube and this is going to be our floating object so in order to have the scene look a bit better i'm going to create a quick ocean shader but since it's not the point of the tutorial i will speed it up and although i will show you the note graph at the end i encourage you to go see another tutorial um like polytus is a great resource and he's a really funny guy so please go watch it i guess now is a good time to say that you don't need to be in any specific render pipeline to follow this tutorial i'm doing it in hdrp but you could also do it in urp or even in the standard render pipeline if you have amplify shader or if you're just very smart and know how to code shader by hand so as you can see it's a very simple shader i'm just lurping between two colors and i'm adding a normal map on top of that which is slowly scrolling um there's also a depth effect that you can see here in action all in all it's very simple and it doesn't look very good but it will be good enough for this tutorial so now i'm going to slowly scroll through my shader so that you can copy it if you want but if you have followed my advice and picked another better shader you can just skip this section and join me in the next one [Music] okay so we're three minutes in and we still haven't done anything that helps with floating objects so let's just create a script and open it the first thing that we're going to do is to say that this script requires a rigid body this way it will automatically add one when you add the script to a game object then we're going to add a few variables a few floats which will define the drag effects of the rigid body both under and over water so both a linear drag and and angular drag so that's for underwater and then we're just going to add an air drag i'm just going to set the air drag to the rigid body default which is 0 and 0.05 and for the underwater i you know i picked three and one but you'll have to play with the values yourself we're going to need a reference to a rigid body and a state ball that will tell us whether we are underwater or above the water and finally we're going to add this floating power which is going to decide how strong the force we applied to the rigid body is so in the start function we're going to just get a reference to our rigid body and then we're going to change the update function to fixed update because this is all physics so first we're going to create a local variable which is going to calculate the difference between the rigid body that we are working with and the height of the water so for that i'm just going to add an another public floats which is going to be called water heights so a simple subtraction okay so i just renamed that because even by my standard that was very unclear then we're going to check if the rigid body is below the water if it is we're going to add a force which is going to be upwards so vector3.up and multiplied by the floating power as well as the difference between the position of the rigid body and the water because the lower the rigid body is we want the stronger the force right like if you put a floating balloon very low underwater and you release it it will jump above the water and we're using a force mode which is force then we're going to check if our underwater boolean was false so previously the rigid body was not under the water and if it was false we're going to switch it to true finally if the difference was above zero so the rigid body was above the water and we add a boolean state set to underwater then we're going to switch that to false finally let's create a switch state function that will allow us to change between our underwater drag and our air drag if we're going to the underwater state then we are going to set the rigidbody's drag variables to the one that we have defined previously and if we're going to above the water then we're going to do the opposite finally we're just going to call the function in fixed updates but only if we have switched from one state to the other this frame so that we don't have to call it every single fixed update okay well that's done so let's compile add the script to the rigidbody let's you know change the mass to something like 4 kilograms and we'll see if that's enough hit play okay so here we have an issue which is that the rigid body is too heavy or the floating power is too low so let's just rate this raise it more raise it more okay now we have something that looks like an actual floating object and you can see that if you drop it it just has this sort of spring-like motion and let's duplicate it a bunch and yeah this is it you now know how to make a simple floating object now it works only on flat surfaces because as you remember we're just checking whether the rigid body is below or above a certain height now that's all fine for simple objects but what if we want to have like platforms on which the player can step well that won't work with the system we've just built because we're just checking whether the rigid body is below or under the water but a more complex object will have multiple points that can be either above or below the water so the way this works is we're going to have to change our script a bit we're going to create an array of transforms which are going to be floaters so like a list of little bureaus that we're going to use to add forces to different parts of the rigid body you'll see it's quite easy to adapt the script that we've already made for that so for every one of these floaters which can be one for the simple objects as we've seen but it can be also two three four we're going to check whether each floaters is below or above the water right so let's use a for loop and then instead of checking the rigid body's position we're going to check the floater position then if the floater is below the water we're going to add a force to the rigid body but at the position of the floater [Music] now it gets a bit tricky when we want to check whether the object is under or above the water because some floaters can be below and some floaters can be above at the same time so the rule that i've chosen is that if all the floaters are above the water then the rigid body is above the water but if even one floater is below the water then we're going to apply the water drag which is sort of an arbitrary thing but to do this we are going to add an int which is called floaters underwater and at each fix update we're going to set it to zero and if during the for loop any floater is below the water then we're going to increment that int then we're going to check whether the floaters underwater is equal to zero because if it is it means that no floaters is underwater and then we can switch to the above water state now you'd say we could have used only a bull which is true but this way we can know precisely how many floaters are in the water at any point which can be used you know in further implementations or more complex behaviors so yeah let's edit the settings a bit and then we're going to add some floaters so it's going to be pretty easy so first of all yeah for the simple boy i just going to add the object itself as its own floater and for the more complex platform i'm just going to add a few game objects at each corner of the shape you could add some gizmos to make them visible which is what i did in the final version but for this i think it's just enough to do it like this then add all the floaters to the script and we're good to go let's try it and here you go so it's kind of working but as you can see it's a bit too thin so i'm just going to quickly edit these settings [Music] okay well it's working as you can see it's very floaty and you can edit that very easily either by changing the weights of the rigid body or the floating power of um of the script that we've made and the lower it is the lower the rigid body will find the equilibrium point and what's really awesome about this technique is that then you can very easily add other rigid bodies and it will just react realistically with it as you can see so if your player controller uses rigid bodies it is as simple as that you can just have them walk on platforms and if they're too heavy or if they go too much to the side the platform will just capsize so that's the end of this first part in the next part we'll see how we can adapt this script and a simple shader we've made in order to have the rigid bodies react to the waves of the ocean so stay tuned for that and see you next time
Info
Channel: A Beginner's Dev Vlog
Views: 4,279
Rating: undefined out of 5
Keywords:
Id: iasDPyC0QOg
Channel Id: undefined
Length: 13min 18sec (798 seconds)
Published: Fri Apr 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.