Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Irsan · Jul 01, 2011 at 06:18 AM · gridcubesnap

Lego-building System.

I am making a Lego-type building system. The hardest part is getting a tempbrick (outline of a brick that hasn't been placed yet) to align to one of the gridspaces on a brick below it. The brick can have virtually any set of dimensions, and be rotated at any angle. This system needs to work with every brick in an environment, so storing even one script on each is out of the question, since I'd have for instance, 1,000+ bricks with the same script on each of them.

On the brick itself, this script retrieves the x-size of the brick (which is what I want to avoid, as I've said):

 static var brickScaleX : float;
 
 function Start () {
     
     brickScaleX = transform.localScale.x / 2;
 }

Using this information, I can align the tempbrick to one of the squares on the brick, but only the x-position. (The Y and Z positions are just filler; they're both .1)

BEFORE

alt text

AFTER

alt text

This is the script that does this:

 var brickScaleXSame : float;
 var tbPositionX : float;
 var tbNewPositionX : float;
 
 function Start () {
 
     brickScaleXSame = (Mathf.Round(GetScale.brickScaleX));
     tbPositionX = transform.localPosition.x * (GetScale.brickScaleX * 2);
     tbNewPositionX = (Mathf.RoundToInt(tbPositionX));
     
     transform.localPosition = Vector3((tbNewPositionX / (GetScale.brickScaleX * 2)), .1, .1);
 
     if (Mathf.Approximately(1.0, (GetScale.brickScaleX)/(brickScaleXSame)))
         print ("X is even.");
             else
         print ("X is odd.");
 }

So far it only does the X position, because trying it with the other axes doesn't seem to work. I needed to determine if the brick's x-size is even or odd in-case the tempbrick had an even x-size. GADDRHGGGGGGGGGGGGGGGHDGHJDHD

What I'm doing with the above script is this:

Get position of the tempbrick relative to the brick. round the x-position of the tempbrick to the nearest tenth. Then I have to compensate for the fact that the tempbrick is a child of the brick, so the relative position gets mucked up. I compensated by multiplying the x-size of the brick by 2, and then dividing the position of the tempbrick relative to the brick by the new brick x-size.

I'm at the end of my wits here. I've been trying to come up with this system for half a year. Is there a way to create a grid that's more practical?

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
0

Answer by DavidDebnar · Jul 01, 2011 at 07:31 AM

Hmmm....I think that the easiest way would be something like this. Brick spawns at one of the grids columns, or you can adjust it, than when you move your mouse right, the brick moves to the right by the totalDist float, and snaps there.

var totalDist : float; //total distance block have to move var actualBlock : Transform;

var blockIsPlaced : boolean = true;

var block : Transform; //one lego piece var old : Transform;

function Start () { var startBlock = Instantiate(block, Vector3(500,500,500), transform.rotation); //there has to be at least SOME child startBlock.parent = transform; }

function Update () { if(blockIsPlaced) { old = actualBlock;

     if(old != null)
         actualBlock = Instantiate(block, old.position, transform.rotation);
     else
         actualBlock = Instantiate(block, transform.position, transform.rotation);
     actualBlock.parent = transform;
     actualBlock.renderer.material.color = Color(Random.Range(0.0,1.0),Random.Range(0.0,1.0), Random.Range(0.0,1.0));
     blockIsPlaced = false;
 }

  if(Input.GetKeyDown(KeyCode.RightArrow)) 
  {
     actualBlock.transform.position.x += totalDist; 
     print("moving right");
 }
 
  if(Input.GetKeyDown(KeyCode.LeftArrow)) 
  {
     actualBlock.transform.position.x -= totalDist;
 }
 
  if(Input.GetKeyDown(KeyCode.UpArrow)) 
  {
     actualBlock.transform.position.y += totalDist;
 }
 
  if(Input.GetKeyDown(KeyCode.DownArrow)) 
  {
     actualBlock.transform.position.y -= totalDist;
 }
 for(var child : Transform in transform)
     if(child != null)
         if(child.transform.position.x == actualBlock.transform.position.x && child.transform.position.y == actualBlock.transform.position.y)actualBlock.transform.position.z = child.transform.position.z - 0.07;
             else {actualBlock.transform.position.z = transform.position.z;}

  if(Input.GetButtonDown("Jump"))
  {
      blockIsPlaced = true;
  }

}

Move block with arrow keys, place with Spacebar && put this script on grid, and set the total dist to gridPlane.x / grid.xColumns :-)

  • David

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 DavidDebnar · Jul 01, 2011 at 07:55 AM 0
Share

This one works a way better :-) //upper one is the same from now

  • $$anonymous$$

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Check children on a grid 1 Answer

Snap blocks that aren't cubes. 1 Answer

Is there a toggle for snap to grid? 10 Answers

What is wrong with my code? 1 Answer

Snap object to grid 1 Answer


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