Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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;
         }
    }
}
share|improve this answer
    
Thanks, I used a similar approach, as it is described here: answers.unity3d.com/questions/58449/… –  Andrei T. Ursan May 12 '11 at 13:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.