Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by Di_o, Jul 4, 2023.

  1. Di_o

    Di_o

    Joined:
    May 10, 2016
    Posts:
    55
    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:

    Code (CSharp):
    1. public class StickyParent : MonoBehaviour
    2. {
    3.     [SerializeField] Transform parent;
    4.     [SerializeField] Transform qrCode;
    5.  
    6.     [SerializeField] Vector3 localQrPosition;
    7.     [SerializeField] Vector3 localQrRotation;
    8.  
    9.     public void Update()
    10.     {
    11.         if (parent == null) return;
    12.  
    13.        var fixedPos = new Vector3(-localQrPosition.x, -localQrPosition.y, localQrPosition.z); // Had to invert x and y, for the correct outcome. Not sure why...
    14.         var trackedPosition = qrCode.TransformPointUnscaled(fixedPos); // Extention method: TransformPoint that ignores scale
    15.         var trackedRotation = qrCode.rotation * Quaternion.Euler(localQrRotation);
    16.  
    17.         parent.position = trackedPosition;
    18.         parent.localRotation = trackedRotation;
    19.     }
    20. }
    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.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    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://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    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://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

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

    Di_o

    Joined:
    May 10, 2016
    Posts:
    55
    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.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,940
    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?
     
  5. Di_o

    Di_o

    Joined:
    May 10, 2016
    Posts:
    55
    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.