为什么当我在网格的立方体之间添加 space 时,整个网格的大小都发生了变化?
Why when i'm adding a space between the cubes on a grid the whole grid size is change?
网格为 10x10
这是加空格前的原稿:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridGenerator : MonoBehaviour
{
public GameObject gridBlock;
public int gridWidth = 10;
public int gridHeight = 10;
public List<Vector3> positions = new List<Vector3>();
public List<GameObject> blocks = new List<GameObject>();
private GameObject[] wallsParents = new GameObject[4];
void Start()
{
wallsParents[0] = GameObject.Find("Top Wall");
wallsParents[1] = GameObject.Find("Left Wall");
wallsParents[2] = GameObject.Find("Right Wall");
wallsParents[3] = GameObject.Find("Bottom Wall");
GenerateGrid();
}
private void GenerateGrid()
{
for (int x = 0; x < gridWidth; x++)
{
for (int z = 0; z < gridHeight; z++)
{
GameObject block = Instantiate(gridBlock, Vector3.zero, gridBlock.transform.rotation) as GameObject;
block.transform.parent = transform;
block.transform.tag = "Block";
block.transform.localScale = new Vector3(1, 0.1f, 1);
block.transform.localPosition = new Vector3(x, 0, z);
block.GetComponent<Renderer>().material.color = new Color(241, 255, 0, 255);
if (block.transform.localPosition.x == 0)//TOP
{
positions.Add(block.transform.localPosition);
block.transform.parent = wallsParents[0].transform;
block.transform.name = "TopWall";
}
else if (block.transform.localPosition.z == 0)//LEFT
{
positions.Add(block.transform.localPosition);
block.transform.parent = wallsParents[1].transform;
block.transform.name = "LeftWall";
}
else if (block.transform.localPosition.z == gridWidth - 1)//RIGHT
{
positions.Add(block.transform.localPosition);
block.transform.parent = wallsParents[2].transform;
block.transform.name = "RightWall";
}
else if (block.transform.localPosition.x == gridHeight - 1)//BOTTOM
{
positions.Add(block.transform.localPosition);
block.transform.parent = wallsParents[3].transform;
block.transform.name = "BottomWall";
}
blocks.Add(block);
}
}
}
}
结果:4 面墙就位:
然后我改了行:
block.transform.localPosition = new Vector3(x, 0, z);
到
block.transform.localPosition = new Vector3(x * 1.5f, 0, z * 1.5f);
现在的结果是4面墙的位置不对:
我也需要乘以墙块吗?
但是,如果我已经将所有方块乘以 1.5f,那么为什么墙方块的位置也不会改变 1.5f?
block.transform.localPosition = new Vector3(x * 1.5f, 0, z * 1.5f);
此行将设置 localPosition.x = 1.5f * x
,这只会影响右墙和底墙,因为顶墙和左墙有 x = 0
,所以乘法不会影响。
您必须更改比较以检查索引值而不是 localPosition
:
if (x == 0) //TOP
if (z == 0) //LEFT
if (z == gridHeight - 1) //RIGHT
if (x == gridWidth - 1) //BOTTOM
这将在墙上添加正确的方块,与位置无关。
希望这对您有所帮助:)
网格为 10x10
这是加空格前的原稿:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridGenerator : MonoBehaviour
{
public GameObject gridBlock;
public int gridWidth = 10;
public int gridHeight = 10;
public List<Vector3> positions = new List<Vector3>();
public List<GameObject> blocks = new List<GameObject>();
private GameObject[] wallsParents = new GameObject[4];
void Start()
{
wallsParents[0] = GameObject.Find("Top Wall");
wallsParents[1] = GameObject.Find("Left Wall");
wallsParents[2] = GameObject.Find("Right Wall");
wallsParents[3] = GameObject.Find("Bottom Wall");
GenerateGrid();
}
private void GenerateGrid()
{
for (int x = 0; x < gridWidth; x++)
{
for (int z = 0; z < gridHeight; z++)
{
GameObject block = Instantiate(gridBlock, Vector3.zero, gridBlock.transform.rotation) as GameObject;
block.transform.parent = transform;
block.transform.tag = "Block";
block.transform.localScale = new Vector3(1, 0.1f, 1);
block.transform.localPosition = new Vector3(x, 0, z);
block.GetComponent<Renderer>().material.color = new Color(241, 255, 0, 255);
if (block.transform.localPosition.x == 0)//TOP
{
positions.Add(block.transform.localPosition);
block.transform.parent = wallsParents[0].transform;
block.transform.name = "TopWall";
}
else if (block.transform.localPosition.z == 0)//LEFT
{
positions.Add(block.transform.localPosition);
block.transform.parent = wallsParents[1].transform;
block.transform.name = "LeftWall";
}
else if (block.transform.localPosition.z == gridWidth - 1)//RIGHT
{
positions.Add(block.transform.localPosition);
block.transform.parent = wallsParents[2].transform;
block.transform.name = "RightWall";
}
else if (block.transform.localPosition.x == gridHeight - 1)//BOTTOM
{
positions.Add(block.transform.localPosition);
block.transform.parent = wallsParents[3].transform;
block.transform.name = "BottomWall";
}
blocks.Add(block);
}
}
}
}
结果:4 面墙就位:
然后我改了行:
block.transform.localPosition = new Vector3(x, 0, z);
到
block.transform.localPosition = new Vector3(x * 1.5f, 0, z * 1.5f);
现在的结果是4面墙的位置不对:
我也需要乘以墙块吗?
但是,如果我已经将所有方块乘以 1.5f,那么为什么墙方块的位置也不会改变 1.5f?
block.transform.localPosition = new Vector3(x * 1.5f, 0, z * 1.5f);
此行将设置 localPosition.x = 1.5f * x
,这只会影响右墙和底墙,因为顶墙和左墙有 x = 0
,所以乘法不会影响。
您必须更改比较以检查索引值而不是 localPosition
:
if (x == 0) //TOP
if (z == 0) //LEFT
if (z == gridHeight - 1) //RIGHT
if (x == gridWidth - 1) //BOTTOM
这将在墙上添加正确的方块,与位置无关。
希望这对您有所帮助:)