C# 如何在unity2d无头转轮中生成地面
我目前写了一个游戏,我有两个脚本,在游戏中生成地面。但是,它们不是在玩家到达一个场地的终点时生成的,而是在游戏开始时生成的。我不想发生这种事 有人知道为什么会这样吗?C# 如何在unity2d无头转轮中生成地面,c#,unity3d,C#,Unity3d,我目前写了一个游戏,我有两个脚本,在游戏中生成地面。但是,它们不是在玩家到达一个场地的终点时生成的,而是在游戏开始时生成的。我不想发生这种事 有人知道为什么会这样吗? 请帮我修一下 谢谢 这是我的代码: 脚本1: public class ObjectPooler : MonoBehaviour { public GameObject pooledObject; public int pooledamnt; List<GameObject> pooledObjects; // S
请帮我修一下 谢谢 这是我的代码: 脚本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()
{
}
}
以下是玩家移动时生成地面的基本脚本:
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);
}
}
}
使用UnityEngine;
公共类GroundGeneration:单一行为
{
公众浮标与玩家的距离最近;
公共游戏对象地砖;
公众浮标宽度;
公共转换播放器;
void Start()
{
//生成一个磁贴,这样玩家就不会在开始时掉落
地砖(1);
}
无效分片(int n)
{
int i=0;
//产卵
而(i 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);
}
}
}