[英]Spawning random gameObjects into set positions
I'm new to coding and the answer is probably simple.我是编码新手,答案可能很简单。 I am making a pairs memory game in unity and I want the cards to spawn randomly each time the player loads the game.
我正在制作一个统一的配对记忆游戏,我希望每次玩家加载游戏时卡片都会随机产生。 I have a gameobject list of cards and a transform list of spawning positions that I want the cards to spawn in. So far I have managed to shuffle the cards into a random order.
我有一个卡片的游戏对象列表和我希望卡片生成的生成位置的转换列表。到目前为止,我已经设法将卡片洗牌为随机顺序。 But to do this I've converted the card gameobject list into integers.
但要做到这一点,我已将卡片游戏对象列表转换为整数。
public List<GameObject> cardsToSpawn;
public List<Transform> cardPositions = new List<Transform>();
public int cardCount;
public int someNum;
public List<GameObject> cards;
public GameObject empty;
void Awake()
{
// list of the card gameobjects.
List<int> cardsToSpawn = new List<int>();
cardsToSpawn.Add(1);
cardsToSpawn.Add(2);
cardsToSpawn.Add(3);
cardsToSpawn.Add(4);
//Randomises the order. A, B, C, D = C, D, B, A for example.
//cardsToSpawn is assigned a random number (1-18(length of cardsToSpawn list)).
for (int i = 0; i < cardsToSpawn.Count; i++)
{
int temp = cardsToSpawn[i];
int randomIndex = Random.Range(i, cardsToSpawn.Count);
cardsToSpawn[i] = cardsToSpawn[randomIndex];
cardsToSpawn[randomIndex] = temp;
//spawn card i=1 in cardPositions[1], card i=2 in cardPositions[2]...
temp = cards
Instantiate(cardsToSpawn[i], cardPositions[i].position, transform.rotation);
// but this refers back to the original list declared at the beginning of the script rather than the new randomIndex int.
}
// randomises the order. A, B, C, D = C, D, B, A for example.
//tell me the order (i.e. make sure it is randomised.)
cardsToSpawn.ForEach(i => Debug.Log("{0}\t"+ i));
}
In your for
loop you are generating a new Random.Range
... this does not exclude duplicates and on the other side also doesn't guarantee that all of the cards get placed.在您的
for
循环中,您正在生成一个新的Random.Range
......这不排除重复项,另一方面也不能保证所有卡片都被放置。
What you rather want to do is randomize one of the lists (of course you can randomize both but this wouldn't gain any more randomness actually) and then iterate through all elements for placing them.您更愿意做的是随机化列表中的一个(当然,您可以随机化两个列表,但这实际上不会获得更多随机性),然后遍历所有元素以放置它们。
For the shuffling there are multiple options.对于洗牌,有多种选择。 you can either use an extension method like this one or (because it is easier for me to explain for now) use Linq
OrderBy
as described here您可以使用扩展方法,像这一个或(因为它更容易为我解释现在)使用LINQ的
OrderBy
的描述在这里
Eg like例如喜欢
using System.Linq;
public List<GameObject> cardsToSpawn;
public List<Transform> cardPositions = new List<Transform>();
void Awake()
{
// First make sure both lists have the same length
if(cardsToSpawn.Count != cardPositions.Count)
{
Debug.LogError("List length are different! Make sure you have the same amount of elements in both lists!");
return;
}
// get the cards in randomized order
Random rnd = new Random();
// OrderBy returns an IEnumerable ordered by the given predicate
// since our given predicate returns a random value each time
// the elements get shuffled
var shuffledCards = cardsToSpawn.OrderBy(c => rnd.Next()).ToArray();
for (int i = 0; i < shuffledCards.Length; i++)
{
// since the cards are already shuffled you can simply iterate
// over this array in order now
var card = shuffledCards[i];
var position = cardPositions[i];
Instantiate(card, position, transform.rotation);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.