-1

当我在为一个建塔游戏(比如这个)编写代码时,我需要一种生成塔的方法,所以我使用了 Instantiate 并为转换创建了一个名为“place”的变量,并尝试使用 for 循环变量。但它没有用。

这是我的脚本:

using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;

public class spawner : MonoBehaviour
{
    public float3 startingLocation;
    Transform place;
    public void SpwanTower(float xx, float yy, float zz, GameObject Brick)
    {

        for (int i = 0; i < yy; i++)
        {
            for (int e = 0; e < xx; e++)
            {
                for (int o = 0; o < zz; o++)
                {

                    Instantiate(Brick,place);
                }
            }
        }
    }
}
4

2 回答 2

1

我认为您想要以下内容:

Instantiate(Brick, new Vector3(e, i, o), Quaternion.identity);

因为您没有使用e,io变量。

于 2019-10-11T12:58:30.293 回答
0

试试这个

public float3 startingLocation;
Transform place;
public void SpwanTower(float xx, float yy, float zz, GameObject Brick)
{
    Vector3 currPos = new Vector3(
        startingLocation.x,
        startingLocation.y,
        startingLocation.z
    );

    for (int i = 0; i < yy; i++)
    {
        for (int e = 0; e < xx; e++)
        {
            for (int o = 0; o < zz; o++)
            {
                //you can change the plus sign to determine to what direction the tower will be built
                currPos.x = startingLocation.x + e;
                currPos.y = startingLocation.y + i;
                currPos.z = startingLocation.z + o;
                Instantiate(Brick, currPos, Quaternion.identity);
            }
        }
    }
}

OBS:new Vector3(e, i, o)与其做 ,不如更改 currPos 的 x、y、z 值,因为这将执行多次

希望它有效。

于 2019-10-12T22:06:14.987 回答