Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Andrei T. Ursan · May 12, 2011 at 11:26 AM · cubejoint

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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 &lt; cubes.length; i++)
     {
         cubes[i].gameObject.AddComponent(Rigidbody);

         if(i &lt; (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!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jean-Fabre · May 12, 2011 at 12:22 PM 0
Share

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.

avatar image Joshua · May 12, 2011 at 12:27 PM 0
Share

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.

avatar image Andrei T. Ursan · May 12, 2011 at 01:09 PM 0
Share

Thanks a lot! didn't knew that you can do Edit$$anonymous$$ode Scripts :D. I will study more :D

avatar image Joshua · May 12, 2011 at 01:13 PM 0
Share

Yeah it's an aspect of unity that most newcomers (including me) don't notice until later. It's VERY handy though :)

avatar image
-1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

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

trying to make the inside of a rubik's cube 2 Answers

How do I make a grey cube transparent? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges