• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Spawn at random? 1 Answer

Instantiated (clone of) prefab, doesn't trigger another object's collider when inside it. 2 Answers

server and client sync 2 Answers

Spawning unique prefabs at different transform posititions 0 Answers

Problem with instantiate script 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges