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?

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 :slight_smile:

– David