• 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
Question by Vance Walsh · May 20, 2013 at 04:35 PM · gameobjecttransform.positiontransform.rotationtransform.translate

Align Parent object using child object as point of reference

Hierarchy:

  • Game object 1 with child transform (empty gameobject as a child)

  • Game object 2 with child transform (empty gameobject as a child)

Goal: Align game object 2's position and rotation with the game object 1 transform. Game object 2 needs to be aligned via its child transform (empty game object with only a transform). Also want to end up with the same hierarchy as we started with as the final result.

Current Approach - hoping for more optimized solution or easier to read / shorter:

  1. make child of game object 2 the 'parent' of game object 2 (it keeps its relative position this way)

  2. move the now 'parent' to game object 2 to the destination game object 1 transform (via transform.position and rotation - easy enough)

  3. make the game object 2 the 'parent' of its original child object transform

Obviously changing the parent / child several times gets old to type (i can make it a function yes) but it still seems slow / not required. Also consider that both game object 1 and 2 are child objects to yet another object in the scene (for both organization and movement). I tried Transform.Translate as it seemed to be the correct thing, but it didn't provide the correct results when testing, I could have just had something misplaced.

I can provide REAL code if required, this is a c# project but I can totally understand javascript, post whatever you feel is appropriate - THANKS!

Edit Image Attached Note that if half the rod is missing (as shown in the image) the overlap is visible in the objects i'm using, thus why i mention the visual issue (besides potential collider, center of mass and other physics issues)

alt text

unity-parent-child-question.jpg (203.2 kB)
Comment

People who like this

0 Show 6
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 robertbu · May 20, 2013 at 04:37 PM 0
Share

I've read this twice and am not sure what you are looking for. A drawing showing the two objects (and their childeren) before and after alignment/positioning would be very helpful.

avatar image Vance Walsh · May 20, 2013 at 04:49 PM 0
Share

I'll try to get 2 images put together. In the mean time... I'd like to know of a more efficient way to move the 2nd object and align it to the first however I can't just use the 2nd objects direct transform. I need to use its child transform as its 'anchor' point. Basically I have game object 1 as a 'connector' with multiple 'points' (child game objects) and I have game object 2 which is a 'rod' that 'clicks' into the 'connector' slots. Its for a game where you can build your vehicle from the ground up. Basically think of lego's technic pieces or k'nex if you are familiar with them.

so i need to align the rod end into the connector slot. i don't want to make the center of the rod align to the connector slot (it looks wrong - as in the mesh are inside each other).

avatar image Vance Walsh · May 20, 2013 at 05:10 PM 0
Share

image uploaded and thank you so much for the response!

avatar image Owen-Reynolds · May 20, 2013 at 05:20 PM 0
Share

So the children are "plug-in here" mount points?

Idea is to start at the "target plug." You now know the position and rotation of your plug (will be rotated opposite of the target, since maybe plugs have +Z facing away.)

You then "walk backwards" from the plug to the parent using -localPosition and your plug's real rotation. That gives the parent position. You then adjust the parent rotation for the plug's localRotation (backwards.)

Just a few lines of pure math, but it hurt my head to try to work it out. Could create and move a dummy transform to use more built-ins. I'd keep the parenting tricks, if they are working, until you know they are too slow.

avatar image Vance Walsh · May 20, 2013 at 05:28 PM 0
Share

Valid points, i'll try to work something up with this.

To your point about performance. Yes it isn't slow yet but i'm only attaching two objects together. I'm concerned for later when the user is actually attaching multiple things back and forth, moving them, changing which objects the rods / connectors are 'connected' to, etc. Its going to be a vehicle / structure builder with the possibility of having a lot of little pieces making the whole on screen. I will run some tests with a few thousand pieces and see how long it takes to iterate through them and move them to connect to each other.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by robertbu · May 20, 2013 at 05:31 PM

Here is a bit of code that does what I think you want:

 // tr1P - Game Object 1 parent transform
 // tr1c - Game Object 1 child transform
 // tr2P - Game Object 2 parent transform
 // tr2C - Game Object 2 child transform
 void Alignment(Transform tr1P, Transform tr1C, Transform tr2P, Transform tr2C) {

     Vector3 v1 = tr1P.position - tr1C.position;
     Vector3 v2 = tr2C.position - tr2P.position;
     tr1P.rotation = Quaternion.FromToRotation(v1, v2) * tr1P.rotation;
     tr1P.position = tr2C.position + v2.normalized * v1.magnitude;
 }

Note I cannot think of anything particularly wrong with the way you are currently doing it.

Comment
Jon_Olive
metalted

People who like this

2 Show 5 · 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 Vance Walsh · May 21, 2013 at 10:27 AM 0
Share

Thanks for this!

avatar image Owen-Reynolds · May 21, 2013 at 04:26 PM 0
Share

I think this assumes that, when connected, A, the connectors, and B are always in a straight line. In other words, every object's connector always faces directly away from the center of the object (which is the case in the picture.)

Think would need more math if, say, there was an off-center up-facing socket on the flat part of the disc (but a center-socket on the disc, should work fine.)

avatar image Vance Walsh · May 21, 2013 at 05:09 PM 0
Share

I haven't tested your proposed theory, but in my tests its working with my 'plug in here' game object transforms. because its all based on those positions / rotations. And size I keep z forward, it works fine for me. But I can see what you are getting at. I'll make sure I don't have a bug in the works!

avatar image robertbu · May 21, 2013 at 10:41 PM 0
Share

No more math needed. Regardless of the relationship of the empty game object to the parent, this code will align the objects. There is a question of the relative rotation of the two visible object along the axes that connects them.

Note, if the socket is always forward, you can simplify the code just a bit. v1 would be -tr1.forward. v2 would be tr2.forward. You would still need the magnitude of v1, but you could calculate it or hard code it. Unless your are going to be doing a lot of these kinds of changes per frame, the simplifications are not necessary.

avatar image Owen-Reynolds · May 22, 2013 at 01:31 PM 0
Share

"...question of the relative rotation..." Yes. Typically, the rotation of a mount point is meant to affect the rotation of the connected object.

The child of the rod might be rotated 90 degrees (but not moved) to indicate it only connects to the disc like a "b" or a duck-foot (rotation of the disc mount point would say which one.)

avatar image

Answer by EthanF4D · Oct 02, 2019 at 07:06 AM

The following do not need a source parent. and target, targetChild don't have to be direct parent/child. can be ancestor/descendant in the hierachy

 public static void Align(Transform target, Transform targetChild, Transform source)
 {
     if (target && targetChild && source)
     {
         target.rotation = source.rotation * Quaternion.Inverse(Quaternion.Inverse(target.rotation) * targetChild.rotation);
         target.position = source.position + (target.position - targetChild.position);
     }
 }
Comment
FarbrorMartin
Mccbbi
unity_0bE92MX1w5VaiA

People who like this

3 Show 3 · 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 Jon_Olive · Apr 21, 2020 at 11:03 AM 0
Share

This thread helped me solve a problem that was plaguing me for ages - namely lining up coordinate systems of two different 3d tracking systems - thanks a lot! I used EthanF4D's method ultimately - see my reply to this question: https://answers.unity.com/questions/1493924/align-htc-vive-controller-with-optitrack-rigid-bod.html

avatar image EthanF4D Jon_Olive · Jan 05, 2022 at 04:35 PM 0
Share

i am glad it helped out

avatar image unity_0bE92MX1w5VaiA · Jan 05, 2022 at 03:21 PM 0
Share

This solution was exactly what I needed!

avatar image

Answer by ljdp · Sep 04, 2018 at 07:43 PM

I know this is an old question, but I came to a different solution. This also works well when tweening to position.

 void Alignment(Transform t1P, Transform t1C, Transform t2) {
     t1P.rotation = t2.rotation * Quaternion.Inverse(t1C.localRotation);
     t1P.location = t2.position + (t1P.position - t1C.position);
 }
Comment
Kinosei

People who like this

1 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 Chambers88 · Mar 22, 2019 at 06:41 PM 0
Share

You mean position instead of "location" right? Or would it be localPosition?

Either way, didn't work here :/ perhaps because my t2 is scaled?

avatar image DaedricWolf19 · Jul 13, 2019 at 07:57 PM 0
Share

I'm sorry to revive this, but could you explain mathematically how you came to this solution? I'm stuck at how i would actually go about taking it into consideration myself, let alone write my own solution.

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

20 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

Related Questions

changing x multiplies x,y and z position for no reason 2 Answers

How to shift the player to a definite position in the x-axis using key presses? 1 Answer

Move up gameobject from under the ground or object (vibration problem) 0 Answers

Positions of Objects in Unity - World Position, Transform Position, Inspector Position, Local Position, Prefab Positions, Center of Objects for Parents & Childs 1 Answer

Can an accelerometer be used for a RigidBody based movement? 0 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