Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by AlphaNerd_DE · Mar 23, 2015 at 07:38 AM · c#physicsgravityfloating

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.

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

3 Replies

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

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.

Comment
Add comment · Show 3 · 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 AlphaNerd_DE · Mar 23, 2015 at 04:06 PM 0
Share

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.

avatar image CHPedersen · Mar 23, 2015 at 06:50 PM 0
Share

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. :-)

avatar image AlphaNerd_DE · Mar 23, 2015 at 07:15 PM 0
Share

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.

avatar image
1

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!

Comment
Add comment · Show 2 · 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 Xeong-Hu · Mar 23, 2015 at 08:59 AM 0
Share

OIh Yeah! In case you're wondering. Just select the floating island and Add Component. Type in "Spr" and you should see it.

avatar image AlphaNerd_DE · Mar 23, 2015 at 04:23 PM 0
Share

Now, I don't use 2D, but this is still exactly what I want. It works marvellous.

avatar image
0

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.

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

6 People are following this question.

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

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

Issue using wheelColliders around sphere 1 Answer

(C#) Gravity Field generated in a collider 0 Answers


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