Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Garsum · Sep 07, 2016 at 07:13 AM · movementtransformmoveeuleranglesquaternions

What is the correct way to move objects relative to each other ?

When a parent object has a child object it is very easy to rotate and move the parent and have to child remain in the correct position relative to the parent. In order to move the child from one parent to another while maintaining the relative position it seems really easy to copy transform.local.position and transform.local.rotation from one parent to another.

Unfortunately for my application the object I'm trying to move is not a child of the first object and will not become the child of the second. I still need to maintain the relative positioning when the test rig is moved from its relative position from the starting plane to its new position relative to the ending plane in three dimensions.

Here is a two-dimensional representation of what is needed: alt text It seems to me that this type of relative movement is the basic component of unity. Unfortunately I can't seem to decipher the correct functions to use.

The following code works:

but I feel there must be a much better and simpler and more efficient way to accomplish the task.

Any pointers would be greatly appreciated.

PS: my previous account fails whenever I login so I was forced to create a new account for this post. My apologies for duplicating the question.

 using UnityEngine;
 using System.Collections;
 
 public class TestTriggerRight : MonoBehaviour {
 
     public string MyName; // use this name to tell thing apart
     public GameObject StartingPlane; //Plane Object with Rig Object position somewhere around it 
     public GameObject EndingPlane; // Plane Objece that we want to move the rig Object to
     public GameObject TestRig; // Rig Object that we are going to move;
     public GameObject TestRigE; // A Rig Object placed correctly to see if it worked correctly
 
     // Use this for initialization
     void Start()  // We will attach the script to a vive controller and use a trigger pull to move the rig object
     {
         var trackedController = GetComponent<SteamVR_TrackedController>();
         if (trackedController == null)
         {
             trackedController = gameObject.AddComponent<SteamVR_TrackedController>();
         }
 
         trackedController.TriggerClicked += new ClickedEventHandler(DoClick);
     }
 
     // Update is called once per frame
     void Update()
     {
 
     }
     void DoClick(object sender, ClickedEventArgs e)
     {
         Debug.Log(MyName);
         Debug.Log("- - - - - - - - - -< Location >- - - - - - - - - -");
         Debug.Log(StartingPlane.transform.position);
         Debug.Log(TestRig.transform.position);
         Debug.Log(EndingPlane.transform.position);
         Debug.Log(TestRigE.transform.position);
         Debug.Log("- - - - - - - - - -< Angles >- - - - - - - - - -");
         Debug.Log(StartingPlane.transform.eulerAngles);
         Debug.Log(EndingPlane.transform.eulerAngles);
         Debug.Log("- - - - - - - - - -< Move to Neutral >- - - - - - - - - -");
         Vector3 StartingRotation = StartingPlane.transform.rotation.eulerAngles; // Get Starting Plane Rotation in Dergees
         TestRig.transform.RotateAround(StartingPlane.transform.position, Vector3.right, -StartingRotation.x);
         TestRig.transform.RotateAround(StartingPlane.transform.position, Vector3.up,-StartingRotation.y);
         TestRig.transform.RotateAround(StartingPlane.transform.position, Vector3.forward, -StartingRotation.z);
         Debug.Log(StartingRotation);
         Debug.Log(TestRig.transform.rotation.eulerAngles);
         Debug.Log(TestRig.transform.position);
         Debug.Log("- - - - - - - - - -< Relocate to ending >- - - - - - - - - -");
         TestRig.transform.position = TestRig.transform.position - StartingPlane.transform.position + EndingPlane.transform.position;
         Debug.Log(TestRig.transform.position);
         Debug.Log("- - - - - - - - - -< Move to New >- - - - - - - - - -");
         Vector3 EndingRotation = EndingPlane.transform.rotation.eulerAngles; // Get Ending Plane Rotation in Dergees
         TestRig.transform.RotateAround(EndingPlane.transform.position, Vector3.right, EndingRotation.x);
         TestRig.transform.RotateAround(EndingPlane.transform.position, Vector3.up, EndingRotation.y);
         TestRig.transform.RotateAround(EndingPlane.transform.position, Vector3.forward, EndingRotation.z);
         Debug.Log(EndingRotation);
         Debug.Log(TestRig.transform.rotation.eulerAngles);
         Debug.Log(TestRig.transform.position);
     }
 }
 




move-test-rig.jpg (69.0 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by metalted · Sep 07, 2016 at 07:36 AM

If you want to make a kind of parent-child position relationship without actually parenting the object, you could make use of a method like this:

https://docs.unity3d.com/ScriptReference/Transform.TransformPoint.html

I wrote an example code to make things clear:

 //The position calculated from the masterObject
 private Vector3 relativePosition;
 //What position do we want the slaveObject to have in local space?
 private Vector3 wantedPosition = new Vector3(0,0,-5);
 //The slave object(child)
 public Transform slaveObject;
 //The master object(parent)
 public Transform masterObject;
 
 //Call every frame
 void Update()
 {
     //Calculate the position
     //What this does:
     //Take the master object and use the local space "wantedPosition" to
     //calculate a world space position. Assign this to "relative position"
 
     relativePosition = masterObject.TransformPoint(wantedPosition);
 
     //Set the position of the slave object to this relative position
     slaveObject.position = relativePosition;
 
     //Make the slave objects look at the master object
     slaveObject.LookAt(masterObject);
 }


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 doublemax · Sep 07, 2016 at 08:54 AM 0
Share
 slaveObject.LookAt(masterObject);

Unfortunately this simplification doesn't work in all cases. What if the "child" element has an arbitrary relative rotation to the "parent"?

avatar image metalted doublemax · Sep 07, 2016 at 09:16 AM 0
Share
 slaveObject.LookAt(masterObject);

This can best be used in cases with rotations on a flat plane.

 slaveObject.rotation = masterObject.rotation;

This option is better if you want to create a kind of 3d LookAt() in this situation.

To answer your question about the arbitrary rotation: By removing the "slaveObject.LookAt(masterObject)" part, only the position of the slaveObject will be affected. This way you can add your own rotations to the slaveObject, by using transform.Rotate, transform.rotation etc.

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

85 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 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 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 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

Limit movement to just forward/backward and left/right 0 Answers

How could I make an object move from left to right and it will come back from right? 0 Answers

Limit movement to just forward/backward and left/right 0 Answers

GameObject doesn't move 1 Answer

Why Quaternions? 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