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.
1 Answer
I usually don't write code for these questions, but I'm bored. I don't have unity on this computer so I'm just guessing, it should give you an idea even if it doesn't compile. I just added each blocks right and top joint as long as it's not on the top row or far right column. In a real situation you would add the parent transforms x and y position and do all positioning in local space so it can accept the parents transform, but you get the idea I hope.
int x = 6;
int y = 7;
float hSpacing = 0.5f;
float vSpacing = 0.5f;
GameObject[,] wall = new GameObject[x,y];
for(int i = 0;i<width;i++)
{
for(int j = 0;j<height;j++)
{
//Add the Cube
wall[i,j] = (GameObject)Instantiate(cube,
new Vector3(i*(cube.lossyScale.x+hSpacing),
j*(cube.lossyScale.y+vSpacing),0);
//Add right joint
if(i+1<width)
{
FixedJoint fjR = wall[i,j].AddComponent("FixedJoint") as FixedJoint;
fjR.connectedBody = wall[i+1,j].rigidbody;
}
//Add top joint
if(j+1<height)
{
FixedJoint fjT = wall[i,j].AddComponent("FixedJoint") as FixedJoint;
fjT.connectedBody = wall[i,j+1].rigidbody;
}
}
}
-
\$\begingroup\$ Thanks, I used a similar approach, as it is described here: answers.unity3d.com/questions/58449/… \$\endgroup\$– user6395May 12, 2011 at 13:45