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: 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);
}
}
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);
}
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"?
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.