gpt4 book ai didi

c# - 生成新对象到随机对象的位置

转载 作者:行者123 更新时间:2023-12-04 03:59:21 25 4
gpt4 key购买 nike

我需要帮助,我有一个生成关卡的代码,每次生成都是随机发生的,所以我无法做到,当在关卡的某个部分达到触发器时,在关卡结束时在最后一个平台上,生成了更多平台,我尝试通过 pos = GameObject.Find("Platform 4 End Platform(Clone)").transform.position; 搜索对象,但是在最后我没有成功,有人可以解释如何做到最好吗?下面附上所有代码,如我有生成等。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class LevelGeneratorRock : MonoBehaviour
{

[SerializeField]
private GameObject startPlatform, endPlatform, platformPrefab, triggerPlatform;

private float blockWidth = 0.5f, blockHeight = 0.2f;

[SerializeField]
private int amountToSpawn = 100;
private int beginAmount = 0;


public Vector3 pos;



private List<GameObject> spawnedPlatforms = new List<GameObject>();

[SerializeField]
private GameObject playerPrefab;

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
newPlatform = Instantiate(endPlatform);
newPlatform.tag = "EndPlatform";
}
else if (i % 10 == 0)
{
newPlatform = Instantiate(triggerPlatform);
newPlatform.tag = "triggerPlatform";
}
else
{ // Normal platform
newPlatform = Instantiate(platformPrefab);
}




spawnedPlatforms.Add(newPlatform);
int left = Random.Range(0, 2);

if (left == 0)
{

newPlatform.transform.position =
new Vector3(pos.x - blockWidth, pos.y + blockHeight, pos.z);

}
else
{

newPlatform.transform.position =
new Vector3(pos.x, pos.y + blockHeight, pos.z + blockWidth);

}

pos = newPlatform.transform.position;

// fancy animation
if (i < 25)
{

float pos = newPlatform.transform.position.y;

newPlatform.transform.position =
new Vector3(newPlatform.transform.position.x,
newPlatform.transform.position.y - blockHeight * 3f,
newPlatform.transform.position.z);

newPlatform.transform.DOLocalMoveY(pos, 0.3f).SetDelay(i * 0.1f);

}

} // for loop



} // instantiate level

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









}
}// class









































我也会附上小编的照片 /image/VBAd0.png

最佳答案

看起来您拥有 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();
}
}

关于c# - 生成新对象到随机对象的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63332883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com