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. Dismiss Notice

Question Image moving too far

Discussion in 'UGUI & TextMesh Pro' started by tomlugin100, Jun 17, 2023.

  1. tomlugin100

    tomlugin100

    Joined:
    May 6, 2023
    Posts:
    45
    Hello and thank you so much for reading! I'm coding a store scene where I allow players to purchase different kinds of "platforms" to jump on. I have a right arrow which on press should move the next purchase item to the center of the screen. window change screenshot.png
    As you can see in the image, there's a "Pack" called Kerrvillian which should move to the left at the same time as the Americano moves left off the screen. I have code that looks like this:
    Code (CSharp):
    1. Debug.Log("moving left");
    2.            
    3.             Vector3 pos2 = Pack2.GetComponent<RectTransform>().position;
    4.             Vector3 pos1 = Pack1.GetComponent<RectTransform>().position;
    5.             Debug.Log(pos2.x);
    6.             if (pos2.x < 0)
    7.             {
    8.                 Pack2.GetComponent<RectTransform>().position = new Vector3(0, pos2.y, 0);
    9.                 isClickable = true;
    10.                 currentWindow = 2;
    11.             }
    12.             else if (pos2.x > 0)
    13.             {  
    14.                 pos2.x -= windowChangeSpeed;
    15.                 pos1.x -= windowChangeSpeed;
    16.                 Pack2.GetComponent<RectTransform>().position = pos2;
    17.                 Pack1.GetComponent<RectTransform>().position = pos1;
    18.             }
    When I manually set the Pos X to 0, everything looks proper. However, when I run the script, the Kerrvillian platform keeps moving left after it hits 0, all the way to -540.
    window change screenshot 3.png
    Why is it doing this? I need the "KErrvillian PAck" to stop at 0 but it doesn't. Please help me!
     

    Attached Files:

  2. tomlugin100

    tomlugin100

    Joined:
    May 6, 2023
    Posts:
    45
    Also, in the console as you can see in the image, a posX of 0 is showing. However, in the inspector -540 is showing. I'm still rather new to Unity so maybe I'm missing something obvious?
     
  3. tomlugin100

    tomlugin100

    Joined:
    May 6, 2023
    Posts:
    45
    I believe I solved my problem by using RectTransform.anchoredPosition instead of RectTransform.position. I found that solution through Google and this thread: https://forum.unity.com/threads/change-position-ui-image-in-canvas.586324/
    However, I don't understand what the anchoredPosition is, I'm just using it. Could anyone explain what anchoredPosition is? Thank you
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Good luck with that. I still don't fully understand RectTransforms.

    For instance, the simple act of placing the anchors at the corner of the RectTransform, something you might intuit would be easy, is insanely complex. Check it:

    http://forum.unity3d.com/threads/sc...hor-to-gui-object-size-rect-transform.269690/

    Note how many "Oh it doesn't quite work right, here's my fixes" versions got posted.

    CRAZY.