- Home /
how to automatically create fixed joints in a wall of cubes?
I want to create a wall:
width 6 cubes
height 7 cubes
therefore I have 22 cubes on the margins and 20 cubes inside. For each of the margins cubes(except the corners) I have to set 3 fixed joints, and for the cubes inside the wall I have to set 4 fixed joints - all this to have good physics behavior.
Pretty much I have to create a fixed joint between a cube and all his neighbors => and that's a lot!
Is there a way to automatically create these fixed joints?
Is there another way to create a physics friendly wall? I want to simulate a wall of bricks hited by a projectile.
Answer by Joshua · May 12, 2011 at 12:19 PM
In these cases you want to use in-editor scripting to quickly set things up.
I would use one script to actually create them all and a separate one to tweak them, so you have more freedom when tweaking and so they are all created when referencing eachother. Firstly create an empty GameObject named "Wall", place it where you want the left-most brick to be and simply run:
@script ExecuteInEditMode()
var done : boolean = false; var wall : GameObject; var width : int = 6; var height: int = 7;
var cube : GameObject; var cubeWidth : float; var cubeHeight : float;
function Update () { if (!done) { for (var i = 0; i < height; i++) { for (var j = 0; j < width; j++) { var newCube = Instantiate (cube, Vector3(wall.transform.position.x+j*cubeWidth, wall.transform.position.y+i*cubeHeight ,wall.transform.position.z), Quaternion.identity); newCube.name = ""+i+"-"+j; //names them after their position in the grid for easy refferencing; } }
done = true;
}
}
next you want a script that sets everything up:
@script ExecuteInEditMode()
var done : boolean = false; var wall : GameObject;
var height : int = 7; var width : int = 6;
function Update () { if (!done) { var cubes : Array = wall.GetComponentsInChildren(Transform);
cubes.Shift(); //remove the wall itself from the array
for (var i = 0; i < cubes.length; i++)
{
cubes[i].gameObject.AddComponent(Rigidbody);
if(i < (cubes.length-width)) //connect each to the one above it, so exclude the upper row.
{
cubes[i].gameObject.AddComponent(HingeJoint);
cubes[i].gameObject.hingeJoint.connectedBody = cubes[i+width].gameObject.rigidBody;
}
}
done = true;
}
}
Now it's up to you to edit this script that it does not only attach it to each one above it, but also below, right and left to it. Good luck!
I would actually do a in game script, not real need to have it done in the editor since anyway if you want to tweak a value, you need to script it as well, since when it's created, you're not going to go on all created joint and modify a setting.
Well to have it run in the game itself you obviously just remove the very first line of both scripts. It's nice, how ever, to have it in the editor so you can manually place it, rotate it, etc.
Thanks a lot! didn't knew that you can do Edit$$anonymous$$ode Scripts :D. I will study more :D
Yeah it's an aspect of unity that most newcomers (including me) don't notice until later. It's VERY handy though :)
Answer by vatchareeta · Dec 21, 2011 at 06:42 PM
Hi I do not understand the code you gave why it does not work I got this message: Script newbehaviourscript has not finished compilation yet. Please wait until compilation of the script has finished and try again.
Your answer
Follow this Question
Related Questions
A Cube made from cubes 1 Answer
Make an object rotate from one end only 1 Answer
Configurable Joint just falls apart? 1 Answer