如何一次从随机位置生成一个对象?
How t spawn objects from random places one at a time?
我正在制作一款 android 游戏,我需要从顶部生成物品。我希望生成位置在我的场景顶部水平随机。我使用了这些代码,这些代码没有给我任何随机的东西,而且多个游戏对象同时从同一个生成器中生成。我想一次生成一个游戏对象,而 oher 应该只在第一个从游戏场景中销毁后生成。请帮助我,我是 unity 以及 c# 和 Whosebug 的新手。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject[] spawnableItems;
// Update is called once per frame
void Update()
{
bool isTimeToSpawn (GameObject itemGameObject)
{
spawnableSprites item = itemGameObject.GetComponent<spawnableSprites>();
float spawnDelay = item.seenEverySeconds;
float spawnsPerSecond = 1 / spawnDelay;
if(Time.deltaTime > spawnDelay)
{
Debug.LogWarning("SpawnRate Capped By FrameRate");
}
float threshold = spawnsPerSecond * Time.deltaTime;
if(Random.value < threshold)
{
return true;
}
else
{
return false;
}
}
foreach (GameObject item in spawnableItems)
{
if(isTimeToSpawn(item))
{
Spawn(item);
}
}
}
void Spawn (GameObject myGameObject)
{
GameObject myItem = Instantiate(myGameObject) as GameObject;
myItem.transform.parent = transform;
myItem.transform.position = transform.position;
}
}
void Start()
{
// will wait 2 seconds then call the function Spawn() every 0.3 seconds
InvokeRepeating("Spawn", 2.0f, 0.3f);
}
void Spawn()
{
GameObject myItem = Instantiate(myGameObject) as GameObject;
myItem.transform.parent = transform;
myItem.transform.position = new Vector2(Random.Range(0, width) , height);
}
您想在顶部随机生成一个对象,然后仅在第一个对象被销毁后才生成另一个对象?正确的?
看起来您不需要 spawn/delete 多个对象。
你知道在游戏开发中,我们会尽量提高效率,有时你需要的只是将旧对象用作新对象,然后欺骗玩家认为它是新的。
所以我的建议是不要删除你的对象,只需再次将它的位置设置在顶部,就像这样:
float spawnXmin = 0;
float spawnXmax = 100;
float spawnY = 0;
float spawnZ = 0; // * numbers are just for demonstration
myItem.transform.position = new Vector3(Random.Range(spawnXmin, spawnXmax), spawnY, spawnZ);
也不要在 Update() 中使用这些代码,任何你在 Update 中放入的东西都会花费你很多,因为它会在每一帧中 运行,所以尽可能避免在 Update() 中放入东西可能。
我正在制作一款 android 游戏,我需要从顶部生成物品。我希望生成位置在我的场景顶部水平随机。我使用了这些代码,这些代码没有给我任何随机的东西,而且多个游戏对象同时从同一个生成器中生成。我想一次生成一个游戏对象,而 oher 应该只在第一个从游戏场景中销毁后生成。请帮助我,我是 unity 以及 c# 和 Whosebug 的新手。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject[] spawnableItems;
// Update is called once per frame
void Update()
{
bool isTimeToSpawn (GameObject itemGameObject)
{
spawnableSprites item = itemGameObject.GetComponent<spawnableSprites>();
float spawnDelay = item.seenEverySeconds;
float spawnsPerSecond = 1 / spawnDelay;
if(Time.deltaTime > spawnDelay)
{
Debug.LogWarning("SpawnRate Capped By FrameRate");
}
float threshold = spawnsPerSecond * Time.deltaTime;
if(Random.value < threshold)
{
return true;
}
else
{
return false;
}
}
foreach (GameObject item in spawnableItems)
{
if(isTimeToSpawn(item))
{
Spawn(item);
}
}
}
void Spawn (GameObject myGameObject)
{
GameObject myItem = Instantiate(myGameObject) as GameObject;
myItem.transform.parent = transform;
myItem.transform.position = transform.position;
}
}
void Start()
{
// will wait 2 seconds then call the function Spawn() every 0.3 seconds
InvokeRepeating("Spawn", 2.0f, 0.3f);
}
void Spawn()
{
GameObject myItem = Instantiate(myGameObject) as GameObject;
myItem.transform.parent = transform;
myItem.transform.position = new Vector2(Random.Range(0, width) , height);
}
您想在顶部随机生成一个对象,然后仅在第一个对象被销毁后才生成另一个对象?正确的? 看起来您不需要 spawn/delete 多个对象。 你知道在游戏开发中,我们会尽量提高效率,有时你需要的只是将旧对象用作新对象,然后欺骗玩家认为它是新的。 所以我的建议是不要删除你的对象,只需再次将它的位置设置在顶部,就像这样:
float spawnXmin = 0;
float spawnXmax = 100;
float spawnY = 0;
float spawnZ = 0; // * numbers are just for demonstration
myItem.transform.position = new Vector3(Random.Range(spawnXmin, spawnXmax), spawnY, spawnZ);
也不要在 Update() 中使用这些代码,任何你在 Update 中放入的东西都会花费你很多,因为它会在每一帧中 运行,所以尽可能避免在 Update() 中放入东西可能。