Camera, canvas, MouseOver and transform.position, I'm lost

Here is the setup.
I have a map as a background.
I’m using cubes at specific location to use the mouseover behavior in order to have a pop up show information about that location. (the pop up is functioning great and held together in an empty object)
the cubes have an SO containing all their information and specifically a vector2 for the transform.position.

Also note that I have the camera set to orthographic (size 5) because I want to be able to move the camera with click/drag.

Here’s where I’m completely lost/stuck.
I have a mouseover function on the script attached to the cube and it gets it’s location (transform.position) from the SO without a problem, however when the pop-up comes up it appears at a completely different vector2.
When I debug I do get the right x/y values.

I’ve tried looking into WorldToScreen etc… but this doesn’t seem to be the problem.

Relevant code (I think):

 private void OnMouseOver()
    {
        Vector3 tmpPos;


        townNameTxt.SetText(thisTown.townName);
        townLocTxt.SetText(thisTown.townMapLoc.ToString());
        townSizeTxt.SetText(thisTown.townSize.ToString());
        Debug.Log(" thisTown " + thisTown.townMapLoc.x + "," + thisTown.townMapLoc.y);

        tmpPos =  new Vector3(thisTown.townMapLoc.x, thisTown.townMapLoc.y, 0f);
       
        townPopUP.transform.position = new Vector3(tmpPos.x,tmpPos.y,0f);

        Debug.Log("tmpPos " + tmpPos.x + "," + tmpPos.y + " TownName " + thisTown.Name);
        townPopUP.SetActive(true);
    }

the debug.log looks like this:
5167742--512732--upload_2019-11-12_10-54-23.png

and yet my townPopUP (the empty object) comes up at 158.4, 21.12, 49.28…

I’m sure I’m missing something dumb but any help would be appreciated!

A pop up of what? A UI item?

You will need to use WorldToScreenPoint. You can test by setting “Input.mousePosition” for line 13 position and it should show where the mouse is.

Yes it is a UI item. and I realized by trying to do a drag function that indeed the coordinates are in ScreenPoint. Going to try it out now.

Thanks!

Ok now I’m even more confused.

I updated the code to have that line use
tmpPos = Camera.main.WorldToScreenPoint( new Vector3(thisTown.townMapLoc.x, thisTown.townMapLoc.y, 0f));
But now the UI popup goes well into the 10000’s on both X & Y axis.

Thoughts?

Do you have Screen Space: Overlay on the canvas? Add this code to test:

void OnGUI()
   {
       if (!townPopUP.activeSelf) return;

       Vector3 screenPoint = Camera.main.WorldToScreenPoint(new Vector3(thisTown.townMapLoc.x, thisTown.townMapLoc.y, thisTown.townMapLoc.z);

       GUI.Label(new Rect(screenPoint.x, Screen.height - screenPoint.y, 300f, 20f), "Popup");
   }

The Canvas is set to world space with the main camera set for Event.
I have the sorting layer set to a top layer called PopUps.

the OnGui makes the word “Popup” show up exactly at the spots where the towns are setup.

btw thank you very much for helping me through this. learning… learning.

It’s harder with world space canvas. You will need a more complicated calculation. WorldToScreenPoint is only good for 2D canvas’s that match the screen size.

So I could try switching the canvas and see if I can figure that out.

Going to take a look.