- Home /
How to draw an outline around multiple hexagon game objects?
Hey again everybody :)
My question is, how would I go about creating an outline around multiple hex tiles such as in the picture below? And on that note, around hex tiles in oddly shaped patterns, such as if an object was in the way of multiple tiles? Note: think the new XCom game, the way they show the movement ranges of the units... but in hexagon format.
The backstory of the project I'm working on is that it is a hex-based strategy game, and the movement and attack ranges of the units would be shown on the hex tiles.
I'm stuck as to how to solve this problem though. I've had multiple ideas of how to do it, ranging from using a projector and hex outline shape (wouldn't be valid at some times, as the move range could be blocked & reduced by a wall), using the GL class in unity, and even line renderers.
The GL class and line renderers seem like they could do the trick, but with either of them, I am unable to find the vector3 points of the outlines of the hexagon tiles. Well, I can find them using:
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
...but the vertices it provides me with are vertices around the game object (hexagonal prism as it were) as if the game object was sitting at 0,0,0 world coords, which it is not.
Does anybody have any suggestions of how to solve this, or even suggestions of an alternate way to do it?
This is a very good question, I am also interested in this. If someone has ideas, please let us know !
Answer by MarkFinn · Feb 21, 2013 at 02:15 PM
If it were mine, I'd highlight the affected/affectable tiles, rather than the outline. Much more generic and flexible approach I reckon. It'd allow you have indents in the area, or allow for line of sight and the like much more easily, rather than trying to calculate the outline shape.
That is the way it's being done now, but the issue with that comes when there are ramps. On the ramp, half the horizontally flat tile is inside the ramp, half is outside, like a spike sticking out of the ramp.
I've been working on some line calculations etc, and figured I can do something like this:
void Start () {
hexCenter = target.transform.localPosition;
corners[0] = new Vector3(hexCenter.x+0.43f, hexCenter.y+1, hexCenter.z+0.25f);
corners[1] = new Vector3(hexCenter.x, hexCenter.y+1, hexCenter.z+0.49f);
corners[2] = new Vector3(hexCenter.x-0.43f, hexCenter.y+1, hexCenter.z+0.25f);
corners[3] = new Vector3(hexCenter.x-0.43f, hexCenter.y, hexCenter.z-0.25f);
corners[4] = new Vector3(hexCenter.x, hexCenter.y+1, hexCenter.z-0.49f);
corners[5] = new Vector3(hexCenter.x+0.43f, hexCenter.y+1, hexCenter.z-0.25f);
corners[6] = new Vector3(hexCenter.x+0.43f, hexCenter.y+1, hexCenter.z+0.25f);
int i = 0;
while(i < 6) {
Debug.DrawLine(corners[i], corners[i+1], Color.green, 10);
i++;
}
}
Then using a comparison between each hex tile I can find out ones that have really similar vertice and remove them from the list. Then the ones that remain I'd raycast down from each point to the terrain, and use a line renderer between the points and it should outline the hexes. What do you think?
Your answer
Follow this Question
Related Questions
turret having a range of tiles 0 Answers
Drawing a Range Icon in 3D Space 0 Answers
Hexagonal grid with 120 degree angles 1 Answer
How can I highlight tiles that are in Range of attack? 1 Answer