- Generates hexes landscapes, with heights (Civ5 bored me by being flat-McFlat-in-flatland)
- Every hex is selectable, using custom fix for Unity’s broken mouse-click handler (see below)
- Any object sitting on landscape is selectable (ditto)
- Selected units move if you click any of the adjacent hexes (shown using f-ugly green arrows on screenshot)
The green “you can move here” arrows look like spider-legs at the moment. #TotalFail. Next build I’m going to delete them (despite having spent ages tweaking the procgen mesh generation for them, sigh) and do something based on wireframe cages, I think.
I started with simple prototyping around hexes, but soon found that it’s worth investing the time to implement all the primitives in Amit’s page on Hexagon grids for games: http://www.redblobgames.com/grids/hexagons/
In practice, especially the ability to create a class that lets you do “setHex( HexCoord location, GameObject items )” and “getContentsOfHex( HexCoord location )” and things like “getNeighboursOf” … is very rapidly essential.
Mouse clicks in Unity
IMHO: work pretty badly. They require the physics engine, which – by definition – returns the WRONG answer when you ask “what did I click on?” (it randomises the answer every click!). They also fundamentally oppose Unity’s own core design (from the Editor: when you click any element of a prefab, it selects the prefab).
So I wrote my own “better mouse handler” that fixes all that. When you click in scene, it automatically propagates up the tree, finds any listeners, informs them what was clicked, and lets you write good, clean code. Unlike the Unity built-in version.
Procedural meshes for arrows
With hindsight, I should have just modelled these in blender. But I thought: I want a sinusoidal curve arrow; how hard can it be? I may want to animate it later, by destroying/adding points – that would be a lot of work with Unity’s partial animation system (it’s great for humanoids, less great for geometry) – but animating points in a mesh from C# code is super-easy.
In the end, I spent way too long tweaking the look, and on having 2-sided polygons that broke the Unity5 Standard shader by being too thin (on the plus side: I now know what that mistake looks like, and I’ll recognize it in future Unity projets. It has a very peculiar, unexpected, look to it).
I should have just made them in Blender, and – if I got as far as wanting to animate them – re-modelled in source code later (or found a “convert blender file to C vertices array” script, of which I’m sure there are hundreds on the web. Doh!
2 replies on “Simple #civ5 clone in Unity: hexes, movement, unit selection”
Since you added a commit hash, is the code on a public repo?
Couldn’t find the link to it (nor anywhere on your site)
The hash is so that later – when the game launches – I can come back and make demos or timelapse videos (or something) of each build and show how rubbish they were :), and how much changed month to month.
I’m about 1 week of development time away from having a playable demo. Unfortunately, I have to find a new job ASAP, so I’ve had to stop all work on this now. Otherwise I’ll have nowhere to live!