Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Translate positions between different parents

Discussion in 'Scripting' started by Mibutec, Apr 24, 2023.

  1. Mibutec

    Mibutec

    Joined:
    Jul 30, 2021
    Posts:
    6
    I'm on my way to create an in-game tutorial for my 2d game. I created an overlay displaying the help text, deactivating all clicking on inactive elements and doing things like that. One part of this overlay is to highlight the object the user should interact with.

    I created a method
    Code (CSharp):
    1.  
    2. public void highlight(GameObject objectToHighlight) {
    3.     RectTransform myHighlightRect = getHighlight();
    4.     makeRectSamePositionAndSizeAsInput(myHighlightRect, objectToHighlight);
    5. }
    6.  
    The objectToHighlight is somewhere deep in an object tree describing the current screen while myHighlightRect is inside my overlay so both have different coordinate references.

    After trying out a lot the simple solution seems to be

    Code (CSharp):
    1.  
    2. public void makeRectSamePositionAndSizeAsInput(RectTransform myHighlightRect, GameObject objectToHighlight) {
    3.     RectTransform baseObjectTransform = (RectTransform) objectToHighlight.transform;
    4.     myHighlightRect.position = baseObjectTransform.position;
    5.     myHighlightRect.sizeDelta = new Vector2(baseObjectTransform.rect.width, baseObjectTransform.rect.height);
    6. }
    7.  
    However, this only works when myHighlightRect and objectToHighlight have the same pivot point. As myHighlightRect has the pivot point (0,0) the highlight is displayced by half the object size when objectToHighlight has pivot point (0.5, 0.5).

    How can I solve this pivot point thing? Am I doing it right at all?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Make it so all pivot points are the same.

    The easiest way is to choose a spot for the pivot (I like center) and then remake anything you contemplating feeding through the above routine so that it has a center pivot. This is easily done by inserting an extra GameObject in the hierarchy.

    One handy way to do these sorts of FTUE tutorial thingies is to:

    - load your scene as normal
    - load over a special "blocker" and "dimmer" set to RaycastTarget (blocks all touches)
    - reach into the existing pieces of your scene and clone them
    - move the clone to a point in the hierarchy that shows above the dimmer layer.

    Making a "FTUE" canvas that has the dimmer at its back can be a great way to start. That gives the individual pieces a place to land.

    I like this because you don't have to leave all the FTUE noise lying around your scene. It only loads in a separate scene or canvas prefab when it is needed, otherwise it is gone.
     
  3. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
  4. Mibutec

    Mibutec

    Joined:
    Jul 30, 2021
    Posts:
    6
    First things first: The answer to my question how to handle the pivot thing is:

    You can recompute the offset created by pivot point and move your overlay object by that offset
    Code (CSharp):
    1. Vector3 pivotOffset = new Vector2(baseObjectTransform.rect.width * baseObjectTransform.pivot.x, baseObjectTransform.rect.height * baseObjectTransform.pivot.y);
    2. myHighlightRect.localPosition -= pivotOffset
    From my prior life using pure C++ then BlitzBasic I'm used in thinking in coordinates from top left corner of the screen to lower right. As Unity provides (0.5, 0.5) as the default and I want Unity to do all the magic my game works on each mobile no matter which resolution it has, I try to use defaults where ever I can.
    But I'm still confused in thinking in that coordinates. Is there a best practise how to use pivot in 2D? Should I just set pivot in all my objects to a point I like? Or is (0.5, 0.5) best practise?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Oh don't even get me started how many times THAT has bitten me! :)

    Not really... you should try to leverage that pivot point as necessary. It is better (for instance) to put it at the feet / floor contact point for walking sprites, for example.

    Really the big win is just to try and put as little what I call "warts" in your code and write your code so that it assumes that "all the things" work the same. Then do whatever data wranglings (inserting extra transforms) to make "all the things" work the same from that standpoint.