Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我';m在网格上的立方体之间添加一个空格,整个网格大小是否改变?_C#_Unity3d_Unity5 - Fatal编程技术网

C# 为什么我';m在网格上的立方体之间添加一个空格,整个网格大小是否改变?

C# 为什么我';m在网格上的立方体之间添加一个空格,整个网格大小是否改变?,c#,unity3d,unity5,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; p

网格为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面墙的位置不对:

我是否也需要乘以墙块

但是如果我将所有的块乘以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
这将在墙中添加与位置无关的右侧块


希望这有帮助:)

我忘了问网格大小是否为8x7?现在它是10x10,12x12也可以正常工作,但7x8或9x3不行。我建议的逻辑应该可以工作,不管网格大小如何。禁止stackoverflow不工作。你应该正确地解释你的问题,而不是说我应该在这里编辑或解释新问题吗?我的意思是,举例来说,如果宽度是7,高度是8,那么墙就不会像以前那样就位。这就是当我点击高亮显示网格时的意思,这是7x8时的墙:你能分享你的最新代码吗,可能在pastebin.com或gist.github.com上,刚刚意识到墙对象不在原点(0,0,0)。这可能是个问题吗?
block.transform.localPosition = new Vector3(x * 1.5f, 0, z * 1.5f);
if (x == 0) //TOP

if (z == 0) //LEFT

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

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