生成新对象到随机对象的位置

时间:2020-08-10 01:10:49

标签: c# unity3d

我需要帮助,每次生成随机发生时,我都有一个用于生成级别的代码,因此我无法做到,因此在级别的某个部分到达触发器时,在最后一个平台上,生成了更多平台,我尝试通过/var/www/html/project1.loc/index.php搜索对象,但最终我没有成功,有人可以解释如何做到最好吗?所有代码都附在下面,因为我有一代人,等等。

pos = GameObject.Find("Platform 4 End Platform(Clone)").transform.position;

我还将附上编辑的照片 https://i.stack.imgur.com/VBAd0.png

1 个答案:

答案 0 :(得分:1)

您似乎拥有List中的所有平台spawnedPlatforms。您也已经设置了标签,因此可以遍历列表并检查标签的“ EndPlatform”对吗?如果您要使用特定的EndPlatform,

var platformToFind = "3";
foreach (var platform in spawnedPlatforms) {
    if (platform.tag == "EndPlatform" && platform.name.Contains(platformToFind)) {
        // Do something
    }
}

根据您的评论,听起来您希望每次播放器跳上10个平台时都想要创建10个以上平台。

这意味着您每10个平台需要一个触发器平台。因此,您应该将if (i == amountToSpawn - 90)更改为if (i % 10 == 0)。这称为模运算符,它为您提供除法的余数。例如。 13%10 = 3,或者我们的情况是:10%0 = 0、20%0 = 0、30%0 = 0,等等。

它看起来像这样:

void Awake()
{
    beginAmount = 0;
    amountToSpawn = 10;
    InstantiateLevel();
}
const int MAX_PLATFORMS = 100;
void InstantiateLevel()
{

// Notice the conditional changed here
for (int i = beginAmount; i < beginAmount + amountToSpawn; i++)  {

            GameObject newPlatform;

            if (i == 0) {
                newPlatform = Instantiate(startPlatform);
            } else if (i == MAX_PLATFORMS) { // End Platform
            } else if (i % 10 == 0) {
                newPlatform = Instantiate(triggerPlatform);
                newPlatform.tag = "triggerPlatform";
            } else { // Normal platform
            }
}

然后在您的播放器脚本中,您将得到类似的内容:

void OnCollisionEnter(Collision other) {
   if (other.gameObject.tag.CompareTag("triggerPlatform")) {
       // Tell the level generator to spawn more platforms
       levelGenerator.beginAmount = levelGenerator.beginAmount + 11;
       levelGenerator.amountToSpawn = 10;
       levelGenerator.InstantiateLevel();
   }
}
相关问题