【发布时间】:2017-11-29 23:40:04
【问题描述】:
您好,我有一个敌人生成系统,它工作正常。但是敌人在同一点重叠,因为我使用的是 random.range,我在地图上有 4 个点,我希望每个敌人随机选择一个点。因此,我希望在生成敌人后,其他敌人只有 3 个选项可以生成,而不是 4 个。
这是我的代码:
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);
}
}
【问题讨论】: