Spawn Objects at Random Position in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello everyone. In this video we'll learn  how to make objects spawn at random positions   inside our Unity scene. At the end of the  video we will have made something like this,   each time the space key is pressed a new  cube is created at a random position. Okay, let's begin I have a simple scene setup  with a ground plane and a cube object. I'll   select the cube object and i'll add a RigidBody  to it. I will drag and drop the cube object into   the desktop folder so we can create a prefab out  of it. I'll delete the cube in the scene view.   Okay, right click and i'll create a new Empty  GameObject. I will call this object Spawner.   i'll just reset its position to zero zero zero.   Okay, inside the Asset Folder i'll  right click and i'll create a new   C# Script. I'll call the C# Script RandomSpawner.  Double click to open it in Visual Studio. Inside Visual Studio first of all I will delete  the Start Method because we won't be using it.   I'll create a new variable public GameObject  called cubePrefab. Now this variable will   keep reference to the cube prefab we have made  inside Unity and inside the Update Uethod first   of all i will check if the space key has been  pressed so if (Input.GetKeyDown(KeyCode.Space)) Then first of all we'll create a random  spawn position so I'll create a variable   to hold that position Vector3 randomSpawnPosition equals new Vector3 now inside our new Vector3  I will pick a random value for the x axis,   so we're going to use the Random.Range function. from minus 10 to 11. So we're gonna  have an x value from -10 to then   why i wrote 11 is because if you hover  over the Random.Range function you're   gonna see that the first number it takes  is inclusive and the second is exclusive   so basically that means the minus 10 number can  be created but the 11 won't be created. Maximum   number it will be 10 I hope that makes sense. For  the y-axis i'm gonna just type 5(five) because we   want our cube to be spawned above our ground  plane and I will do the same for the z axis so Random.Range from minus 10 to 11. Now that  we have created the randomSpawnPosition   we just gonna Instantiate a new object.  Now instantiate, the first parameter it   takes is the object we want to instantiate  or to create that will be our cubePrefab   the second argument it takes is the position  we have a randomSpawnPosition and the third   argument it takes is the rotation we're gonna type  Quaternion.Identity. This basically means that   there will be no rotation. So we finished that if  we save the script and go back into UNITY EDITOR   we're gonna wait few seconds for the script to  load. Now inside our scene view we're gonna select   the Spawner and i'll drag and drop the script onto  it. Inside the cubePrefab slot i'm gonna drag and   drop the cubePrefab. And now if we go into Play  Mode and if I press Space a new cube is created,   each time i press space a new cube is  created at a random position in our scene. And that's it! In the next video we'll learn how  to spawn random objects at random locations in   Unity. Thank you for watching and  see you in the next one. Bye bye!
Info
Channel: Unity Ace
Views: 47,801
Rating: undefined out of 5
Keywords: unity, unity tutorial, unity3d, unity spawning objects, unity spawn object at random position 3d, unity spawn object at random position, unity spawn at random position, random position spawn in unity, how to spawn object in unity, how to spawn object at random position in unity, how to spawn object at random position in unity 3d, spawning objects in unity, how to instantiate object at random position, instantiate prefab unity, unity spawn, how to spawn in unity, unity spawner
Id: IbiwNnOv5So
Channel Id: undefined
Length: 4min 28sec (268 seconds)
Published: Wed Jan 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.