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

Target Box GUI

Discussion in 'Immediate Mode GUI (IMGUI)' started by FIllbiker, Apr 26, 2011.

  1. FIllbiker

    FIllbiker

    Joined:
    Jun 9, 2010
    Posts:
    118
    I tried to search something about this but with no success.
    I would like to create small target box like here

    for waypoints or targets.
    I have no idea where to start, if it should be GUITexture or something else. Target box should have still the same size (no Z axis movement) and still should be connected with target gameobject (X,Y). If I create GUITexture with this box, I can only set position (X,Y) but if I move, it moves too...I need to connect it to the target.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I'm not exactly sure what effect you are going for here, but you can convert an object's position in the 3D scene to the equivalent screen position using Camera.WorldToScreenPoint. You can then use this screen position to draw GUI elements using, for example, GUI.DrawTexture.
     
  3. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,910
    Here is a script I made that works, except the boxes appear behind the player at 180 degrees also. Anyone know what that is?

    Also, the boxes don't scale well with different resolutions.

    EDIT: You might need this image - http://i.imgur.com/u4Gqe.png

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ItemScript : MonoBehaviour {
    5.  
    6.     public Texture2D boxImage;
    7.     public int selectionBoxW = 256, selectionBoxH = 128;
    8.  
    9.     private Vector3 itemScreenPosition;
    10.     private float distanceToPlayer;
    11.  
    12.     //=======================================
    13.  
    14.     void Update()
    15.     {
    16.         // Get 2D screen position of this item from its 3D world position
    17.         itemScreenPosition = Camera.main.WorldToScreenPoint(transform.position);
    18.  
    19.         // Get distance from this item to the player (assuming player is the main camera)
    20.         distanceToPlayer = Vector3.Distance(transform.position, Camera.main.transform.position);
    21.     }
    22.  
    23.     //=======================================
    24.  
    25.     void OnGUI()
    26.     {
    27.         // Get GUI box position and size
    28.         float posX = itemScreenPosition.x - (selectionBoxW / 2) / distanceToPlayer;
    29.         float posY = Screen.height - itemScreenPosition.y - (selectionBoxH / 2) / distanceToPlayer;
    30.         float width = selectionBoxW / distanceToPlayer;
    31.         float height = selectionBoxH / distanceToPlayer;
    32.  
    33.         // Draw boxImage
    34.         GUI.DrawTexture(new Rect(posX, posY, width, height), boxImage);
    35.     }
    36.  
    37. }
    38.  
     
    Last edited: Oct 17, 2014
    pvpoodle likes this.
  4. Mahtrok

    Mahtrok

    Joined:
    Feb 18, 2012
    Posts:
    2
    If you still have the problem:

    Because the x and y coordinates you get by the targets.transform are the same, if the target is behind the camera or in front of it, so it will always show the texture at the same coordinates.

    I used
    Code (csharp):
    1.  
    2. if (selectedTarget.renderer.isVisible){
    3.    GUI.DrawTexture(and so on);
    4. }
    5.  
    to solve the problem, so the selectedTarget texture is only visible while the targetted object is in sight.
     
    pvpoodle likes this.
  5. isfrseirra259

    isfrseirra259

    Joined:
    Jun 17, 2013
    Posts:
    1
    Just asking, in line 22 of the code, what is the "transform.position"? i know the first one is the position transform of the camera, but what is the transform of the second?
     
  6. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,910
    transform.position on its own means the transform of the GameObject the script is attached to.