- Home /
Get an object to float to the top of water.
Okay, I need for an object that starts out rolling along the bottom of water due to being heavy and then upon loosing mass, float to the top. I've watched videos and read blogs on buoyancy but all of them are how to employ it on object that always float on top of water. I need for the object to switch back and forth between floating and sinking. Any help anyone could give me would be greatly appreciated.
Answer by GrandWarlock · Sep 05, 2020 at 08:54 PM
@Fkat First of all, buoyancy and weight are completely unrelated. What is the rate at which it loses/gains volume? Let's call rate r. You can have different rates for gaining and losing, Im just doing a universal one for now. Now lets say you have variable V for volume of the object. We don't need to account for density of the fluid because water's density is 1. Assuming normal gravitation acceleration(9.8 m/s^2). So, in your update method { runs every one second, you can look up how to do that if you dont know //There are better ways to do this part but this is just the way I have at the top of my head if(the game object is in water){ V+=r; (Your Game Object).GetComponent().AddForce(0, 9.8V, 0); }else{ V-=r; } }
This may be wrong, I'm not completely sure, but based on what I know about physics this is what I think.
@GrandWarlock Thank you! I should have known that it was the upward force that needed to increase and that multiplied by the fixed delta time! It especially the absence of the latter that was causing problems. Now I need only stabilize the object when it reaches the surface of the water. Again, thank you!
Answer by Eno-Khaon · Sep 05, 2020 at 10:30 PM
By its simplest description, buoyancy is a difference of mass between a (typically) liquid and a (typically) solid object, relative to the same space (density).
Based on that, if you know the volume of the portion of the object submerged in the water (100% when completely underwater, 0% when not touching water) and, by extension, its density, the body of water will push the object upward against gravity to balance out the displacement of that water. If the object is less dense than the water (at its current depth, as applicable), it will be successfully pushed upwards against gravity. If the object is more dense than the water, its descent will still be slowed as it displaces the water, but it will continue to sink until it reaches a point where it is no longer more dense than the surrounding liquid (and, since that won't necessarily apply, it would instead be stopped by something solid, like a floor).
In short, this means that by decreasing and increasing the object's mass (and, by extension, its density), the object will rise and fall in the water respectively. If your formula is set up right, then the work is already done for you.
To note, ships are a focal point of buoyancy, so it's not strange to see them emphasized in that regard. Either way, the same logic and formulas generally apply to anything floating (or sinking) in anything else.
@$$anonymous$$o-Khaon Thank you for your answer. It basically confirmed what GrandWarlock was telling me. If it would allow me I'd mark this as an accepted answer as well.
Your answer
Follow this Question
Related Questions
Damping forces to stop boat oscillation 0 Answers
How to Script Buoyancy with a Water Shader 2 Answers
2D 360 degress platformer example needed 0 Answers
Water Ripples 1 Answer
How can I calculate how much of an object has entered a trigger collider? 0 Answers