Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by itsteveg, Nov 12, 2019.

  1. itsteveg

    itsteveg

    Joined:
    Apr 30, 2018
    Posts:
    9
    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):

    Code (CSharp):
    1.  private void OnMouseOver()
    2.     {
    3.         Vector3 tmpPos;
    4.  
    5.  
    6.         townNameTxt.SetText(thisTown.townName);
    7.         townLocTxt.SetText(thisTown.townMapLoc.ToString());
    8.         townSizeTxt.SetText(thisTown.townSize.ToString());
    9.         Debug.Log(" thisTown " + thisTown.townMapLoc.x + "," + thisTown.townMapLoc.y);
    10.  
    11.         tmpPos =  new Vector3(thisTown.townMapLoc.x, thisTown.townMapLoc.y, 0f);
    12.        
    13.         townPopUP.transform.position = new Vector3(tmpPos.x,tmpPos.y,0f);
    14.  
    15.         Debug.Log("tmpPos " + tmpPos.x + "," + tmpPos.y + " TownName " + thisTown.Name);
    16.         townPopUP.SetActive(true);
    17.     }
    the debug.log looks like this:
    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!
     
  2. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,890
    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.
     
  3. itsteveg

    itsteveg

    Joined:
    Apr 30, 2018
    Posts:
    9
    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!
     
  4. itsteveg

    itsteveg

    Joined:
    Apr 30, 2018
    Posts:
    9
    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?
     
  5. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,890
    Do you have Screen Space: Overlay on the canvas? Add this code to test:

    Code (csharp):
    1. void OnGUI()
    2.    {
    3.        if (!townPopUP.activeSelf) return;
    4.  
    5.        Vector3 screenPoint = Camera.main.WorldToScreenPoint(new Vector3(thisTown.townMapLoc.x, thisTown.townMapLoc.y, thisTown.townMapLoc.z);
    6.  
    7.        GUI.Label(new Rect(screenPoint.x, Screen.height - screenPoint.y, 300f, 20f), "Popup");
    8.    }
    9.  
     
  6. itsteveg

    itsteveg

    Joined:
    Apr 30, 2018
    Posts:
    9
    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.
     
  7. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,890
    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.
     
  8. itsteveg

    itsteveg

    Joined:
    Apr 30, 2018
    Posts:
    9
    So I could try switching the canvas and see if I can figure that out.

    Going to take a look.