【问题标题】:Attempting to get a Hex grid试图获得一个十六进制网格
【发布时间】:2015-11-23 19:56:11
【问题描述】:

我试图让已经创建的六角预制形状以网格形式相互对齐。我得到了网格,但我似乎无法让六角形相互适应。修复了 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);


        }
    }

}

}

第一张图片是我拥有的,第二张是我正在尝试制作的。 This is the square board hex

This is what I am trying to do

【问题讨论】:

  • 我很确定y%0 没用。你的意思是y%2
  • 谢谢你,我修好了,但还是不行。
  • 我的猜测是水平间距与垂直间距不一样?这是因为六边形的宽度与高度不同(与正方形相比),因此您必须将您的 xy 值乘以不同的数量,而不是乘以整个向量(无论如何这都是一件令人困惑的事情) .
  • 我想我明白你在说什么,但我不知道我会如何为每一行奇数的十六进制做到这一点。为了让山峰进入山谷。如果它们是正方形,网格本身就很好。
  • Vector3 pos = new Vector3(x*1.75, 0.0f, y*2); 1.75 值的构成(你必须玩它)。 “奇数”行无关紧要。如果第 2 行移过来,则第 3 行也需要移过这么多......再加上一些,所以它靠得很近,这意味着第 4 行需要移动......等等。我的意思是:您将它们视为正方形,而它们不是正方形:它们的边界框是矩形的。

标签: c# unity3d grid hex tiles


【解决方案1】:
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() {

} }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    相关资源
    最近更新 更多