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

Move gameobject to canvas real position on screen

Discussion in 'UGUI & TextMesh Pro' started by Gazzosa, Sep 17, 2019.

  1. Gazzosa

    Gazzosa

    Joined:
    Jul 25, 2019
    Posts:
    3
    Hello!
    Remember when in Crash Bandicoot series you pick up an extra life? Life was an object placed in the game world, but when picked up it goes up and reach the canvas.
    I want to achieve the same result. I got a prefab which I want to go to the same "Score" position in canvas. The problem is that the canvas is not aligned and scaled with the game scene, so it takes its absolute position, as you see in the picture.
    Is there a way to "convert" the position of the canvas in the position relative to what you actually see on screen?

    The prefab is a textmeshpro inside an empty object, because I find transform is easier to use than recttransform. The script attached to the prefab is:
    Code (CSharp):
    1. Transform totalScoreText;
    2. void Start()
    3.     {
    4. //get the position of the target ui element
    5.         totalScoreText = GameObject.FindGameObjectWithTag("UIscore").GetComponent<Transform>();
    6.     }
    7.     void Update()
    8.     {
    9.         transform.position = Vector3.MoveTowards(transform.position, totalScoreText.transform.position, 10 * Time.deltaTime);
    10.     }

    Probably you discussed this kind of problem already but I can't find a short way to explain in keyworlds and search it in the forum.
     

    Attached Files: