C# 停止允许random.range拾取同一个点。它使敌人相互重叠
你好,我有一个敌人的产卵系统,工作正常。但是敌人在同一点上重叠,因为我使用random.range,我在地图上有4个点,我希望每个敌人随机选择一个点。因此,我希望在繁殖敌人后,其他敌人只能获得3个繁殖选项,而不是4个 这是我的密码:C# 停止允许random.range拾取同一个点。它使敌人相互重叠,c#,unity3d,unity5,C#,Unity3d,Unity5,你好,我有一个敌人的产卵系统,工作正常。但是敌人在同一点上重叠,因为我使用random.range,我在地图上有4个点,我希望每个敌人随机选择一个点。因此,我希望在繁殖敌人后,其他敌人只能获得3个繁殖选项,而不是4个 这是我的密码: using UnityEngine; using System.Collections; using System.Collections.Generic; public class Spawn : MonoBehaviour { // The enem
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Spawn : MonoBehaviour {
// The enemy prefab to be spawned.
public GameObject[] enemy;
//public Transform[] spawnPoints;
public List<Transform> spawnPoints = new List<Transform>();
private float timer = 3;
int index = 0;
List <GameObject> EnemiesList = new List<GameObject>();
private int m_enemyCount = 4;
// Update is called once per frame
void Update () {
if (timer >0)
{
timer -= Time.deltaTime;
}
if (timer <= 0 )
{
if ( EnemiesList.Count == 0 )
{
Spawner();
timer = 5;
}
}
}
void Spawner ()
{
// Create an instance of the enemy prefab at the randomly selected spawn point's position.
//Create the enemies at a random transform
for (int i = 0; i<m_enemyCount;i++)
{
int spawnPointIndex = Random.Range (0, spawnPoints.Count);
Transform pos = spawnPoints[spawnPointIndex];
GameObject InstanceEnemies= Instantiate ( enemy[index] , spawnPoints[spawnPointIndex].position , Quaternion.identity) as GameObject;
// Create enemies and add them to our list.
EnemiesList.Add(InstanceEnemies);
}
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
公共类繁殖:单行为{
//要繁殖的敌人预制场。
公开游戏对象[]敌人;
//公共转换点;
公共列表点=新列表();
专用浮点计时器=3;
int指数=0;
List EnemiesList=新列表();
私有整数m_enemyCount=4;
//每帧调用一次更新
无效更新(){
如果(计时器>0)
{
timer-=Time.deltaTime;
}
如果(计时器从for循环末尾的列表spawnpoints
中删除已获取的繁殖点,如下所示:
spawnPoints.RemoveAt(spawnPointIndex);
您可能希望在for循环之前创建该列表的副本,以保持原始状态。处理此问题的简单方法是在Spawner()
方法中创建一个本地生成点列表,以便跟踪已使用的生成点。例如:
void Spawner ()
{
//create a local list of spawn points
List<Transform> availablePoints = new List<Transform>(spawnPoints);
// Create an instance of the enemy prefab at the randomly selected spawn point's position.
//Create the enemies at a random transform
for (int i = 0; i<m_enemyCount;i++)
{
//use local availableSpawnPoints instead of your global spawnPoints to generate spawn index
int spawnPointIndex = Random.Range (0, availableSpawnPoints.Count);
Transform pos = spawnPoints[spawnPointIndex];
GameObject InstanceEnemies= Instantiate ( enemy[index] , avaialableSpawnPoints[spawnPointIndex].position , Quaternion.identity) as GameObject;
// Create enemies and add them to our list.
EnemiesList.Add(InstanceEnemies);
//remove the used spawnpoint
availableSpawnPoints.RemoveAt(spawnPointIndex);
}
}
void生成程序()
{
//创建繁殖点的本地列表
列表可用点=新列表(生成点);
//在随机选择的繁殖点的位置创建一个敌人预制的实例。
//以随机变换创建敌人
对于(int i=0;ioh,但是现在当我的敌人死亡时,我得到了这个错误参数超出范围。呜呜,我的错误!查看我的更新答案。我忘记在产卵器()中新建一个列表方法,而不是直接复制繁殖点。这应该可以通过添加更新来解决您的问题。敌人再次重叠在彼此的顶部。D:看起来实例化使用的是全局繁殖点,而不是可用的繁殖点。我已经更新了答案,希望是最后一次!:D更正:)u rock:D