So, now we have our island proper with water. What is the next big thing about ProjectSalt? Multiple islands and sailing between them. That means we need to be able to generate different islands that are both random and the same for the same world seed. We need a couple more things like being able to walk on islands and have a boat that we can use to get to the other islands.
To deal with the walking on the islands, I use a mesh collidder on my island, feed it the mesh, and then instantiate a Unity Standard Assets FPSCharacterController. It casically works right out the box and we don’t really have to fuzz about anything. It could use some serious tweaks and a little bit of script changing, but it get’s the job done.
Now that we can traverse our island, we need another island to go to. The easiest way to do that would be simply to create a second island or copy the one we already have and place it a bit away, but where’s the actual fun in that. We’ll start by imagening that the world is split up into rectangles. The useful thing about that is that we can put an island into each rectangle, the max size of the island being the rectangle itself if we choose too, and we’d never have any intersecting islands unless we specifically wanted them to, say for example to make larger islands. Now we need to find proper random numbers so we can put islands around us on the grid, and a sequentially seeded Random function won’t do the trick. I found a blog post by Rune Skovbo Johansen dealing with exactly what we need. I ended up using the XXHash class he mentions and so far it works like a charm. We can now generate random numbers that will both be reliably the same when revisiting the island and still have a great randomness.
Now that we have multiple islands, we’re going to need to be able to move between them. We need a boat. The simplest way to make a boat would be to simply have it travel along the ground plane in a direction of our choosing, but again, where’s the fun in that. We’re going to implement it proper, waves and bouyancy and all.
First, we need to be able to sample the waves so that it feels like we’re actually moving across a body of water that’s in motion. We can do that quite simply by mimicing the scroll function in the water shader, but on the CPU, and sample the texture with that offset. The sample functions behave roughly the same so it’s basically just copying the wave offsetting part of the vertex function in the shader and put that in a script.
Now that we have the ability to sample the height of a wave at any given time, let’s make it a bit more exciting by actually using bouyancy. We’ll be using Archimede’s Principle for this endeavor. I found a great video by Khan Academy that really rocks going through the mathematics behind it. I also came across a great post by alexzzzz at the unity forums with a very useful bouyancy script that we can almost use right off the bat. I only used a small part of it but as I understand it you’d be able to use arbitrary objects for bouyancy as it creates a voxel representation of the collider to simplify the math. You can use the script just as it is or only the most useful parts for our goal, makes no difference, as long as it floats on the waves and we can use it to get to our next island.
Don’t forget to add bouyancy to the player character!