In this video of the Spawning Series we will learn How to Spawn Random Objects in Unity. We will use an array to store three GameObjects and in play mode we will pick a Random Object to be Spawned by pressing the Space key. Now please note that I use the Space key to Spawn Random Objects for demonstration purposes. You can Spawn them using a timer or trigger etc. If this video helped you please leave a like and Subscribe so I can make more of them. Cheers!
CODE USED IN THE VIDEO:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomObjectSpawner : MonoBehaviour
{
public GameObject[] myObjects;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
int randomIndex = Random.Range(0, myObjects.Length);
Vector3 randomSpawnPosition = new Vector3(Random.Range(-10, 11), 5, Random.Range(-10, 11));
Instantiate(myObjects[randomIndex], randomSpawnPosition, Quaternion.identity);
}
}
}
#SpawnRandomObject #UnitySpawnRandom #UnityRandomSpawning
Sur cette page du site, vous pouvez voir la vidéo en ligne How to Spawn Random Objects in Unity durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Unity Ace 25 janvier 2022, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 26,704 fois et il a aimé 398 téléspectateurs. Bon visionnage!