- Home /
How to make objects float
Hello, I want to implement small floating islands for my sidescroller, that one can jump on. I started with counteracting gravity with
void FixedUpdate()
{
rb.AddForce(-Physics.gravity);
}
And the island floated, but when I jump on it, it will of course just lose height forever, because I only counteract the gravity that acts on the mass of the island, not island+player.
So I somehow need a force that always acts in the direction of the Islands origin. I tried adding a force that depends on the distance of the islands position and its origin. That kind of works, but the island will never again stop moving up and down again.
What can I do to achieve my goal?
Edit: I forgot to mention that I want my island to float in air, not in water, so I have to work my way around physics, as that shouldn't be possible in the real world.
Answer by CHPedersen · Mar 23, 2015 at 08:17 AM
What you need is to implement bouyancy. The force to apply to the object depends on the amount of water displaced by the object at any one time.
If your object has a complex shape, it will be difficult to exactly determine its volume. Therefore, it's probably better to try to estimate its density and its mass. For example, for a person, it's relatively easy to arrive at a reasonable volume because we can assume that a person's density roughly matches that of water, and we're quite good at estimating approximate mass of a human. Given those two numbers, it's trivial to arrive at the volume.
The tricky part is to come up with a way to handle the situation where the object is only partially submerged, because obviously, a partially submerged object only displaces water equal to some fraction of its volume. So there is an equilibrium there, where the mass of a volume of water equal to some fraction of the object's volume exactly equals the mass of the submerged portion of the object. And that's the sweet spot where the object can come to rest floating.
See the linked Wikipedia article for more information.
I probably should've mentioned, that I want my islands to float in air and not in water. Because now, this doesn't apply anymore. It should be physically impossible for an object to float and still do that, if something is on it. It would have to become less dense, or in other words, become lighter. But I don't know how to do that, because it's bullshit physics. Other than that, it's a pretty good answer, so thank you anyway.
Actually, the physics in air is exactly the same. I really recommend you read that article. :-) There's a whole section on the bouyancy of air. For something to be floating in air is not at all impossible, that's what balloons do. You have to give your islands some pretty funky densities for sure, but that's why video games are awesome. :-)
Well, I know that it works the same for gases, but not in this case. An object will float if its density is exactly the same as the density of the gas it wants to float in. It will go up if its density is less, and down if its more dense. Now let's assume that the island is exactly as dense as the surrounding air. The island will float wherever I put it. But if someone jumps onto that island (who is obviuosly more dense than air), than the density has changed to something higher than air and the island will inevitably move down, until the player leaves the island. And it won't move up after that, because it is again exactly as dense as the surrounding air. But SpringJoints work just fine.
Answer by Xeong-Hu · Mar 23, 2015 at 08:57 AM
Hey dude, What's up!
CHPedersen is right. Bouyancy is the answer. And unity has the perfect tool for that.
Check this out.
http://docs.unity3d.com/Manual/class-SpringJoint2D.html
SpringJoint2D. I think that's what you need. A helper recommended this to me when I was trying to do an effect for a Water Spout when a GameObject is weighing on it.
Worked fine for me.
Hope it helps man!
OIh Yeah! In case you're wondering. Just select the floating island and Add Component. Type in "Spr" and you should see it.
Now, I don't use 2D, but this is still exactly what I want. It works marvellous.
Answer by britsplease · Mar 23, 2015 at 09:18 AM
Your islands should not have any Rigidbodies, they need only Colliders. This way the islands won't move and the player can still jump on them.
Your answer
Follow this Question
Related Questions
Can't get gravity to work on Character Controller 2 Answers
Multiple Cars not working 1 Answer
Gravity and rotation control 0 Answers