Making a Game in Unity 004: Flotation

Just so you know, this is not a tutorial series! I am writing this series mainly to give you a behind the scenes look at the process of making a game in Unity, and to document the development of this project.

See other posts in this series.


Making a game in Unity 004

In the last two weeks, I most notably got flotation working, despite having another power outage and getting quite sick.

Monday

Since I hadn’t finished my blog post for last week (my power went out last night), I did that this afternoon.

Afterwards, I continued working on flotation mechanics, and I figured out a way of doing it. However, it’s currently solely based on how far below the surface the object is—it doesn’t take into account size or mass. This could cause some issues a little later on, but I’ll have to wait and see until I test it with larger objects like ships.

Tuesday

Today I made the necessary changes to allow my floating objects to vary in size. So far my fifty meter long “ship” (just a rectangular prism) is floating nicely, but I need to make some changes to where on the object the forces are applied.

Wednesday

This evening I worked on getting the boat to float properly with the waves, but I ran into a problem. I’m simulating flotation by testing the depth of five points throughout the ship, and applying upward forces based on said depth.

The problem is getting these points to rotate properly with the ship. Essentially it’s the same issue I had when I was trying to make my own basic collision system. At this point I’m unsure of what I’ll do to sort this out.

Thursday

Today was a relatively unproductive day—in large part due to my indecision regarding my physics system. As I explained yesterday, I need to get these points to rotate with the ship the same way child objects rotate with their parents in Unity.

As far as I can tell right now, this is no easy task and would require a lot of time. Since this is already a massive project, I pretty much have two options. Either I can import a bunch of Unity libraries or I can move the server code into a Unity project and run it in headless mode.

I’ve already attempted the former, which resulted in a SecurityException, and from the research I’ve done it looks like I can’t use these libraries outside of Unity. Maybe I need to poke around some more, but so far that option isn’t looking too great.

As for running the server in Unity’s headless mode, I’ve heard it’s not too great for performance. It would however allow me to build the map through Unity’s editor, and I would have access to all the usual classes. Taking the performance hit may end up being a necessary sacrifice.

Friday

Today was pretty busy.

Saturday

This morning my power went out (the weather is still crazy here). It came back later in the afternoon, however my internet was still down—meaning I wasn’t able to get anything done. Instead, I cleaned my work space and went through a bunch of schoolwork from last year to see if I still need any of it, which was way overdue.

Sunday

This afternoon I wrote today’s blog post about Getting Over the Programming Hump. Afterwards, I started looking more into the issue I explained on Thursday. After talking to some people on Discord, I’m pretty certain that I can’t use Unity’s libraries in a console app (at least not without substantial effort).

I’d prefer to keep the server out of Unity, so I decided to look closer at what BEPUphysics offers. Thanks to IceCold on Discord for putting me on the right trail! He pointed out the existence of BEPU’s CompoundBody class to me. After poring over the CompoundBody, RigidTransform, and Quaternion classes on GitHub, I (sort of) pieced together how they work.

Monday

Today I managed to reproduce the positional mechanics of a child object using several methods from the classes I mentioned yesterday. I’m very happy about this, since it’ll allow me to keep my server independent from Unity.

I also got a little more complex flotation working with a larger object, but the values still need a lot of tweaking.

Tuesday

I spent today sick and asleep in bed. I started feeling it yesterday, but this morning when I woke up, everything hurt and I was super tired.

Wednesday

Although I was still sick, I did a little bit of work today. I tweaked the flotation values a bunch, so that’s functional now. It’s still far from perfect, but definitely acceptable.

Large object floating on waves
Large objects now float (mostly) realistically. Sorry for the low resolution, it’s an in-editor screenshot.

I also made my console logger thread safe, separated the console logger thread from the command thread, and cleaned up and commented a whole bunch of code.

Thursday and Friday

My cough got worse on Thursday so I ended up spending most of the day in bed. Although it started (slightly) improving Friday, I didn’t feel up to working on the game again just yet.

Saturday

Today I started feeling better. My cough isn’t gone and my nose is 100% stuffed now, but my throat doesn’t hurt nearly as much anymore. I completed a few miscellaneous tasks that needed doing, on the project and otherwise, but there’s nothing interesting to report.

Sunday

Today I added the System.Runtime.Numerics NuGet package to the server and added some Vector3 and Quaternion conversion methods. This will allow me to easily switch physics libraries in the future if I want/need to.

What’s next?

Over the next two weeks I’ll be working on making the ships functional. That means wind, steering, allowing players to walk on them properly, etc. I may also do some modeling so I have an actual boat to work with—I’m getting quite tired of the cubes.

See other posts in this series.