{"id":3215,"date":"2014-08-31T00:30:07","date_gmt":"2014-08-30T21:30:07","guid":{"rendered":"http:\/\/unitycoder.com\/blog\/?p=3215"},"modified":"2014-09-04T13:36:33","modified_gmt":"2014-09-04T10:36:33","slug":"beginner-instantiate-objects-at-random-positions","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2014\/08\/31\/beginner-instantiate-objects-at-random-positions\/","title":{"rendered":"[Beginner] Instantiate objects at random positions"},"content":{"rendered":"<p>**This is our first guest post, simple beginners tutorial for spawning (instantiating) objects at random positions:<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>Hello, my name is Liviu Globa i\u2019m an indie developer, making games for about 4 years in Unity. I would like to show you some tips that will make your game better and more interesting.<\/p>\n<p>Today I\u2019ll speak about how to make your game more unpredictable using Unity class Random.<\/p>\n<p>First let\u2019s find pros in random things in a game :<br \/>\n-Randomization gives to your game infinite playability<br \/>\n-Things don\u2019t look mechanic<br \/>\n-Randomization is also used in AI, maps procedural generation etc.<\/p>\n<p><strong>How to program a random position in 3d space(C#)<\/strong><\/p>\n<p>In this mini Tutorial you will learn how to use Random class in C#, so let\u2019s start with essentials for example let\u2019s place some cubes in random positions into space.<\/p>\n<p>1.Just Create a new empty project, name it how you want to. File-&gt;New Project.<br \/>\n2.Create a cube. GameObject-&gt;Create Other-&gt;Cube.<br \/>\n-by drag\u2019n\u2019droping the cube into Assets folder we will have a prefab of this cube.After that delete the cube.<br \/>\nSo this is our experimental cube:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss1.jpg\" target=\"_blank\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3219\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2014\/08\/31\/beginner-instantiate-objects-at-random-positions\/random_tutorial_ss1\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss1.jpg?fit=998%2C560&amp;ssl=1\" data-orig-size=\"998,560\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"random_tutorial_ss1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss1.jpg?fit=998%2C560&amp;ssl=1\" class=\"alignnone wp-image-3219 size-medium\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss1-300x168.jpg?resize=300%2C168\" alt=\"random_tutorial_ss1\" width=\"300\" height=\"168\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss1.jpg?resize=300%2C168&amp;ssl=1 300w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss1.jpg?w=998&amp;ssl=1 998w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>(click image for full size)<br \/>\n3.Create C# script and name it Generator.<br \/>\n4.Drag\u2019n\u2019Drop Generator script on the Main Camera object.<br \/>\n5.Now open Generator script and edit it like i did:<\/p>\n<p>&#8211; we will need a public variable of type GameObject for the prefab of the cube.<br \/>\n&#8211; two variables of type int to define minimal and maximum allowed distances.<br \/>\n&#8211; and one variable to define how much cubes we want to place in the space.<\/p>\n<p>Your script should have this:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss2.jpg\" target=\"_blank\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3218\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2014\/08\/31\/beginner-instantiate-objects-at-random-positions\/random_tutorial_ss2\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss2.jpg?fit=996%2C543&amp;ssl=1\" data-orig-size=\"996,543\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"random_tutorial_ss2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss2.jpg?fit=996%2C543&amp;ssl=1\" class=\"alignnone wp-image-3218 size-medium\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss2-300x163.jpg?resize=300%2C163\" alt=\"random_tutorial_ss2\" width=\"300\" height=\"163\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss2.jpg?resize=300%2C163&amp;ssl=1 300w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss2.jpg?w=996&amp;ssl=1 996w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>(click image for full size)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\nVector3 GeneratedPosition()\r\n{\r\n int x,y,z;\r\n x = Random.Range(min,max);\r\n y = Random.Range(min,max);\r\nz = Random.Range(min,max);\r\n return new Vector3(x,y,z);\r\n}\r\n\r\n<\/pre>\n<p>This is the core of lesson, this function generates every time a new vector.Inside this function we can see that there are 3 variables of type int x,y and z and they are generated by itself individually using next method Random.Range(min,max); where min &lt;= random value &gt; max.<br \/>\n*Note: So if min = 0 and max = 100 random value can be {0,1,2,3,4\u2026\u2026..97,98,99}.! Without 100.<\/p>\n<p>&nbsp;<\/p>\n<p>Next step is to place the cubes, we will use for this a for loop and the function Instantiate for placing game objects into the scene:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvoid PlaceCubes()\r\n{\r\n\u00a0 for(int i = 0; i &lt; numberOfCubes; i++)\r\n\u00a0 {\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 Instantiate(cube, GeneratedPosition(), Quaternion.identity);\r\n\u00a0 }\r\n}\r\n\r\n<\/pre>\n<p>So the function PlaceCubes() will place a number of cubes equal with our variable numberOfCubes.<\/p>\n<p>How we can see in Instantiate function we need to put next parameters:<br \/>\n-the object of type GameObject;<br \/>\n-the position of type Vector3;<br \/>\n-the rotation of type Quaternion;<br \/>\nGeneral form : Instantiate(GameObject gameObject,Vector3 position, Quaternion rotation);<br \/>\n6.Now save script by pressing Ctrl + S and got o the Unity Editor.<br \/>\n7.Click on the Main Camera object in the Hierarchy (if you forgot to assign the script to the camera do it!)<br \/>\n8. Put any values in the Inspector, something like this:<br \/>\n<a href=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss4.jpg\" target=\"_blank\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3216\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2014\/08\/31\/beginner-instantiate-objects-at-random-positions\/random_tutorial_ss4\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss4.jpg?fit=1060%2C595&amp;ssl=1\" data-orig-size=\"1060,595\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"random_tutorial_ss4\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss4.jpg?fit=1024%2C574&amp;ssl=1\" class=\"alignnone size-medium wp-image-3216\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss4-300x168.jpg?resize=300%2C168\" alt=\"random_tutorial_ss4\" width=\"300\" height=\"168\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss4.jpg?resize=300%2C168&amp;ssl=1 300w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss4.jpg?resize=1024%2C574&amp;ssl=1 1024w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss4.jpg?w=1060&amp;ssl=1 1060w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>(click image for full size)<br \/>\n9.Click Play and you will see this (depends on your values)<br \/>\n<a href=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss3.jpg\" target=\"_blank\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3217\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2014\/08\/31\/beginner-instantiate-objects-at-random-positions\/random_tutorial_ss3\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss3.jpg?fit=994%2C558&amp;ssl=1\" data-orig-size=\"994,558\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"random_tutorial_ss3\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss3.jpg?fit=994%2C558&amp;ssl=1\" class=\"alignnone wp-image-3217 size-medium\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss3-300x168.jpg?resize=300%2C168\" alt=\"random_tutorial_ss3\" width=\"300\" height=\"168\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss3.jpg?resize=300%2C168&amp;ssl=1 300w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss3.jpg?w=994&amp;ssl=1 994w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>(click image for full size)<br \/>\nEvery time you will see something different.This is the fun of randomization that Unity and scripting can ofer to your game.<br \/>\nRandom Class can offer you more possibilities then just randomizating an integer,for example you there are functions that will help you to get a random point inside a circle, inside a sphere etc. if you need to visit great unity documentation:<br \/>\n<a title=\"http:\/\/docs.unity3d.com\/ScriptReference\/Random.html\" href=\"http:\/\/docs.unity3d.com\/ScriptReference\/Random.html\" target=\"_blank\">http:\/\/docs.unity3d.com\/ScriptReference\/Random.html<\/a><\/p>\n<p><strong>Full source code download:<\/strong><br \/>\n<a title=\"https:\/\/dl.dropboxusercontent.com\/u\/94477658\/Tutorial_1_Random_by_Liviu%20Globa\/Generator.cs\" href=\"https:\/\/dl.dropboxusercontent.com\/u\/94477658\/Tutorial_1_Random_by_Liviu%20Globa\/Generator.cs\" target=\"_blank\"> https:\/\/dl.dropboxusercontent.com\/u\/94477658\/Tutorial_1_Random_by_Liviu%20Globa\/Generator.cs<\/a><\/p>\n<p>You can contact me on mail : globaliviu(at)mail.ru<\/p>\n<p>Also you can follow some of my projects on facebook: <a title=\"https:\/\/www.facebook.com\/creatix.entertainment.official\" href=\"https:\/\/www.facebook.com\/creatix.entertainment.official\" target=\"_blank\">https:\/\/www.facebook.com\/creatix.entertainment.official<\/a><\/p>\n<p>I would like to help you out.<br \/>\nThank you for reading,<br \/>\nGloba Liviu<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>**This is our first guest post, simple beginners tutorial for spawning (instantiating) objects at random positions: &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- Hello, my name is Liviu Globa i\u2019m an indie developer, making games for about 4 years in Unity. I would like to show you some tips that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3217,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[420,3],"tags":[653,108,264,383],"class_list":["post-3215","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","category-unity3d","tag-guest","tag-instantiate","tag-random","tag-tutorial"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/08\/random_tutorial_ss3.jpg?fit=994%2C558&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-PR","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/3215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/comments?post=3215"}],"version-history":[{"count":6,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/3215\/revisions"}],"predecessor-version":[{"id":3225,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/3215\/revisions\/3225"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/3217"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=3215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=3215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=3215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}