如何在20个随机位置列表的3个位置实例化3个游戏对象

时间:2017-02-02 08:53:12

标签: c# unity3d unity5

我想在20个随机位置列表的3个位置实例化3个游戏对象。当我玩游戏时,它会在20个位置实例化20个游戏对象,而不是3个游戏对象。我该怎么做?这是我的代码:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SpawnManager: MonoBehaviour {

private GameObject Player;

public List<GameObject> spawnPositions;
public List<GameObject> spawnObjects;

void Start()
{
    Player = GameObject.FindGameObjectWithTag("Player");
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Player")
    {
        SpawnObjects ();
    }
}

void SpawnObjects()
{
    foreach (GameObject spawnPosition in spawnPositions) 
    {
        int selection = Random.Range (0, spawnObjects.Count);
        Instantiate (spawnObjects [selection], spawnPosition.transform.position, spawnPosition.transform.rotation);
    }
}
}

更新:

有时候2个对象放在同一个位置,我希望对象在不同的​​位置实例化。我试图将随机位置添加到列表中,并且只实例化它是否已经在列表中,但它不起作用。这是我的代码:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SpawnManager : MonoBehaviour {

private GameObject Player;

public List<GameObject> spawnPositions;
public List<GameObject> spawnObjects;

private GameObject obj;

void Start()
{
    Player = GameObject.FindGameObjectWithTag("Player");
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Player")
    {
        SpawnObjects ();
    }
}

void SpawnObjects()
{

    for (int i = 0; i < 3; ++i)
    {
        int randomObject = Random.Range(0, spawnObjects.Count);
        int randomPosition = Random.Range(0, spawnPositions.Count);

        List <GameObject> _spawnPositions = new List<GameObject>();

        obj = spawnPositions[randomPosition];

        _spawnPositions.Add(obj);

        if (!_spawnPositions.Contains (obj)) 
        {
            Instantiate (spawnObjects [randomObject], _spawnPositions [randomPosition].transform.position, _spawnPositions [randomPosition].transform.rotation);
        } 

        else 
        {
            Debug.Log ("error");
        }

    }
}

}

1 个答案:

答案 0 :(得分:3)

您循环遍历所有位置并向其添加随机对象,您还需要将其随机化。所以这个:

foreach (GameObject spawnPosition in spawnPositions) 
{
    int selection = Random.Range (0, spawnObjects.Count);
    Instantiate (spawnObjects [selection], spawnPosition.transform.position, spawnPosition.transform.rotation);
}

应该是这样的:

int randomObject = Random.Range(0, spawnPositions.Count);
int randomPosition = Random.Range(0, spawnPositions.Count);

Instantiate (spawnObjects[randomObject], spawnPositions[randomPosition].transform.position, spawnPositions[randomPosition].transform.rotation);

然后把它放在for循环中,如:

for (int i = 0; i < 3; ++i)

在您更新的代码中,您在每个循环上创建列表,您需要使用相同的列表(未经测试但应该有效):

List<int> randomObjects = new List<int>();
List<int> randomPositions = new List<int>();
for (int i = 0; i < 3; ++i)
{
    int randomObject;
    do
    {
        randomObject = Random.Range(0, spawnObjects.Count);
    }
    while (randomObjects.Contains(randomObject));
    randomObjects.Add(randomObject);

    int randomPosition;
    do
    {
        randomPosition = Random.Range(0, spawnPositions.Count);
    }
    while (randomPositions.Contains(randomPosition));
    randomPositions.Add(randomPosition);

    Instantiate(spawnObjects[randomObject], spawnPositions[randomPosition].transform.position, spawnPositions[randomPosition].transform.rotation);
}