简体   繁体   English

无法将位置设置为动态游戏对象Unity3D

[英]Can't set position to dynamically gameobject Unity3D

I have a stranger problem. 我有一个陌生人的问题。 I want generate a flat tiled terrain for a personal test and the way I follow is: 我想生成一个平铺的地形用于个人测试,我遵循的方法是:

  • Create custom primitive 创建自定义原语
  • Set a tiled group with the same material (child of empty gameobject) 设置具有相同材质的平铺组(空游戏对象的子级)
  • Put group in an empty gameobject for generate a large tiled map (child of main gameobject) 将组放在一个空的游戏对象中以生成一个大的平铺地图(主游戏对象的子对象)

Everything works, but the last section, when I generate map, all empty gameobject spawn at the same space but with correct value in the inspector. 一切正常,但是最后一部分,当我生成地图时,所有空的游戏对象都在同一空间生成,但在检查器中具有正确的值。

Terrain generation: 地形生成:

// Main Camera
public GameObject mainCamera;

// Area Block GameObject
private GameObject areaBlock;
private Transform t;

// Terrain Cube
private GameObject tiledCube;

// Tiled Block size
public int blockSize;

// Cube Renderer
public Material[] mats;

// Terrain Size
public int xSize = 1;
public int zSize = 1;

private void Start()
{
    // Generate Terrain
    GenerateTiledTerrain();

    // Center Camera to terrain
    mainCamera.transform.position = new Vector3((xSize * blockSize) / 2, 5, (zSize * blockSize) / 2);
}

private void GenerateTiledCube()
{
    // Create a terrain block
    tiledCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    tiledCube.transform.localPosition = new Vector3(0, 0, 0);
    tiledCube.transform.localScale = new Vector3(1, 0.1F, 1);
    //tiledCube.isStatic = true;

    // Assing material to terrain block
    int randomMaterial;

    randomMaterial = Random.Range(0, mats.Length);
    tiledCube.GetComponent<MeshRenderer>().sharedMaterial = mats[randomMaterial];

    Destroy(tiledCube);

    int count = 0;

    // Generate tiled block 
    for (int i = 0; i < blockSize; i++)
    {
        for (int j = 0; j < blockSize; j++)
        {
            count++;

            tiledCube.name = "Cube_" + count;
            Instantiate(tiledCube, new Vector3(i, 0, j), Quaternion.identity, areaBlock.transform);
        }
    }
}

private void GenerateTiledTerrain()
{
    Destroy(areaBlock);

    int count = 0;

    for (int i = 0; i < xSize; i++)
    {
        for(int j = 0; j < zSize; j++)
        {
            count++;
            areaBlock = new GameObject("AreaBlock_" + count);
            areaBlock.transform.parent = gameObject.transform;
            areaBlock.transform.localPosition = new Vector3(i * blockSize, 0, j * blockSize);
            GenerateTiledCube();
            //Instantiate(areaBlock, new Vector3(i * blockSize, 0, j * blockSize), Quaternion.identity, gameObject.transform);

        }
    }
}

I have to use new GameObject instead Instatiate() because if I try to instantiate, the position is correct but every clone get the previous child: the first gameobject have 16 children, the second 32, 64... 我必须使用new GameObject Instatiate()代替Instatiate()因为如果尝试实例化,位置是正确的,但是每个克隆都得到前一个子对象:第一个游戏对象有16个子对象,第二个游戏对象有32个子对象...

How can I fix this problem? 我该如何解决这个问题?

Thanks in advance. 提前致谢。

Instantiate clones the input object, including all of its children. 实例化克隆输入对象,包括其所有子对象。 If you assign this result back to the input object (or in some other way give it children), then yes, the new clone will have double what the previous one did. 如果将此结果分配回输入对象(或以其他方式为其赋予子对象),则可以,新克隆将是前一个克隆的两倍。

new GameObject() on the other hand creates an empty game object with no children. 另一方面, new GameObject()创建一个没有子代的游戏对象。 This is by design. 这是设计使然。

areaBlock should be a prefab asset that doesn't change. areaBlock应该是不会更改的预制资产。 You're using it as a scene object that you assign children to (that's what GenerateTiledCube() is doing: assigning children to the areaBlock's transform). 您将其用作分配孩子的场景对象( GenerateTiledCube()所做的就是:将孩子分配给areaBlock的变换)。

What you need to do instead is have two separate objects : one to clone and one in the scene to apply the clones to. 相反,您需要做的是有两个单独的对象 :一个要克隆,另一个在场景中要应用克隆。

Maybe you need to set localPosition for newly gameObject, instead of prefab. 也许您需要为新的gameObject设置localPosition而不是prefab。 Try: 尝试:

var obj = Instantiate(tiledCube...
obj.transform.localPosition = new Vector3(i, 0, j);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM