如何在一个无休止的奔跑者中产生地面



我目前正在写一个游戏,我有两个脚本在游戏中生成地面。然而,它们不是在玩家到达一个场地的尽头时生成的,而是在游戏开始时生成的。我不想发生这种事。

有人知道为什么会这样吗
请帮我解决这个问题。

谢谢!

这是我的代码:

脚本1:

public class ObjectPooler : MonoBehaviour
{
public GameObject pooledObject;
public int pooledamnt;
List<GameObject> pooledObjects;
// Start is called before the first frame update
void Start()
{
pooledObjects = new List<GameObject>();
for (int i = 0; i < pooledamnt; i++)
{
GameObject obj = (GameObject)Instantiate(pooledObject);
obj.SetActive(false);
pooledObjects.Add(obj);
}
}
public GameObject GetPooledObject()
{
for (int i = 0; i < pooledObjects.Count; i++)
{
if (pooledObjects[i].activeInHierarchy)
{
return pooledObjects[i];
}
}
GameObject obj = (GameObject)Instantiate(pooledObject);
obj.SetActive(false);
pooledObjects.Add(obj);
return obj;
}
// Update is called once per frame
void Update()
{
}
}

脚本2:

public class 
GroundGenerator : MonoBehaviour
{
public GameObject thePlatform;
public Transform GenOnPoint;
public float DistanceBetween;
private float PlatformWidth;
public float DistanceBewtweenmin;
public float Di stanceBetweenmax;
public ObjectPooler objectpool;
public GameObject[] thePlatforms;
private int platformSelecter;
// Start is called before the first frame update
void Start()
{

}
// Update is called once per frame
void Update()
{



GameObject newPlatform = objectpool.GetPooledObject();
newPlatform.transform.position = transform.position;
newPlatform.transform.rotation = transform.rotation;
newPlatform.SetActive(true);


}
}

以下是一个在玩家移动时生成地面的基本脚本:

using UnityEngine;
public class GroundGeneration : MonoBehaviour
{
public float ClosestDistacnceFromPlayer;
public GameObject GroundTile;
public float TileWidth;
public Transform Player;
void Start()
{
//Spawn a tile so that the player won't fall off at the start
SpawnTile(1);
}
void SpawnTile(int n)
{
int i = 0;
//Spawn n tiles
while (i < n)
{
Instantiate(GroundTile, transform.position, Quaternion.identity);
i++;
//Teleport the "Ground generator" to the end of the tile spawned
transform.position += TileWidth * Vector3.right; // Or use Vector3.forward if you want to generate ground on z axis.
}
}

void FixedUpdate()
{
if (Vector3.Distance(Player.position, transform.position) <= ClosestDistacnceFromPlayer)
{
SpawnTile(1);
}
}

}

GroundTile的原点应位于它与上一个瓷砖相交的点,并且它应该是预制的。地面将在包含地面生成脚本的对象的位置开始生成。

最新更新