-2

我正在彼此下方实例化一个平台,您可以在下面的图片中看到

彼此下方的平台

但我想生成这样的平台

我想要的图案

那么我该怎么做呢?您可以在下面阅读我的代码,该代码仅在 Y 位置生成彼此下方的平台。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class platformGeneration : MonoBehaviour
{
    public int numOfPlatforms;
    public GameObject platform;
    public Transform spawnPosition;

// Start is called before the first frame update
    void Start()
    {

        Vector3 pos = spawnPosition.GetComponent<Transform>().position;

        for (int i = 0; i < numOfPlatforms; i++)
        {
            spawnPlatofrm(pos + new Vector3(i+2,-i - 5, 0));



            //spawnPlatofrm(pos + new Vector3(0, -i - 5, -i - 2));

            //for (int j = 0; j < numOfPlatforms; j++)
            //{
            //    spawnPlatofrm(pos + new Vector3(0, -j - 5, -j - 2));
            //}

            //spawnPlatofrm(pos + (Vector3.right * i));


        }
    }



    void spawnPlatofrm(Vector3 spawnPosition)
    {
        Instantiate(platform, spawnPosition, Quaternion.identity);
    }
}
4

1 回答 1

0

您可以尝试用以下方法替换您的启动方法:

    void Start()
    {
        Vector3 pos = spawnPosition.GetComponent<Transform>().position;

        int next = 2;
        int slide = 0;
        for (int i = 0; i < numOfPlatforms; i++)
        {
            spawnPlatofrm(pos + new Vector3(i+next,-i - 5, slide));
            next *= -1;
            if(next == 2) slide++;
        }
    }
于 2019-05-16T10:07:27.863 回答