# Buoyancy script

**URL:** https://discussions.unity.com/t/buoyancy-script/432786
**Category:** Community Showcases
**Created:** [January 6, 2011, 12:04pm UTC](https://discussions.unity.com/t/buoyancy-script/432786 "2011-01-06T12:04:26Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![alexzzzz](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/alexzzzz/32/333491_2.png) [@alexzzzz](https://discussions.unity.com/u/alexzzzz)
#### Post date: [January 6, 2011, 12:04pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/1 "2011-01-06T12:04:26Z")

</div>

Just Archimedes’ law:

[https://vimeo.com/18468122](https://vimeo.com/18468122)

Requires a collider and rigidbody components.

**P.S.**  
I’ve updated the script:

1. The old one didn’t take into account object rotation and consequently produced wrong results sometimes.
2. The new one draws nice gizmos

**P.P.S.**  
Version 2.1

1. I decided to not calculate density from volume, because volume “calculation” itself is quite inaccurate. It’s easer to specify the correct density manually.
2. Now it works with concave shapes too. Thanks to **Antitheory** for the idea.

**P.P.P.S.**  
License: [**WTFPL version 2**](http://www.wtfpl.net/about/).

[466976–16416–$Buoyancy.cs](https://discussions.unity.com/uploads/short-url/viuz9wjjwksSj8ziDiyOPVEDp5f.cs) (8.43 KB)

---

<div class="post-metadata">

### Author: ![granada\_1](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@granada\_1](https://discussions.unity.com/u/granada_1)
#### Post date: [January 6, 2011, 1:17pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/2 "2011-01-06T13:17:09Z")

</div>

Looks great,i can see me floating away on a raft 😄.

Dave

---

<div class="post-metadata">

### Author: ![cupsster](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/cupsster/32/471573_2.png) [@cupsster](https://discussions.unity.com/u/cupsster)
#### Post date: [January 6, 2011, 2:00pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/3 "2011-01-06T14:00:59Z")

</div>

nice presentation… will give it a try. Thank You for sharing.

---

<div class="post-metadata">

### Author: ![7loop7](https://avatars.discourse-cdn.com/v4/letter/7/d9b06d/32.png) [@7loop7](https://discussions.unity.com/u/7loop7)
#### Post date: [January 6, 2011, 2:31pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/4 "2011-01-06T14:31:43Z")

</div>

looks very cool, will have to give it a try later.  
thanks you =)

---

<div class="post-metadata">

### Author: ![3dDude](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/3ddude/32/354257_2.png) [@3dDude](https://discussions.unity.com/u/3dDude)
#### Post date: [January 6, 2011, 2:52pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/5 "2011-01-06T14:52:44Z")

</div>

man that water shader rocks!!! maybe a package for that to??? 😄 thanks!

---

<div class="post-metadata">

### Author: ![bigkahuna](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bigkahuna/32/291490_2.png) [@bigkahuna](https://discussions.unity.com/u/bigkahuna)
#### Post date: [January 6, 2011, 2:53pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/6 "2011-01-06T14:53:29Z")

</div>

Nicely done. I noted the “links” to the ocean shader that were commented out. Does it work with the community ocean shader?

---

<div class="post-metadata">

### Author: ![dart](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dart/32/243762_2.png) [@dart](https://discussions.unity.com/u/dart)
#### Post date: [January 6, 2011, 3:26pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/7 "2011-01-06T15:26:10Z")

</div>

Nice work. Thanks for sharing

---

<div class="post-metadata">

### Author: ![anon\_19585093](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_19585093](https://discussions.unity.com/u/anon_19585093)
#### Post date: [January 6, 2011, 3:35pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/8 "2011-01-06T15:35:15Z")

</div>

The Ocean script/shader by Nikko has a buoyancy script to that works similar to this, although that one is only two-dimensional (ie: it doesn’t chop the object along the y-axis).

I just made my own little version of the same script in order to make it work semi-realistically with irregular objects. The problem with the approach taken here is that it simply gets the bounds (a rectangular prism) of the collider and chops that up into cubes… this means that a cone, for example, would have just as much buoyancy at it’s tip as at it’s base (obviously not true).

To solve this problem I use basically the same approach as this script, but each point is checked to see if it’s truly “inside” the collider. I accomplished this by firing 6 rays originating outside the mesh toward the point. each ray corresponds to a direction along the x,y,z axes. If any of the rays don’t hit the collider it mean’s the point does not reside inside the mesh. There can be some false-positives, but only for very concave shapes.

The script is designed to work with the Ocean script but it could be easily modified to work with any water plane. If there is interest I’ll post the script here.

---

<div class="post-metadata">

### Author: ![bigkahuna](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bigkahuna/32/291490_2.png) [@bigkahuna](https://discussions.unity.com/u/bigkahuna)
#### Post date: [January 6, 2011, 3:38pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/9 "2011-01-06T15:38:28Z")

</div>

Thanks Antitheory. I’d love to see your enhanced script as well.

---

<div class="post-metadata">

### Author: ![system](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/system/32/97283_2.png) [@system](https://discussions.unity.com/u/system)
#### Post date: [January 6, 2011, 3:40pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/10 "2011-01-06T15:40:12Z")

</div>

Looks really awesome, thanks for sharing!

---

<div class="post-metadata">

### Author: ![alexzzzz](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/alexzzzz/32/333491_2.png) [@alexzzzz](https://discussions.unity.com/u/alexzzzz)
#### Post date: [January 6, 2011, 3:43pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/11 "2011-01-06T15:43:59Z")

</div>

**3dDude,**  
It’s a community ocean shader:

[http://www.unifycommunity.com/wiki/index.php?title=Ocean\_Shader\_Project](http://www.unifycommunity.com/wiki/index.php?title=Ocean_Shader_Project)  
[http://forum.unity3d.com/threads/16540-Wanted-Ocean-shader](http://forum.unity3d.com/threads/16540-Wanted-Ocean-shader)

> [@bigkahuna](#):
>
> Does it work with the community ocean shader?

Yes, it does.

Actually, the ocean package comes with its own buoyancy script. I looked at it, got the idea, and wrote this one from scratch. Then, in order to avoid the defective javascript\<\>c# interoperability, I translated the whole ocean script to c# for my personal use.

Here it is:

[467101–16380–$Ocean.cs](https://discussions.unity.com/uploads/short-url/k5cuv4vaQPfUCXXy9tMvKbDqDAK.cs) (35.1 KB)

---

<div class="post-metadata">

### Author: ![alexzzzz](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/alexzzzz/32/333491_2.png) [@alexzzzz](https://discussions.unity.com/u/alexzzzz)
#### Post date: [January 6, 2011, 3:50pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/12 "2011-01-06T15:50:09Z")

</div>

> [@anon\_19585093](#):
>
> To solve this problem I use basically the same approach as this script, but each point is checked to see if it’s truly “inside” the collider. I accomplished this by firing 6 rays originating outside the mesh toward the point. each ray corresponds to a direction along the x,y,z axes. If any of the rays don’t hit the collider it mean’s the point does not reside inside the mesh. There can be some false-positives, but only for very concave shapes.

Nice idea!

---

<div class="post-metadata">

### Author: ![anon\_19585093](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_19585093](https://discussions.unity.com/u/anon_19585093)
#### Post date: [January 6, 2011, 5:31pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/13 "2011-01-06T17:31:14Z")

</div>

**EDIT**

I removed my version of the script because it had some errors in it. I have fixed them now and will clean-up the code a bit so you guys can check it out. I tested it out with composite objects too. I made a pretty sweet catamaran… I will post it all soon in a new thread.

---

<div class="post-metadata">

### Author: ![alexzzzz](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/alexzzzz/32/333491_2.png) [@alexzzzz](https://discussions.unity.com/u/alexzzzz)
#### Post date: [January 6, 2011, 6:13pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/14 "2011-01-06T18:13:30Z")

</div>

I have updated my buoyancy script, because the old one produces wrong results if an object is rotated. Check the first post.

---

<div class="post-metadata">

### Author: ![alexzzzz](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/alexzzzz/32/333491_2.png) [@alexzzzz](https://discussions.unity.com/u/alexzzzz)
#### Post date: [January 6, 2011, 6:24pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/15 "2011-01-06T18:24:27Z")

</div>

Does anyone remember how floating boxes should look?

Is this 45 degree angle ok?

---

<div class="post-metadata">

### Author: ![anon\_19585093](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_19585093](https://discussions.unity.com/u/anon_19585093)
#### Post date: [January 6, 2011, 8:49pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/16 "2011-01-06T20:49:21Z")

</div>

> [@alexzzzz](#):
>
> Does anyone remember how floating boxes should look?
> 
> [![](http://img714.imageshack.us/img714/5076/capturatt.th.jpg)](http://img714.imageshack.us/i/capturatt.jpg/)
> 
> Is this 45 degree angle ok?

I get the same effect when using cubes… I don’t know whether that is what it would actually look like or not. Considering that all the cubes are doing that then there is either a real-life physics reason or a programming-error physics reason. It’s just a guess, but it may have something to do with fluid dynamics…

In most cases though objects like a crate don’t have their center of gravity right in the middle anyways… usually it’s closer to the bottom because the stuff inside is heavier nearer the bottom (or you are packing your crates wrong!). If you imbalance your cubes they might end up floating more level…

**EDIT:**

Now that I think about it some more I may have the reason why. It’s because the only way to calculate the buoyancy affect on an individual chunk of the object is to the proportion of it which is underwater… however when the chunk is rotated the proportion is not linearly related to the chunk’s center point’s position relative to the water-level.

---

<div class="post-metadata">

### Author: ![granada\_1](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@granada\_1](https://discussions.unity.com/u/granada_1)
#### Post date: [January 6, 2011, 8:53pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/17 "2011-01-06T20:53:54Z")

</div>

> [@](#):
>
> In most cases though objects like a crate don’t have their center of gravity right in the middle anyways… usually it’s closer to the bottom because the stuff inside is heavier nearer the bottom (or you are packing your crates wrong!). If you imbalance your cubes they might end up floating more level…

In real life i sapose you would get both 😵‍💫.

dave

---

<div class="post-metadata">

### Author: ![bigkahuna](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bigkahuna/32/291490_2.png) [@bigkahuna](https://discussions.unity.com/u/bigkahuna)
#### Post date: [January 6, 2011, 9:08pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/18 "2011-01-06T21:08:09Z")

</div>

A floating object is considered “at rest” when the center of buoyancy is directly above the center of gravity. I don’t recall what an ideal cube’s resting point is, but I’d guess it’s pretty much what you’re seeing which is similar to an ideal sphere which has no resting point (the center of gravity and center of buoyancy are in the same location).

---

<div class="post-metadata">

### Author: ![anon\_19585093](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon\_19585093](https://discussions.unity.com/u/anon_19585093)
#### Post date: [January 6, 2011, 9:52pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/19 "2011-01-06T21:52:00Z")

</div>

> [@bigkahuna](#):
>
> A floating object is considered “at rest” when the center of buoyancy is directly above the center of gravity. I don’t recall what an ideal cube’s resting point is, but I’d guess it’s pretty much what you’re seeing which is similar to an ideal sphere which as no resting point (the center of gravity and center of buoyancy are in the same location).

Very interesting… I just read up on ship-design, center of buoyancy, “metacenters” and what not. Pretty interesting stuff…

---

<div class="post-metadata">

### Author: ![granada\_1](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@granada\_1](https://discussions.unity.com/u/granada_1)
#### Post date: [January 6, 2011, 10:24pm UTC](https://discussions.unity.com/t/buoyancy-script/432786/20 "2011-01-06T22:24:29Z")

</div>

> [@](#):
>
> Very interesting… I just read up on ship-design, center of buoyancy, “metacenters” and what not. Pretty interesting stuff…

Been thinking about this to,i think random is the word 😵‍💫 (😵‍💫 but then again i usualy am 😄)

Dave

[Next page](https://discussions.unity.com/t/buoyancy-script/432786.md?page=2)
