Jon
Jon

Reputation: 43

How to Get Coordinates from Hex Map on Click in Unity

I'm making the board game known as Hex. When I click on a tile, it changes to either blue or yellow, but I need to also be able to know what the coordinates of that tile are.

I can't use...

rend.transform.position;

...because the the coordinates I want to receive look similar to this (0,0) being the bottom left and (0,1) above it: enter image description here

The coordinates in the console are the one's I need to receive.

enter image description here

I printed those out by using the column and row variables when I generated the hex map:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HexMap : MonoBehaviour
{
 // Use this for initialization
 void Start()
 {
    GenerateMap();
 }

public GameObject HexPrefab;

public void GenerateMap()
{
    for (int column = 0; column < 11; column++)
    {
        for (int row = 0; row < 11; row++)
        {
            // Instantiate a Hex
            Hex h = new Hex(column, row);

            Instantiate(HexPrefab, h.Position(), Quaternion.identity, this.transform);

            Debug.Log(column + "," + row);
        }
    }
}
}

I want to be able to get the coordinates when I click a hex tile using this script here:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using UnityEngine;
using UnityEngine.Networking;

public class ColorChange : MonoBehaviour {

public Color[]colors; // allows input of material colors in a set sized array
public SpriteRenderer rend;  // what are we rendering? the hex

public enum Player {ONE, TWO};
public static Player currentPlayer = Player.ONE;

// Use this for initialization
void Start () {
    rend = GetComponent<SpriteRenderer> (); // gives functionality for the renderer
}

void NextPlayer() {

   if( currentPlayer == Player.ONE ) {
      currentPlayer = Player.TWO;
   }
   else if( currentPlayer == Player.TWO) {
      currentPlayer = Player.ONE;
   }
}

// Update is called once per frame
void OnMouseDown () {
    // if there are no colors present nothing happens
    if (colors.Length == 0)
        return;

    if (currentPlayer == Player.ONE)
        rend.color = colors [0];
    else if (currentPlayer == Player.TWO)
        rend.color = colors [1];

    NextPlayer();
}

Here is the script I use to determine where the hex tiles need to be:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hex{

public Hex (int q, int r){
    this.Q = q;
    this.R = r;
}

public readonly int Q; // x
public readonly int R;  // y

static readonly float WIDTH_MULTIPLIER = Mathf.Sqrt(3) / 2;

public Vector2 Position(){
    float radius = 0.513f;
    float height = radius * 2;
    float width = WIDTH_MULTIPLIER * height;

    float vert = height * 0.75f;
    float horiz = width;

    return new Vector2(horiz * (this.Q - this.R/2f), vert * this.R);
}
}

I was thinking I need to be able to assign each Hex Model the value of column and row when it is instantiated, but I'm not sure how to do that. I tried several solutions I found online as well as using GetComponent, but I wasn't able to make them work. If anyone has an idea on how this might be possible I would greatly appreciate it!

Upvotes: 2

Views: 2296

Answers (1)

Tobias K.
Tobias K.

Reputation: 561

The reason why GetComponent is not working in your case is because the Coordinates script is on a child object of the hex prefab. You could access a component on a child object by calling GetComponentInChildren. It would look something like the following:

// Instantiate a Hex
Hex h = new Hex(column, row);

// Instantiate the prefab
GameObject instance = Instantiate(HexPrefab, h.Position(), Quaternion.identity, this.transform);

// Let's find the coorinates component on the hex prefab instance.
Coordinates coordinates = instance.GetComponentInChildren<Coordinates>();

// now you may assign the column and row values to it
// ...

There are many possible approaches to this problem. But just keep in mind where in the prefab hierarchy the components are and you will be fine.

Upvotes: 1

Related Questions