Search Unity

Question How to change the pivot of an object at runtime without it moving.

Discussion in 'Scripting' started by TiggyFairy, Mar 18, 2023.

  1. TiggyFairy

    TiggyFairy

    Joined:
    Dec 22, 2019
    Posts:
    506
    So I've been having trouble using code to change the pivot points of UI objects without the object moving. The reason I need to do this is so that I can then use tweening to swoop the object to the top left corner of the screen. However my normal code for doing this sort of thing doesn't seem to work when you aren't parenting or un-parenting objects:

    Code (CSharp):
    1.  
    2. Vector2 x = rect.transform.position;
    3. rect.transform.SetParent(rPanel.transform, true);
    4. rect.pivot = rect.anchorMin = rect.anchorMax = pivot;
    5. rect.transform.position = x;
    I've also tried localPosition. And I also tried to do it with the next block of code here, which very nearly almost seems to work. It moves the object up to sit directly beside the spot where it is supposed to be (which is dead centre of the screen.)

    Code (CSharp):
    1.  v = rct.anchoredPosition;
    2. rct.pivot = new Vector2(0, 1); //rct.anchorMin = rct.anchorMax =
    3. rct.anchoredPosition -= v - (rct.pivot * (rct.sizeDelta / 2));
    Don't suppose anyone knows how I can do this properly? For preference, I would like to be able to change all the anchor points at once as I've found this seems to give better results when resizing the screen. Thank you
     
  2. alexandr13568

    alexandr13568

    Joined:
    May 9, 2021
    Posts:
    4
    Also looking for it...
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
    Why don't you manually make a scene which does exactly what you want, anchor-wise, and then try to recreate that from code by reading what the API does and making sure you programmatically do exactly what you already did manually?

    I mean these things are pretty much simple. Everything you can do in the editor is exposed via API in code. It shouldn't be as hard to experiment with the possibilities, and fully digest the documentation until you are confident with such simple use cases i.e. repositioning anchors etc.
     
  4. TiggyFairy

    TiggyFairy

    Joined:
    Dec 22, 2019
    Posts:
    506
    Can you explain what you mean?
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,116
    Well, I can only rephrase, to be honest.
    You first nail down the actual technique you want to achieve through the inspector, then once you learn what steps produce your desired result, you figure out how to achieve the same thing via the programmatic interface.

    You said
    My point is try to accomplish this manually first. Do you know how to do this from the editor, or did you immediately try to write code? In other words, make it clear for yourself what needs to be done so you can change the pivot points without the object moving.
     
  6. TiggyFairy

    TiggyFairy

    Joined:
    Dec 22, 2019
    Posts:
    506
    Well, I've altered the pivots a lot in the editor and it generally doesn't move if you alter the numbers manually. However, that doesn't seem to translate to the code itself. Not sure why. Would the order I do it matter at all?