Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug UI Objects - in game mode are not on same position as pausemode. Bug? Or did I make a mistake?

Discussion in 'Scripting' started by mathric, May 26, 2024.

  1. mathric

    mathric

    Joined:
    May 10, 2024
    Posts:
    12
    Hey,

    I made an Script for showing positions of enemy. Therefore I created a UI mask, added plane and applied a image of a radar. Then I created a radarLine.

    upload_2024-5-26_21-45-51.png
    upload_2024-5-26_21-46-29.png
    Radar line:
    upload_2024-5-26_21-47-14.png
    I created the radarBlip with a cylinder for the Z-distance as a prefab.
    upload_2024-5-26_21-49-21.png
    here the cylinder.
    upload_2024-5-26_21-49-32.png
     
  2. mathric

    mathric

    Joined:
    May 10, 2024
    Posts:
    12
    then I positioned the Cylinder in the blip like this:
    upload_2024-5-26_21-50-47.png
    this makes moving in z-direction along this line and scale y extend the line.

    I applied the script to the RadarScreen, connected the sliders and run it.


    upload_2024-5-26_21-52-27.png

    Ingame you see blips are correct - but the cylinders seem to be at a wrong position (next to the Demo).

    If you pause and watch the scene editor ...

    you see they are correct.

    upload_2024-5-26_21-53-35.png
     
  3. mathric

    mathric

    Joined:
    May 10, 2024
    Posts:
    12
    I'm using Unity 2022.3.38f1 on Windows10.

    here I added the scripts and a set of PNGs for testing if you like.
     

    Attached Files:

    Last edited: May 26, 2024
  4. mathric

    mathric

    Joined:
    May 10, 2024
    Posts:
    12
    For testing: Create a Scene, add some elements - and If you want to run my code...
    upload_2024-5-26_22-0-0.png
    You need to add Tags. I made a multi-tag script (see attached).

    Else you need to modify the script here:


    Code (CSharp):
    1.  foreach (GameObject obj in objects)
    2.         {
    3.             //infoTextGUI.text = "";
    4.             TagComponent tagComponent = obj.GetComponent<TagComponent>();
    5.             if (tagComponent != null && radarTags.Exists(tag => tagComponent.HasTag(tag)))
    6.             { ... do the code for show object}
    7.  
    to something like:


    Code (CSharp):
    1.  foreach (GameObject obj in objects)
    2.         {
    3.            
    4.             if (obj.Name=="Target"))
    5.             { ... do the code for show object}
    6.  
    And simply place a object with the right name in the scene.


    I tested lots of variants, but the cylinders never position on the ui element. Did I make something wrong or is it a bug?
     

    Attached Files:

  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,369
    Any game with a rocky mountainous lunar terrain and "ROCKET DEMO" on it is my kinda game!! :)


    Generally don't manipulate UI Transforms in code, unless the positions are derivative of OTHER UI Transforms already in the scene, such as anchors placed at the corners of your screen.

    The reason is that it takes like 17 lines of code just to move anchors to the corners of a RectTransform, and that should be about the simplest thing possible, and it's not.

    I usually make a
    public RectTransform LowerLeftCorner
    and
    UpperRightCorner
    property and drag hard-anchored stuff into those, then use the fraction X and Y (also known as "Viewport" in Unity camera speak) to move the UI element to a fraction between those, like so:

    Code (csharp):
    1.     // use Camera.WorldToViewport to get these fractional input values
    2.     public void SetViewportPosition( float xfraction, float yfraction)
    3.     {
    4.         xfraction = xfraction + 0.5f;
    5.         yfraction = yfraction + 0.5f;
    6.  
    7.         // maybe use LerpUnclamped() if you prefer...
    8.         float x = Mathf.Lerp( LowerLeftCorner.position.x, UpperRightCorner.position.x, xfraction);
    9.         float y = Mathf.Lerp( LowerLeftCorner.position.y, UpperRightCorner.position.y, yfraction);
    10.  
    11.         transform.position = new Vector3( x, y);
    12.     }

    Regardless, you'll want to keep this stuff firmly in mind too:

    Here are the official docs on how to make your UI handle various resolutions and aspect ratios cleanly:

    https://docs.unity3d.com/2023.1/Documentation/Manual/HOWTO-UIMultiResolution.html

    Here are some notes on UI Anchoring, Scaling, CanvasScaler, etc:

    https://forum.unity.com/threads/inc...size-between-two-people.1130146/#post-7261747

    https://forum.unity.com/threads/game-ui-button-size-problem.1142650/#post-7337383

    Usually you need to choose a suitable ScaleMode and MatchMode in the Canvas Scaler and stick with it 100%. Generally if you change those settings you will often need to redo your UI entirely.

    I also use this
    CanvasScalerOrientationDriver
    utility to make sharing UI for Landscape / Portrait easier. Read what it does carefully.

    https://gist.github.com/kurtdekker/8802b1b6c708637398f8c9167641efd3


    Apropos of nothing, here is my Jetpack Kurt Space Flight game, basically a first person lunar lander.

    The little POI radar blip markers across the top of the screen are done with the above code snippet:

     
    mathric likes this.
  6. mathric

    mathric

    Joined:
    May 10, 2024
    Posts:
    12
    So you think its an issue with the resolution?