Use localPosition and localRotation of a child to align parent (QR-Codes)

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.

That just sounds like it is… Time for you to start debugging!

Here is how you can begin your exciting new debugging adventures:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the names of the GameObjects or Components involved?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

“When in doubt, print it out!™” - Kurt Dekker (and many others)

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

Please don’t post some generic “How to I code in Unity” stuff… I’m asking about calculating Transformations, not how to debug my code.

Debugging is a way to work out why your calculations are wrong an often puts you onto the right path.

Why not just store a world location/rotation and just instantiate the QR code object in that position?

Indeed! You could post this answer anytime someone asks about code, though. This is simply missing the point. I don’t support copy & paste answers.