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
AFTER
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?