• 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
1
Question by trickpirata · Oct 12, 2012 at 03:23 AM · rigidbodyruntimeconnect

Connect GameObjects together at runtime?

Hello, I am creating a map with the use of 3D blocks on runtime. I've done this before but in a diffent way because the script I made before is for 2D objects and with fix size for each blocks.

Now my question is, how can I connect/snap 2 objects on runtime using a pivot point or a joint? Thanks for the help.

Comment
Add comment · Show 1
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 trickpirata · Oct 12, 2012 at 10:15 PM 1
Share

Hello, I am currently using this method. The first thing I did was

  1. Cube = Parent && Pivot/Connector = Child

but the problem is I'm not getting the exact position of the child. Should I change the hierarchy? Or is it any other way that I can get the exact position of the child? I made the Pivot/Connector a CHILD because I am using $$anonymous$$ULTIPLE PIVOT/CONNECTOR in ONE Cube. Hope this info helps.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by sparkzbarca · Oct 12, 2012 at 05:32 AM

you can join them through scripting

there are three types of joints hinge,spring,character they are a type of component. you can add a joint to an object through add component.

example c# code to add a joint to object a and "join" object a to b

 Gameobject object_A;
 Gameobject object_B;
 
 object_A.addComponent<HingeJoint>();
 
 object_A.HingeJoint.ConnectedBody = object_B;

best of luck! mark if answered

Comment
Add comment · Show 2 · 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 trickpirata · Oct 12, 2012 at 05:35 AM 0
Share

I'm away from my laptop right now. Just a few question, can that hingejoint snap the 2 gameobjects? I mean, the will be close/next to each other?

avatar image Bunny83 · Oct 12, 2012 at 10:52 PM 0
Share

I've just spotted some errors ;)

  • ConnectedBody is a Rigidbody reference, so you have to assign a Rigidbody, not a GameObject.

  • AddComponent starts, like all function names, with a capital "A".

  • The hingeJoint shortcut property starts, like "most" properties in Unity, lowercase.

  • Not an error but a simplification, AddComponent returns the newly added component, so you can use it directly to access the HingeJoint.

Besides that like Fattie said a HingeJoint is just a physical connection between two rigidbodies which applies forces when a rigidbody is moved.

avatar image
0

Answer by Bunny83 · Oct 12, 2012 at 10:43 PM

At the moment i work on a track editor for a racing game and i also have trackparts with multiple connectors as child objects. I place them like that:

 // C#
 Transform Part; // TrackPart
 Transform Con;  // Connector object is a child of "Part"
 
 void AttachTo(Transform aOtherConnector)
 {
     Transform oldParent = Part.parent; // save the original parent
     Con.parent = null;                 // unparent the connector
     Part.parent = Con;                 // attach our actual part as child to the connector
     
     Con.position = aOtherConnector.position; // place the connector
     Con.forward = -aOtherConnector.forward;
     
     Part.parent = oldParent;           // revert the parenting
     Con.parent = Part;
 }

I use "-forward" since the two connectors that should be "connected" face in the opposite direction.

It works pretty well, but i experienced a slight error which grows as you build your TrackPart-chain. I recommend you also use a grid base placement and determine the grid cell the object should be in after you've done what i wrote above and set the position / rotation again to absolute values.

This is of course only a solution if you want to place the objects as static objects (like my racing track for example).

Comment
Add comment · Show 1 · 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 trickpirata · Oct 13, 2012 at 08:47 PM 0
Share

Will definitely try this. I'll try to have a grid-based system but my problem is the scales of my blocks/sizes are not the same.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Connect two rigidbody gameobjects together at runtime? 0 Answers

Dynamically parenting a collider object to a rigidbody object causes collision bug. 1 Answer

Can I scale an object at runtime? 1 Answer

How to procedurally generate a super simple terrain 1 Answer

How to create compound rigidbodies? 2 Answers


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