- Home /
The question is answered, right answer was accepted
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.
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. :-)
Answer by DavidDebnar · Jul 11, 2011 at 09:31 AM
Ok I have it, just wanna close this thread.
Hello! I've entirely forgotten about this question. I'll edit my answer in a couple hours with the correct answer and some example code.
The solution I used was to do a Raycast directed away from the camera, to the point where you want the next lego piece to be spawned.
Another solution would be to keep a hashset/dictionary of all the currently spawned lego pieces, where the key is a Tuple/$$anonymous$$eyValueStore. If a value isn't present, it means that position in the world is free.
Answer by flaviusxvii · Jul 06, 2011 at 04:36 PM
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.
Follow this Question
Related Questions
Check children on a grid 1 Answer
Puzzle piece rotation 0 Answers
Need help finding why my onGUI() sliding image puzzle has errors! 1 Answer
Grid Based Mouse Drag 1 Answer