World Space to Canvas Space

Hey,

I really need some here here. The camera.worldtoscreenpoint has me completely lost. I’m trying to get UI icons to appear in front or around world space objects. I have no idea why this is so complicated, again, i feel like i’m missing some fundamental knowledge but i can’t figure out what that is.

I have takes this script:

using UnityEngine;
using System.Collections;

public class MyWorldIconPositioner : MonoBehaviour

{
    //this is your object that you want to have the UI element hovering over
    public GameObject WorldObject;

    //this is the ui element
    public RectTransform UI_Element;

    void start()
    {
    }


    void update()
    {
        //first you need the RectTransform component of your canvas
        RectTransform CanvasRect = GetComponent<RectTransform>();

        //then you calculate the position of the UI element
        //0,0 for the canvas is at the center of the screen, whereas WorldToViewPortPoint treats the lower left corner as 0,0. Because of this, you need to subtract the height / width of the canvas * 0.5f to get the correct position.

        Vector2 ViewportPosition=Camera.main.WorldToViewportPoint(WorldObject.transform.position);
        Vector2 WorldObject_ScreenPosition=new Vector2(
            ((ViewportPosition.x*CanvasRect.sizeDelta.x)-(CanvasRect.sizeDelta.x*0.5f)),
            ((ViewportPosition.y*CanvasRect.sizeDelta.y)-(CanvasRect.sizeDelta.y*0.5f)));

        //now you can set the position of the ui element
        UI_Element.anchoredPosition=WorldObject_ScreenPosition;
    }
}

from here How to convert from world space to canvas space? - Questions & Answers - Unity Discussions

but am getting an error about the transform. If anyone has any experience with this I would be so grateful.

Thanks in advance.

Nathan

What error are you getting? I’ve used that kind of code many times and I didn’t get an error.

Have you attached this script to the canvas? Since you are using “RectTransform CanvasRect = GetComponent();”

1 Like

Thanks for your response @Akfly

I’m away from my computer for the weekend but I’ll check on Monday. I had attached the script to the panel within the canvas, so maybe that was the problem, looking for the wrong recttransform or something.

But even that, it wasn’t displaying in the smaller window either. I’ll have to check.

For some reason I’ve always found worldtoscreenposition to be cryptic, I wish there was some functioning examples with instructions.

Hey again,

I’m not getting any errors but nothing is working. Could someone please shed some light on this for me.

What I am expecting is that an icon will be rendered to my screen space canvas that basically floats above the game object that it is connected to via the script above. Is this correct?

I have created a separate canvas to try this and have tried rendering in all three modes with now luck. With both Screen Space Overlay and Camera the icon renders in the centre of the screen as if the script were not even there.

Is there something I am missing, or am I using this in the wrong way? Any help would be most appreciated.

Nathan

I have always used Screen Space - Overlay.

Keep in mind the anchors of the element you want to position, when you use the “-(CanvasRect.sizeDelta.x*0.5f)” it means that the anchor should be centered. If your element is not centered, it will be rendered outside of the screen.

WorldToScreenPosition works well, it’s just a multiplication between 2 matrices (elements on the game are positioned with matrices at low level). Basically, it transforms a 3D Vector in the world to a 2D Vector on the screen. If I remember well, the position 0 on the screen is the bottom left corner, that’s why you do the “-(CanvasRect.sizeDelta.x*0.5f)” for centered anchors. This is just a simlified explanation, but you can get more info about this rendering process here (link). It’s at the bottom, but I recommend reading the whole article to understand that part

1 Like

Ah, it was the anchors… I feel like an idiot now.

Thanks for that link! That’s a great resource.

What was wrong with anchors?