正在尝试获取十六进制网格

本文关键字:十六进制 网格 获取 | 更新日期: 2023-09-27 18:23:56

我试图让已经创建的十六进制预制形状在网格中相互对齐。我得到了网格,但我似乎无法让六边形相互适应。修复了y%2,但现在它的设置方式会崩溃。

using UnityEngine;
using System.Collections;
public class boardObject : MonoBehaviour {
// Instantiates a prefab in a grid
public GameObject prefab;
public float gridX = 5f;
public float gridY = 5f;
public float spacing = 2f;
void Start() {
    for (float y = 0.0f; y < gridY; y++) {
        for (float x = 0.0f; x < gridX; x++) {
            // THIS IS WHAT WAS MISSING FOR THIS TO ACTUALLY WORK MAKING A NEW VECTOR 3
            // POSITION AFTER THE LOOP WAS SET UP. EACH NEW hex NEEDED HAVE A NEW POSITION. ->
            Vector3 pos = new Vector3(x, 0.0f, y) * spacing;
            //
            //
            //%2 thingy
            if (y%2.0f==0.0f)
                gridY += 0.5f;
            else 
                gridY -= 0.0f;
            Instantiate(prefab, pos, Quaternion.identity);

        }
    }
}

}

第一张照片是我的,第二张是我正在努力制作的。这是方板六角

这就是我尝试做的

正在尝试获取十六进制网格

using UnityEngine;
using System.Collections;
public class boardObject : MonoBehaviour {
// Instantiates a prefab in a grid
public GameObject prefab;
public float gridX = 5f;
public float gridY = 5f;
public float spacing = 2f;
public float xsize = 1.0f;
public float ysize = 1.0f;
void Start() {
    for (float y = 0; y < gridY; y++) {
        for (float x = 1; x < gridX; x++) { //<<<<<This is where the change was made to get it to work. x = 1 instead of 0.

            Vector3 pos = new Vector3 (x * xsize, 0.0f, y * ysize) ;
            Instantiate (prefab, pos, Quaternion.identity);

            if (y % 2 == 0)
                y += 1;
            else 
                y -= 1;
        }
    }
}
void Update() {

}}