- - Space Game
- - Turning It Around
- - Persistent Data
- - Hex Grids and You
Previously on Space Game . . . .
Last time I got busy with sending data between scenes and saving/loading in-game data between sessions. I still have a lot of work to do on that when I figure out exactly what data I’ll be saving but today we’re going to begin work on something else, the shipyard! This will be extremely complex, at least at first, because I want to use a hex grid as well as click and drag.
As you can see, I’ve changed my task list a bit for better organization.
- Main Menu
Create- Background
- Marketplace
- Navigation
- Settings
- Shipyard
- Hex Grid
- Ship Design
- Click and Drag
- Space
- UI
- Objectives
Simple Test Objective- Real Objectives
- Objective Indicator
- Gravity Wells
- Camera Zoom Control
Pause MenuThrust MechanicShip Rotation
Saving/Loading
And Now on Space Game . . .
I have so many pages bookmarked about hex grids in Unity. Most of them appear to be focused on 3D though, and my game (so far) is fully 2D. I’m sure I can just take what the tutorials and find a way to implement them in my game, but working with hex grids looks extremely complicated. After all this research I’m starting to rethink the way I’m approaching my Shipyard.
Bookmarks
- Unity – Manual: Hexagonal Tilemaps
- Isometric Grid Snapping – Unity Forum
- Snap hex grid – Unity Forum
- Hex Map 1
- 2d : Hexagonal Grid Beginner – Unity Answers
- Game Development with Unity 2D – part 5: A grid of Roman centuriae – Design a Game
- Unity – Scripting API: Grid
Rethinking My Approach
Let me explain my idea and my original approach to the shipyard. The shipyard is going to be a place where you get to build your ship. I know, so original, but there’s more! The idea was to use hexagonal tiles to represent pieces of the ship. Each tile could have up to six connection points where you can attach pieces. Now you understand why I want to use hex grids.
So, you start off with your ship’s cockpit, which has six points you can connect to. You have other pieces you can then drag to your ship, which will snap to the nearest available connection point. There will be more involved, but this is the most basic principle of ship design. Originally, I assumed I would need to have a hexagonal grid for the pieces to snap to. After thinking it over, I still believe I’ll need it, no matter how complicated it will be.
The other problem I’m running into is Unity has a built in hexagon tilemap now (was implemented sometime in 2018). Most of the tutorials I’m finding are older, meaning they may be outdated and unnecessary. Following those tutorials could be pointless. So I dug deep into Unity’s Grid.
Starting Small
So, to figure it out I’m starting simple. First, a UI with some buttons. One each to increase or decrease the X and Y coordinates. Drop a sprite into a hex grid. Then comes the fun part. How does this thing even work. Trial and error learning baby!
I think what I need is to take the vector of the sprite and feed it into WorldToCell. That should give me a simple X/Y system. Then my buttons are set to increment those numbers, then transform the sprite to a new location using CellToWorld.
We can use a script on the sprite to always know where it is in the grid. For testing, in the actual shipyard I’ll probably have to do it differently. If this even works . . . right now it’s all hypothetical.
Going Big
And after a lot of googling and trial and error I actually got it to do what I wanted. I have four buttons on the UI that I can press. Each press will increase/decrease the X/Y value of the sprite’s grid position. Now that I know how this works, I can begin earnestly building the shipyard. There’s so much that will be going on there I have a feeling it will take a while.
For those interested in what I did, it’s pretty simple stuff in the end. First, a hexagonal grid object. Then I dropped a sprite (“darkgrey_01”) in the world and childed it to the grid. I have a script called “GridTest” attached to the sprite. Finally, I have an empty game object called ShipyardUI with a script called “Shipyard” attached.
GridTest Script:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class GridTest : MonoBehaviour { GridLayout gridLayout; Vector3Int cellPosition; // Start is called before the first frame update void Start() { gridLayout = transform.parent.GetComponentInParent(); cellPosition = gridLayout.WorldToCell(transform.position); } // Update is called once per frame void Update() { transform.position = gridLayout.CellToWorld(cellPosition); } public void Move(int XChange, int YChange) { cellPosition.x += XChange; cellPosition.y += YChange; } }
Shipyard Script:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Shipyard : MonoBehaviour { GameObject referenceObject; public int X; public int Y; private void Start() { referenceObject = GameObject.Find("darkgrey_01"); } private void OnGUI() { if(GUI.Button(new Rect(10,100,100,30), "X+")){ referenceObject.GetComponent().Move(1, 0); Debug.Log("X+ = " + X); } if (GUI.Button(new Rect(10, 140, 100, 30), "X-")){ referenceObject.GetComponent ().Move(-1, 0); Debug.Log("X- = " + X); } if (GUI.Button(new Rect(10, 180, 100, 30), "Y+")){ referenceObject.GetComponent ().Move(0, 1); Debug.Log("Y+ = " + Y); } if (GUI.Button(new Rect(10, 220, 100, 30), "Y-")){ referenceObject.GetComponent ().Move(0, -1); Debug.Log("Y- = " + Y); } } }
Next Time on Space Game . . .
So I didn’t actually get anything new done on space game, but I learned a lot about hex grids in Unity. The idea was originally daunting because so much information out there is old and outdated. Now it is time to put all this knowledge to use!
- Main Menu
Create- Background
- Marketplace
- Navigation
- Settings
- Shipyard
Hex Grid- Ship Design
- Click and Drag
- Space
- UI
- Objectives
Simple Test Objective- Real Objectives
- Objective Indicator
- Gravity Wells
- Camera Zoom Control
Pause MenuThrust MechanicShip Rotation
Saving/Loading