Why when i'm adding a space between the cubes on a grid the whole grid size is change?

心不动则不痛 提交于 2020-01-05 07:18:48

问题


The grid is 10x10

This is the original script before adding spaces:

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);
            }
        }
    }
}

And the result: The 4 walls are in place:

Then i changed the line:

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

To

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

And now the result is that the 4 walls are in wrong positions:

Do i need to multiply also the walls blocks too ?

But if i multiply all the blocks already by 1.5f then why the walls blocks are not changing the position too by 1.5f ?


回答1:


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

this line will set localPosition.x = 1.5f * x which would only affect right and bottom walls because, for the top and left walls have x = 0 so multiplication won't affect.

You have to change your comparison to check index value instead of localPosition :

if (x == 0) //TOP

if (z == 0) //LEFT

if (z == gridHeight - 1) //RIGHT

if (x == gridWidth - 1) //BOTTOM

This will add the right blocks in walls independent of position.

Hope this helps :)



来源:https://stackoverflow.com/questions/47576204/why-when-im-adding-a-space-between-the-cubes-on-a-grid-the-whole-grid-size-is-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!