Being able to build big plaps of landscape is one thing, but earth is nothing without water.
Water is one thing I do not miss from my Warcraft III days. It was at a fixed level (give or take some dodginess), it didn’t flow in any particular direction, and it had very little impact on gameplay. The best rivers were pure decoration constructed from massively skewed waterfall models.
No, it’s time for me to break free and do rivers properly.
She’s a River
Compared to a terrain editor, building river geometry is small fry. It’s a set of cross-sections that are joined up. The points on either side of each cross-section can be shifted around, twisted to follow whatever landscape is necessary.
The most interesting part is the UV-mapping. It’s not quite based on world coordinates, but rather based on the position of a vertex relative to the start of the river. This means that when I do a curve in the river, the UVs are closer together on the inside of the bend and further apart on the outside — providing a bend in the flow of water too, completely as desired and as a consequence of simple systems.
The UV-mapping is based only on horizontal coordinates, so the water texture is stretched when there’s a drop — giving built-in waterfalls with no extra effort! (Well, maybe a few foam and spray particle effects.)
While the river geometry isn’t too bad, I’m still not completely there when it comes to the textures.
For water, I have my heart set on the system as included in the original Unreal and other games on the same engine. They had this tremendous system where you could add ripples to any texture, placing ripples of different sizes and even shapes which would cause the texture to distort in real time.
I have so far managed to get ripplish effects with custom shaders that shuffle UV coordinates according to a noise texture, which will do in the mean time — but rest assured, this chapter will not be closed until I have rebuilt that Unreal Engine system (and/or found and/or bought an equivalent).
Anyway, with the visuals squared away, we need to turn to gameplay. What should water do to things inside it?
Since I’ve only got waist-high rivers at this point, I don’t need to do anything too quesy like play with gravity to make you jump higher. So, to simulate wading I have simply disabled running while inside a river.
Detecting river entry and exit to apply this effect is a slightly interesting question. Unity won’t allow you to use non-convex colliders as triggers — since my river contains waterfalls, it is decidedly concave. Which means that, yay, my old friend segmentation comes back to haunt me: the river has to be split up into independent convex sections so that Unity can tell me when things enter and exit each part.
This brings us on to a problem of overlapping — a unit can and will spend some time in more than one section as it moves up the river and straddles the borders in between sections. I’ve thus had to give knowledge of every section to a parent that counts how many entry and exit events there have been, so it can detect when you truly leave the river and not misfire when you pass between parts of it.
Once the problem of detecting entry and exit is sorted, I can move on to extending my buff system. Up until this river work there was a single buff in the system — stun. Stun’s pretty special because it powers through most of the engine; every action a unit can take is halted by stun, from user input to the animations freezing up.
Wading through a river, however, only affects your legs. I was originally expecting to reduce your movement speed, but then I realised that I already have this walk/run system in place. Walking is always the same sedate speed for every character in the entire universe, so why mess with movement speeds that could conflict as more status effects are added when I can simply turn off the ability to run?
I’m all about easy answers, me.