I’m using an editor to spread QR-Codes in local space of an object. For better understanding, I’ll try to explain it with an example:
First I load a model of a car into the editor. This model is our parent. Now I place a QR-Code on each side mirror of the car. The localPosition and localRotation of these QR-Codes are saved for later reference in my application. I also place a 3D-Model in the center of the hood of the car. This 3D-Model will be shown in my AR-Application.
Now I’m using this application inside a real car (prefably the model, I used as reference in my editor). I placed QR-Codes on the real side mirrors of this car. Now when my application detects one of the QR-Codes, I want to use the localPosition and localRotation that I stored earlier, to align the parent in real life.
I’m using this code, to align the parent:
public class StickyParent : MonoBehaviour
{
[SerializeField] Transform parent;
[SerializeField] Transform qrCode;
[SerializeField] Vector3 localQrPosition;
[SerializeField] Vector3 localQrRotation;
public void Update()
{
if (parent == null) return;
var fixedPos = new Vector3(-localQrPosition.x, -localQrPosition.y, localQrPosition.z); // Had to invert x and y, for the correct outcome. Not sure why...
var trackedPosition = qrCode.TransformPointUnscaled(fixedPos); // Extention method: TransformPoint that ignores scale
var trackedRotation = qrCode.rotation * Quaternion.Euler(localQrRotation);
parent.position = trackedPosition;
parent.localRotation = trackedRotation;
}
}
This code seems do the right thing, but the calculations are definetly wrong. Maybe someone understands this issue and can tell me the correct math, to align a parent based on pre-stored localPos and localRot of a child.