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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Keep object to the screen.

Discussion in 'Scripting' started by VisualTech48, Apr 22, 2014.

  1. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    Hi, VisualTech48 here. I'm working on my new horror game, but I need some help. I'm making my game's GUI, but since I wanted to make it in 3D, I made object( In side that object there are the other objects (components) of the GUI, the object itself is just a holder for the other object, components of the GUI ) and placed them in the camera part.
    Link of ScreenShot_1
    Link of ScreenShot_2
    Link of ScreenShot_3
    $SlikeZaUnity3.png
    $SlikeZaUnity.png
    $SlikeZaUnity2.png

    The problem is, when I or someone resizes the screen it goes over it. So what I want to do is to check when the screen is resizeing and then a code for it, to position and scales the 3d gui object depending on how the screen is wide and high if posssible.

    Thank you in advance, here is what I have tried, but failed.
    Code (csharp):
    1.  
    2. var Edge1 : GameObject;
    3. function Start () {
    4. Edge1 = GameObject.Find("PlayerGui");
    5. }
    6.  
    7. function Update ()
    8. {
    9. if(Screen.width == SW  Edge1.transform.localScale.z == Postotak )
    10. {
    11.  
    12. }
    13. else
    14. {
    15. Postotak2 = Screen.width / 100.0;
    16. Postotak1 = Edge1.transform.localScale.z / 100.0;
    17. Postotak = Postotak1 * Postotak2;
    18. Edge1.transform.localScale.z = Postotak;
    19. SW = Screen.width;
    20. }
     
    Last edited: Apr 22, 2014
  2. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
    One of the things I always did with 2D GUIs was determine what the scale of change was based off a base resolution, and then adjust my GUI based on the change.
    For instance, I'd start my calculations with a base resolution of 1024x768. I'd have a GUI element with screen position 5x, 25y. Then my resolution is changed to 640x480. I'd get scaler values for x and y based on the change.
    Code (csharp):
    1.  
    2. scalerX = 640/1024; //More technically, Screen.width/1024. Value is 0.625
    3. scalerY = 480/768; //More technically, Screen.height/768. Value is 0.625
    4.  
    I would then multiply the position by the scalers.
    Code (csharp):
    1.  
    2. screenPos.x *= scalerX;
    3. screenPos.y *= scalerY;
    4.  
    The screen position for the GUI element would now be adjusted to the new resolution. I would then scale the GUI itself by the scaler values.

    For 3D, this might still work. Start with your base resolution, and then do
    Code (csharp):
    1.  
    2. //Get the screen position of the 3D object. Returns as a transform
    3. var 3dGuiPos = Camera.WorldToScreenPoint(3dGuiObject.transform.position);
    4. //Convert it to a real screen point
    5. var 3dGuiScreenPoint = new Vector2(3dGuiPos.position.x, Screen.Height - 3dGuiPos.position.y);
    6.  
    3dGuiScreenPoint would now have the technical spot of the screen where the GUI is. I would write this down and store it as a Vector3 as your "base location" for future calculations.

    When you change resolutions, you can then use 3dGuiScreenPoint to adjust to the change in resolution. First get the scalers as before, and then do
    Code (csharp):
    1.  
    2. var newScreenPos = new Vector2(baseLocation.x * scalerX, baseLocation.y * scalerY);
    3. var new3dPos = Camera.ScreenToWorldPoint(newScreenPos.x, newScreenPos.y, baseLocation.z);
    4.  
    new3dPos should have the revised position of the GameObject.

    Please note that I have never tried to do 3D GUI and the above is untested for 3D, so let me know how it goes. Don't worry about scaling for now. If this works for re-positioning, then scaling can be done since it's at least in the right spot.
     
  3. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    I have tried, but even before I can start it, many errors occurred. There is something mising and some part is just not right like "var screenPosx *= scalerX;" for ex. What does he have to * when the screenPosx is not even declarded. Thank you for your help but I just don't understand.

    The current code:
    Code (csharp):
    1.  
    2. ar scalerX = Screen.width/1024;
    3. var scalerY = Screen.height/768;
    4. var screenPosx *= scalerX;
    5. var screenPosy *= scalerY;
    6. //
    7. var baseLocation = Edge1.transform.position;
    8. var GuiPos = PlayerCamera.WorldToScreenPoint(Edge1.transform.position);
    9. var GuiPosx = GuiPos.x;
    10. var GuiPosy = GuiPos.y;
    11. var GuiScreenPoint = new Vector2(GuiPosx, Screen.height - GuiPosy);
    12. var newScreenPos = new Vector2(baseLocation.x * scalerX, baseLocation.y * scalerY);
    13. var new3dPos = PlayerCamera.ScreenToWorldPoint(newScreenPosx, newScreenPosy, baseLocation.z);
    14. Edge1.transform.position = new3dPos;
    15.  
     
    Last edited: Apr 23, 2014
  4. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
    What are the errors? I'm not completely fluent in JavaScript/UnityScript so I could have done something wrong with syntax.
     
  5. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    Honestly? There were a lot of errors, and I managed to fix couple of them. Both of the ScreenPosx and y are wrong, and *= cannot be used. There is no base location acctualy too, I'm not sure what to make of that, is it the base location of the camera or the obj or something else? NewScreenpos.x and y need to be properly declarded, Unity is giving some error. And if you could explain the scalers a bit, how does it work?