C# 如何在20个随机位置列表的3个位置实例化3个游戏对象
我想在20个随机位置的列表中的3个位置实例化3个游戏对象。当我玩游戏时,它会在20个位置实例化20个游戏对象,而不是3个游戏对象。我怎么做?这是我的代码:C# 如何在20个随机位置列表的3个位置实例化3个游戏对象,c#,unity3d,unity5,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<GameO
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);
}
}
}
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");
}
}
}
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
公共类管理器:MonoBehavior{
私人游戏对象玩家;
公开职位名单;
公共物品清单;
void Start()
{
Player=GameObject.FindGameObjectWithTag(“Player”);
}
无效对撞机(对撞机其他)
{
如果(other.gameObject.tag==“玩家”)
{
产卵对象();
}
}
void对象()
{
foreach(游戏对象在spawnPositions中的spawnPosition)
{
int selection=Random.Range(0,spawnObjects.Count);
实例化(spawnObjects[selection],spawnPosition.transform.position,spawnPosition.transform.rotation);
}
}
}
更新:
有时两个对象放在同一个位置,我希望这些对象在不同的位置实例化。我尝试将随机位置添加到列表中,并且仅在列表中没有随机位置时进行实例化,但不起作用。这是我的代码:
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);
}
}
}
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");
}
}
}
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
公共类管理器:MonoBehavior{
私人游戏对象玩家;
公开职位名单;
公共物品清单;
私有游戏对象obj;
void Start()
{
Player=GameObject.FindGameObjectWithTag(“Player”);
}
无效对撞机(对撞机其他)
{
如果(other.gameObject.tag==“玩家”)
{
产卵对象();
}
}
void对象()
{
对于(int i=0;i<3;++i)
{
int randomObject=Random.Range(0,spawnObjects.Count);
int randomPosition=Random.Range(0,spawnPositions.Count);
列表_=新列表();
obj=繁殖位置[随机位置];
_添加(obj);
如果(!\u.Contains(obj))
{
实例化(spawnObjects[randomObject],_spawnPositions[randomPosition].transform.position,_spawnPositions[randomPosition].transform.rotation);
}
其他的
{
Debug.Log(“错误”);
}
}
}
}
如果要在所有位置上循环并向其中添加随机对象,还需要将其随机化。因此:
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)
for(int i=0;i<3;++i)
在每个循环上创建列表的更新代码中,您需要使用相同的列表(未测试,但应该可以使用):
List randomObjects=new List();
列表位置=新列表();
对于(int i=0;i<3;++i)
{
int随机对象;
做
{
randomObject=Random.Range(0,spawnObjects.Count);
}
while(randomObjects.Contains(randomObject));
添加(随机对象);
int随机位置;
做
{
randomPosition=Random.Range(0,spawnPositions.Count);
}
while(randomPositions.Contains(randomPosition));
随机位置。添加(随机位置);
实例化(spawnObjects[randomObject],spawnPositions[randomPosition].transform.position,spawnPositions[randomPosition].transform.rotation);
}
如果要在所有位置上循环并向其中添加随机对象,还需要将其随机化。因此:
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)
for(int i=0;i<3;++i)
在每个循环上创建列表的更新代码中,您需要使用相同的列表(未测试,但应该可以使用):
List randomObjects=new List();
列表位置=新列表();
对于(int i=0;i<3;++i)
{
int随机对象;
做
{
randomObject=Random.Range(0,spawnObjects.Count);
}
while(randomObjects.Contains(randomObject));
添加(随机对象);
int随机位置;
做
{
randomPosition=Random.Range(0,spawnPositions.Count);
}
while(randomPositions.Contains(randomPosition));
随机位置。添加(随机位置);
实例化(spawnObjects[randomObject],spawnPositions[randomPosition].transform.position,spawnPositions[randomPosition].transform.rotation);
}
如果要在所有位置上循环并向其中添加随机对象,还需要对其进行随机操作。int rnd=/*random integer*/;foreach(spawnPositions.OrderBy(g=>rnd).Take(3)){/*你的实例化代码*/}
如果你在所有位置上循环,并向它们添加随机对象,你也需要随机化;foreach(varspawnpoint在spawnPositions.OrderBy(g=>rnd).Take(3)){/*您的实例化代码*/}