Stacking Lego pieces

Hey guys, Im making a weekend proejct. It will be a puzzle game, based on grid placing blocks. I can place them, but there is a slight problem with stacking. I need to check if the lego piece should or shouldnt go higher – aka stack like a tower.

Lego.js

var totalDist : float;
var actualBlock : Transform;
var blockIsPlaced : boolean = true;
var block : Transform; //one lego piece
var x : float = 0;
var y : float = 0;

function Update (){   
       CheckForPlacing();
       GetInput();
      

if(!blockIsPlaced){
               actualBlock.position.x = x - 2.5 * totalDist;
               actualBlock.position.y = y - 1.5 * totalDist;}}

function CheckForPlacing () {
 if(blockIsPlaced)
        {            
               actualBlock = Instantiate(block, Vector3(
                                         transform.position.x,
                                         transform.position.y,
                                         transform.position.z - 0.1
                                         ),transform.rotation);
               actualBlock.parent = transform;
               blockIsPlaced = false;}}

function GetInput (){
        if(Input.GetKeyDown(KeyCode.RightArrow)){
               if(x != 5){
                       x += 1;}}  
    if(Input.GetKeyDown(KeyCode.LeftArrow)){
               if(x != 0){
                       x -= 1;}}
      if(Input.GetKeyDown(KeyCode.UpArrow)){
               if(y != 5){
                       y += 1;}}     
     if(Input.GetKeyDown(KeyCode.DownArrow)){
               if(y != 0){
                       y -= 1;}}
     
     if(Input.GetButtonDown("Jump")){
               blockIsPlaced = true;}}

– David

PS: I hope that you will understand. :slight_smile:

It sounds like you need to keep a list of occupied locations in the world. When you go to place a block you can check over the list to see if the position is occupied and choose the position above (which you’ll also have to check to see if it is occupied.)

Get that working and you can look into using a 3D array to optimize it.

Ok I have it, just wanna close this thread.