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);
}
}