如何在具有一些不同位置的模式中彼此向下生成平台Unity

时间:2019-05-16 06:28:29

标签: c# unity3d game-development

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

Platforms Below Each Other

但是我想生成这样的平台

The pattern what i want

那么我该怎么做呢?在下面,您可以阅读我的代码,该代码仅在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);
    }
}

1 个答案:

答案 0 :(得分: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++;
        }
    }