Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by bionicnacho · Jan 11, 2013 at 08:09 PM · freezetranslateslowgrid based gamey axis

Placing objects on a grid

EDIT: I found out how to do this much faster with Lovrenc's version of placing the objects on a grid. So, if you want an efficient way on how to place objects on a grid, look at his answer below!

Hello!

I have a grid in my project that has about 4000 single cubes in it. I have a script that lets the user, when they press "Q" or "E", move the grid UP on the Y axis. This is my movegrid script:

 //current grid level
 static var gridLevel : int = 0;
 var grid : GameObject;
 var designgui : GameObject;
 var maxlevel : int = 20;
 var minlevel : int = 0;
 private var camlev : int = 22;
 
 function Start()
 {
     gridLevel = 0;
 }
 function Update () {
 
     if( gameMode.gameMode == "design" ){
         if(Builder.gridbuild){
             grid.SetActive(true);
     
             if (Input.GetKeyDown("e")){
             
                 if(gridLevel < maxlevel){
                     gridLevel += (1);
                     
                     grid.transform.position = Vector3(0,gridLevel,0);
                     Camera.main.transform.position.y = (camlev + gridLevel);
                 }
                 
             }
             
             if (Input.GetKeyDown("q")){
                 
                 if(gridLevel > minlevel){
                     gridLevel -= (1);
                     
                     grid.transform.position = Vector3(0,gridLevel,0);
                     Camera.main.transform.position.y = (camlev + gridLevel);
 
                 }
                 
             }
         
         }
     }
     
     if( gameMode.gameMode == "play" ){
         grid.SetActive(false);    }
         
     if(Builder.mcbuild){
         grid.SetActive(false);    
     }
 
 }

But it goes REALLY slow every time it goes up and also freezes the game for a bit while it is doing so.

Is there any way to make this go faster?

Comment
Add comment · Show 6
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 Lovrenc · Jan 11, 2013 at 08:14 PM 0
Share

How big is big and how many is many?

avatar image bionicnacho · Jan 11, 2013 at 08:18 PM 0
Share

About 3000+ cubes :S

avatar image jogo13 · Jan 11, 2013 at 08:45 PM 0
Share

Ok, cube count is probably the culprit!:)

avatar image bionicnacho · Jan 11, 2013 at 08:51 PM 0
Share

Well, how else would i be able to make a grid, and make instantiated objects snap to it? This is the only way i though of doing it...

I would have done it with a big plane with a tiled texture, but i dont know how i would be able to snap the placed items to the grid

avatar image Lovrenc · Jan 11, 2013 at 08:57 PM 0
Share

You need this grid just for snapping and you used 3000+ cubes? that is more than 36000 polygons and a lot of shifting.

Just use plane with a texture and calculate coordinates. All you need is your step (width/height).

Show more comments

2 Replies

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

Answer by Lovrenc · Jan 11, 2013 at 09:57 PM

Here is how i would do it.

Create a plane and set texture on it so it will represent your grid well (you said you want cell to be 1m x 1m). Then cast ray, and if it hits plane do this

alt text.

First you get your hit postition from raycast (raycastHit.point). Then

 plane.transform.position 

gives you pivot point of the plane. In unity this is in the middle of the plane (drawn in orange).



You want a vector from pivot to raycast hit.

 Vector3 pivotToPoint = raycastHit.point - plane.transform.position;



Now you have to calculate where to position your object:

 float tileZ = pivotToPoint.Z / cellWidth;



This kinda gives us our location on z axis, but not quite. It tells us (in float) how many tiles from pivot our ray stroke (in float). We have to round that walue. Look at the image. If ray stroke more than 1.5 tiles width away, we should in fact move 2 tiles in that direction. So:

 tileZ= Mathf.Round(tileZ);

You have to do the same for the X axis. Then we just have to calculate our world positions. We do this from pivot of the plane and our tile positions.

On the picture our pivot is inside the middle tile. This is important as otherwise we also have to calculate offsets.

Lets calculate final z position:

 float worldZ = plane.transform.position.Z + tileZ * tileWidth;



My longest post ever. Hopefully this is somewhat correct.


gridsnapping.png (38.4 kB)
Comment
Add comment · Show 9 · 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 bionicnacho · Jan 12, 2013 at 11:35 PM 0
Share

wow. It works! Thank you so much!

avatar image EvansGreen · Jan 12, 2013 at 11:39 PM 1
Share
  • for the drawing! :p!

avatar image Lovrenc · Jan 12, 2013 at 11:42 PM 0
Share

You can say i am not an artist aye? :)

Really glad to see this worked. I wasn't sure my logic is correct.

Cheers!

avatar image EvansGreen · Jan 12, 2013 at 11:45 PM 0
Share

That drawing is the essential proof that you have a future as a programmer! It's our curse, our burden, we shall SUC$$anonymous$$ at art! haha

Nice explanation mate, you showed interest in helping!

avatar image $$anonymous$$ · Jan 12, 2013 at 11:52 PM 0
Share
  • for the explanation and drawing.. this site needs many more answers+questions like this.

@bionicnacho You could edit the title of the question to something like "placing objects on a grid", so others can find it in the future more easily.

Show more comments
avatar image
0

Answer by jogo13 · Jan 11, 2013 at 08:21 PM

Create a 'grid manager' script that updates them all at once, using a for loop. In addition if you have a TON of duplicate gameobjects try to avoid having scripts attached to them that have a function Update() This is called automatically on each frame and can slow things down. You can give them a differently named cube_update() or something like that and have the 'grid manager' call it when updated the cubes.

 for(int i=0; i < cubes.Length; i++)
 {
   //handle key press movement
   cubes.cube_update();
 }
Comment
Add comment · Show 1 · 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 bionicnacho · Jan 11, 2013 at 08:29 PM 0
Share

well, the script is attatched to one single ScriptContainer gameObject and what it does is moves all of a gameobject's children (all the grid blocks) up or down... So i dont have the script on every single cube

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

12 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

Related Questions

Translate and Yield giving inaccurate results? 2 Answers

unity freezes repeatedly 1 Answer

Path finding freezes Unity 2 Answers

Asset store freezes up everything 1 Answer

Why does Unity freeze for long periods when highlighting some objects in the hierarchy? 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