Search Unity

Moving UI image based on the player movement, in 1:1 ratio in scene

Discussion in 'Scripting' started by Adam_Benko, Jun 21, 2019.

  1. Adam_Benko

    Adam_Benko

    Joined:
    Jun 16, 2018
    Posts:
    105
    I have a 2D game in unity. I want the UI image (that is in canvas object), to move over the player/enemy gameobject.

    With my script, the UI image moves but it is way slower that the ship , like the transform of gameobject is not the same as the UI image.

    Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ClampUIonPlayer : MonoBehaviour
    7. {
    8.     public Image img;
    9.  
    10.     Camera cam;
    11.  
    12.     public Canvas canvas;
    13.  
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         cam = GameObject.Find("FullScaleRadarCamera").GetComponent<Camera>();
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.  
    26.         Vector3 imgVec = cam.WorldToScreenPoint(this.transform.position);
    27.         //img.transform.position = imgVec;
    28.  
    29.         Vector2 movePos;
    30.  
    31.         //Convert the screenpoint to ui rectangle local point
    32.         RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, imgVec, canvas.worldCamera, out movePos);
    33.         img.transform.position = movePos;
    34.         Debug.Log(movePos);
    35.  
    36.     }
    37.  
    38.  
    39.  
    40. }


    Player's ship contains this script. UI image is placed inside the Canvas. The UI image is displayed only on the second samera (I am doing all of this so that I can display the UI images of players in my game, in minimap. But I need to use UI images, not just the second camera, because I need to have the hover over effect on the players on the minimap. The hover over effect cant seem to work in second camera, with raycast, for standard game objects).

    Here is my scene: https://photos.app.goo.gl/UoLtTu4mKXffq7UBA

    Here is a video of my problem. The selected ship is the green box, the other ships, faster one, is the red box. I control them both at the same time. UI icons dont move 1:1 with these ships.https://photos.app.goo.gl/hXu8aV2UFfcZzBoC6

    Thank you for any help.

    I have tried this solution to the problem https://stackoverflow.com/questions/45046256/move-ui-recttransform-to-world-position I copied the script as it was but the UI images wont move separately , they move as one for some reason.
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    687
    The first part of the movement
    Code (CSharp):
    1. Vector3 imgVec = cam.WorldToScreenPoint(this.transform.position);
    Is all you should need; after that just set the GameObjects location and it should be fine.

    I'm not sure what the whole rectangle thing is about?
     
  3. Adam_Benko

    Adam_Benko

    Joined:
    Jun 16, 2018
    Posts:
    105
    Nope that does not work. If I do that , the UI images are shifted to the side,they dont hover over the game object when I hit the play, (in inspector they are set with exact the same position as the ships)
    And they move like 3 times slower too.
    Here is the script. (tried this before, thats why I need to use rectangle).


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ClampUIonPlayer : MonoBehaviour
    7. {
    8.     public Image img;
    9.  
    10.     Camera cam;
    11.  
    12.     public Canvas canvas;
    13.  
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         cam = GameObject.Find("FullScaleRadarCamera").GetComponent<Camera>();
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.  
    26.         Vector3 imgVec = cam.WorldToScreenPoint(this.transform.position);
    27.         img.transform.position = imgVec;
    28.  
    29.          
    30.     }
    31.  
    32.  
    33.  
    34. }
     
  4. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    687
    You can add offsets to the Vector3 to fine tune it to the correct position.

    What speed do the ships move at?
     
  5. Adam_Benko

    Adam_Benko

    Joined:
    Jun 16, 2018
    Posts:
    105
    Not sure what are you asking. The ships accelerate gradually until they reach their top speed, but that takes like 10 seconds.
     
  6. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    687
    Are the health bars always behind? Even if you've just started moving?
    This seems like it's limited by your FPS
     
  7. Adam_Benko

    Adam_Benko

    Joined:
    Jun 16, 2018
    Posts:
    105
    No, it is not limited by my FPS. The health bars are not behind at the beginning. They always match the position of the ship at the start. Only when the ships move, they lag behind , their movement is not 1:1 but 3:1.
    1 square in inspector for the icon is 3 squares for ship.