简体   繁体   English

如何在 Unity 中生成不重叠的随机 2D 平台?

[英]How to spawn random 2D platforms that do not overlap in Unity?

I'm looking for a way to randomly spawn platforms that do not overlap with each other.我正在寻找一种随机生成不相互重叠的平台的方法。 All the tutorials I found on this topic are for an endless runner game type, and my project is not like that.我在这个主题上找到的所有教程都是针对无尽的跑步游戏类型的,而我的项目不是这样的。 So far I know how to spawn my platforms but I need those gaps between them.到目前为止,我知道如何生成我的平台,但我需要它们之间的那些差距。 I'm a total begginer in Unity and C# so I'm looking for a simple code if possible.我是 Unity 和 C# 的初学者,所以如果可能的话,我正在寻找一个简单的代码。

My code now:我现在的代码:

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

 public class GameStateManager: MonoBehaviour {
public GameObject YellowPrefab;
    public int howManyYellow;

 void Start()
    { GameObject tmpYellow;

        for (int i = 0; i < howManyYellow; i++)
        {
            tmpYellow = Instantiate(YellowPrefab, new
                        Vector3(Random.Range(-50, 50), Random.Range(-
                        40, -17), 0), Quaternion.identity);
        }
 }

My platforms have box colliders used by platform effector, if that information is needed.如果需要该信息,我的平台有平台效应器使用的盒子对撞机。

Edit: If possible, it would be nice to be able to set max distance between the random platforms, but if it's something hard to do with that kind of code then it's OK without that:)编辑:如果可能的话,能够设置随机平台之间的最大距离会很好,但是如果用那种代码很难做到这一点,那么没有它就可以了:)

Look into Physics2D.OverlapBox() or related functions, depending on your needs.根据您的需要,查看Physics2D.OverlapBox()或相关函数。 With this you can check if your object's collider is overlapping with any other collider, like so:有了这个,您可以检查对象的对撞机是否与任何其他对撞机重叠,如下所示:

for (int i = 0; i < howManyYellow; i++)
{
    tmpYellow = Instantiate(YellowPrefab, 
                            new Vector3(Random.Range(-50, 50), 
                            Random.Range(-40, -17), 0), 
                            Quaternion.identity);

    BoxCollider2D tmpYCollider = tmpYellow.GetComponent<BoxCollider2D>();
    tmpyCollider.enabled = false; // Disable object's own collider to prevent detecting itself

    // while collider overlaps, move your object somewhere else (e.g. 17 units up)
    while (Physics2D.OverlapBox(tmpYCollider.bounds.center, tmpYCollider.size, 0) != null)
    {
        tmpYellow.transform.Translate(new Vector3(0, 17));
        // or do something else
    }

    tmpyCollider.enabled = true; // enable the collider again
}

From what I understand, you want to create a bunch of platforms randomly in the area of (-50,-40) -> (50,-17).据我了解,您想在 (-50,-40) -> (50,-17) 区域内随机创建一堆平台。

If you do not want a platform to be created in the same spot you need to keep track of the location that all of the platforms were placed in.如果您不希望在同一地点创建平台,则需要跟踪所有平台所在的位置。

public GameObject YellowPrefab;
public int howManyYellow;
private List<Vector3> locations;

void Start()

locations = new List<Vector3>();
{ 
    GameObject tmpYellow;

    for (int i = 0; i < howManyYellow; i++)
    {
        bool hasItem = false;
         Vector3 tempLocation;
        do{
            tempLocation = new Vector3(Random.Range(-50, 50), Random.Range(-40, -17), 0);
            foreach (Vector3 item in locations)
            {
                if(tempLocation == item){

                    hasItem = true;
                }
            }
        } while(hasItem);
        locations.Add(tempLocation);

         tmpYellow = Instantiate(YellowPrefab, tempLocation, Quaternion.identity);
    }
}

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

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