• 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 bbeckford · Apr 01, 2014 at 11:30 PM · physicsinstantiatespawnproceduralhingejoint

Spawning GameObjects with Hinge Joints

Hi everyone,

I'm working on a level generator for an endless runner and I want to spawn some prefabs with hinge joints.

The trouble is that when I spawn an object with a hinge joint, it retains the anchor and connected anchor from when the prefab was saved, not relative to its spawned position.

Any idea how I can fix this?

Comment
Add comment · Show 2
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 Veerababu.g · Oct 27, 2015 at 10:09 AM 0
Share

hi sir, am facing problem on adding hinge joint to my prefab. what my project i need to create rope that was dynamically increasing and decreasing when we press up and down arrows. what am doing is

//instantiating a prefab

clone = (GameObject)Instantiate (obj, current.transform.position, Quaternion.identity);

//storing the prefab into a list

         instantlist.Add(clone);
         instantlist[instantlist.Count-1].name="clone"+instantlist.Count;

//adding hinge joint

clone.AddComponent(); clone.GetComponent

//assaign position

         clone.transform.position = new Vector3 (current.transform.position.x, current.transform.position.y+1f , current.transform.position.z);
         current = clone.gameObject.transform;
 
avatar image litebox · Dec 15, 2020 at 11:10 AM 0
Share

Another option is to use a Rigid Body (IsKinematic=true) as parent and set the Connected Rigid Body property to this. Spawning the parent keeps the Connected Anchor in place and it works without any glitches. I found this solution on https://stackoverflow.com/questions/22845379/unity-spawning-gameobjects-with-hinge-joints

1 Reply

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

Answer by sampenguin · Apr 15, 2014 at 02:37 AM

I am spawning a hinge joint object from a prefab and it works OK, provided you connect to the other rigid body after instantiation.

 objectBwithHingeJoint = GameObject.Instantiate(hingeJointPrefab, objectA.hingeJointSpawnOffset.transform.position, Quaternion.identity) as GameObject;
 HingeJoint hj = objectBwithHingeJoint.GetComponent<HingeJoint>();
 hj.connectedBody = objectA.rigidbody;

You should also be able to adjust the anchor properties via script at this point.

Also I found early on that nesting an object with a hinge joint inside the object its attaching to caused lots of problems. So I spawn them both independently (with B getting its initial position from a dummy transform inside of A) and attach via script, as you can see above.

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 bbeckford · Apr 16, 2014 at 09:31 AM 0
Share

Thanks for the advice!

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Photon Instantiate 2 Answers

Hingejoint not responding on script 4 Answers

How do you attach moving parts (moved by physics) to an animated character? (Secondary Animations) 1 Answer

Disable weight "inheritance" when using HingeJoint2D? 1 Answer

instantiate a set amount 3 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