Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hexer · Jul 20, 2013 at 09:08 AM · javascriptprefabmap

How to make a 40x40 map out of cubes, using script

I want to make a map out of single cubes (not just one big cube where i change the scale) This is because i want to make an interactive map.

Someone told me i could do this with script and prefabs to make the map.

Now my question is, how do you do this? I can make the prefabs my own, but i'm for sure not experienced enough to make my own script, although i'm still learning and by every script i examine, it gets me further in understanding the scripts.

NOTE : Im using Javascript.

I used this script first but the problem with this one is that it will just generate random objects in a certain Min and Max coordinate i had given. this brings me problems like : holes in the floor and several objects that are standing in the same coordinates

 var myCube : GameObject;
  
 function Start()
 {   
 // Let's store some minimum and maximum possible x,y and z values
      // to position our cubes:
      var minX : int = -20;
      var maxX : int = 20;
      var minY : int = 1;
      var maxY : int = 1;
      var minZ : int = -20;
      var maxZ : int = 20;
      // (you can fiddle with these numbers to change the range of possible spawn positions)
  
      var totalCubes : int = 500; // Change this to whatever number of cubes you'd like to have on-screen
  
      var rot : Quaternion = Quaternion.identity;
  
      for(i=0; i<totalCubes; i++)
      {
           // Use Random.Range to grab randomized x,y and z values within our min/max ranges:
           var randomX : int = Random.Range(minX, maxX);
           var randomY : int = Random.Range(minY, maxY);
           var randomZ : int = Random.Range(minZ, maxZ);
  
           var pos : Vector3 = new Vector3(randomX, randomY, randomZ);
  
           Instantiate(myCube, pos, rot); // Put a new cube on the screen using a randomized position   
      }
 
 }
  
 function Update()
 {
 }

Then i used this script but it would just take to long to set all the blocks, and because i wanted to make a map out of 40x40 cubes that means i had to set 1600 blocks manually.

 var myCube : GameObject;
  
 function Start()
 {
      // Store a bunch of different positions in an Array:
      var aPositions : Array = [new Vector3(0,1,0),
                                       new Vector3(1,1,0),
                                       new Vector3(2,1,0),
                                       new Vector3(3,1,0),
                                       new Vector3(4,1,0)];
  
      var rot : Quaternion = Quaternion.identity;
  
      // Loop as many times as there are elements in the aPositions Array:
      for(i=0; i<aPositions.length; i++)
      {
           Instantiate(myCube, aPositions[i], rot); // Put a new cube on the screen using the i'th position   
      }
 }

I can use every kind of help :) - Hexer.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by AlucardJay · Jul 20, 2013 at 11:36 AM

You almost had the right idea in the first script. Let's look at the requirements :

  • a 2D grid of cubes

  • every position in the grid must have a cube

  • the height is different for each position in the grid

The way would be to loop through every x position, and within that every z position. Please read all the comments in my example script :

 var myCube : GameObject;
 
 var minX : int = -20;
 var maxX : int = 20;
 var minY : int = 0;
 var maxY : int = 4;
 var minZ : int = -20;
 var maxZ : int = 20;
 
 function Start()
 {
     // loop for every z position in the grid
     for ( var z : int = minZ; z < maxZ; z ++ )
     {
         // now loop for every x position in the grid
         for ( var x : int = minX; x < maxX; x ++ )
         {
             // get a random value for Y
             var randomY : int = Random.Range( minY, maxY );
             
             // put it all to together to assign a position
             var pos : Vector3 = new Vector3( x, randomY, z );
             
             // instantiate the cube into a variable, so you can do other things with it
             var clone : GameObject = Instantiate( myCube, pos, Quaternion.identity );
             
             // change the name of the object, include the x and z position in the name
             clone.name = "Cube_" + x.ToString() + "_" + z.ToString();
             
             // in future, 
             // this would be stored in a 2D array for future reference
             // so you could modify the position, material, anything!
             // cubeArray[x,z] = clone;
         }
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hexer · Jul 20, 2013 at 01:21 PM 0
Share

Thanks, but can i just replace.

 // get a random value for Y
              var randomY : int = Random.Range( $$anonymous$$Y, maxY );
      
              // put it all to together to assign a position
              var pos : Vector3 = new Vector3( x, randomY, z );

with

  // loop for every y position in the grid
              for ( var y : int = $$anonymous$$Y; y < maxY; y ++ )
      
              // put it all to together to assign a position
              var pos : Vector3 = new Vector3( x, y, z );

and change the $$anonymous$$Y and maxY to both 1, to get one layer ins$$anonymous$$d of two? I tried it out and it seems to work, but when i add gravity to my cube and click on play it seems to lagg very badly.

(I took my prefabs(cube) that was connected with the script and added a rigedbody by clicking on Component>Physics>Rigedbody)

In the future i want to make a cube fall when interacting with a player. (example : If a player walks over the cube, the cube starts to fall down. which affect just that one cube and not the cubes around it, unless the player walks of those cubes too.)

I can imagine it would lagg alot, or is this just a bug in unity that won't appear in the final stage?

avatar image Hexer · Jul 20, 2013 at 09:40 PM 0
Share

Also how can i change the Y coordinate position of the cube? it doesn't seems to work when i change the $$anonymous$$Y and maxY to both 1.

i'm just new in scripting so i have no idea what a 2D array is :) do you got a link or an example so that i could understand what it is? Is is something that i have to implement in the script? and if so, where do i need to put it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Instantiate prefab within parameters?(Javascript) 1 Answer

I want to turn my flashlight on and off, while still maintaining the flicker effects. 3 Answers

Unity fire1 prefab in code 2 Answers

Too many bugs 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges