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 Mungyun · May 23, 2013 at 12:14 PM · parentingattached

Attaching to parent while keeping position relative

In code, I have been trying to figure out how to parent a rigid body child to an object while always keeping its position the same distance from the parent at all times, taking into account the parents rotation. For example, I create a sphere at 0,0,0 and attach a child sphere at 10,0,0 using transform.parent. If the parent moves 1 unit along X, the child would move 1 along X as well. If the parent rotates 90 degrees on y axis, the child would also rotate 90 degrees along the parents y axis.

Currently I am using fixed joints to accomplish this but I think there has to be a better way. I use Havok at work and they have an AttachToParent that keeps all the orientations and rotations relative to the parent without using joints.

I could easily be thinking about this the wrong way, but how can I accomplish this in Unity?

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
1

Answer by smnerat · May 25, 2013 at 02:11 AM

I'm pretty sure you are over thinking it. If you have an object 1 at (0,0,0), and object2 at (10,0,0) and you parent the object 2 to object 1 (by dragging object 2 onto object 1 in the inspector) and you then move object 1, object 2 will move with it. I don't see why you would need joints for this.

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

Answer by 1337GameDev · May 25, 2013 at 04:18 AM

To programmatically parent an object to another (so that it moves and rotates and scales with the parent object) just set child.transform = parentObj.transform. A parent member is of type Transform, not gameobject. Also make note to parent the current script's gameobject it is attached to, just use: transform.parent = otherGameObject.transform.

When you manipulate the parent, the child gameobject will follow as well, keeping offsets and such from before it was parented.

Parent Unity Doc

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

15 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

Related Questions

finding an object with tag, out of a few objects that are in array 1 Answer

Picking up and dropping objects with parenting 1 Answer

local space and rotation issues 1 Answer

Can a Gameobject that is controlled by Animator be dragged by a non animator controlled object 1 Answer

How do i make my bulletSpawn become a parent of something 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