为什么当我在网格上的立方体之间添加空格时,整个网格大小会发生变化?

时间:2017-11-30 14:48:40

标签: c# unity3d unity5

网格为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);
            }
        }
    }
}

结果:四面墙已到位:

Grid

然后我换了一行:

block.transform.localPosition = new Vector3(x, 0, z);

block.transform.localPosition = new Vector3(x * 1.5f, 0, z * 1.5f);

现在结果是4面墙位置错误:

grid

我是否也需要增加墙块?

但是,如果我将所有块乘以1.5f,那么为什么墙块也不会改变位置1.5f?

1 个答案:

答案 0 :(得分:2)

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

这将在墙中添加与位置无关的右侧块。

希望这会有所帮助:)