Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Simulate WorldPosition from a LocalPosition

Discussion in 'Scripting' started by m506, May 30, 2020.

  1. m506

    m506

    Joined:
    Dec 21, 2015
    Posts:
    80
    Hi, I need some help to simulate the worldPosition from a localPosition. Let me explain.
    I have the following structure of a given gameobject:

    myGameObject
    - pivot


    The code below works beautifully using a pivot:
    Code (CSharp):
    1. pivot.localPosition = position;
    2. pivot.localRotation = rotation;
    3. myTransform.SetPositionAndRotation(pivot.position, pivot.rotation);
    But as shown, it relies on a pivot attached to the gameobject, which I don't like.
    How can I achieve the same without a pivot?
    I already tried TransformPoint/InverseTransformPoint but never managed to get the same results.

    I appreciate any help on this
    Regards
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,549
    That's not really enough information to help you. TransformPoint/InverseTransformPoint are the way to change between local and global coordinates, so if those didn't work, either you used them wrong (in code you haven't shown), or the surrounding code (that you also haven't shown) is relying on them being local in some other way. Either way, you haven't shown the code with the actual problem.
     
  3. m506

    m506

    Joined:
    Dec 21, 2015
    Posts:
    80
    Hi, thanks for replying.
    There's no problem here at all. My code works fine using a pivot, but I just want to optimise it and get rid of the extra gameobject, and learn another technique as well. My question is how I can transform the code I shown to an equivalent using TransformPoint/InverseTransformPoint. Maybe it can also be useful to other developers reading this to know both techniques.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    4,848
    As far as I know there's no way in unity to change the pivot point of rotation apart from:
    • Use a separate pivot object, as you are doing
    • Change the origin point of your 3d model in 3d modeling software such as blender
    • Change the pivot of your 2D Sprite in the Sprite import settings.
     
  5. m506

    m506

    Joined:
    Dec 21, 2015
    Posts:
    80
    No worries, I'll stick with using a pivot for now. Thanks
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    4,848
unityunity