0

I'll explain my issue (My English is a little lame sorry).

I have a Player ( Camera) who is able to move in a Unity Scene. In this scene there is some GameObjects. Those GameObjects May be too far for the camera to see. What I want to achieve is to have an UI Image that appear in the direction of every objects. Even if the objects are too far.

Example : The player stand at a position. In front of him there is an object that is far. I want to have a GUI on screen who says "there is an object at 200 m ". If there is an object behind him I dont want anything to appear. But if he turns back, it will appear cause the player is in the direction of the said GameObject.

I really hope I made myself clear. Please tell me if you need any further explanations. Thank You community !

Blair
  • 6,623
  • 1
  • 36
  • 42
tinkz
  • 100
  • 2
  • 17

3 Answers3

2

Answering to an old question of myself :

Here's what I did :

if (markIsOp)  //Check if it's worth calculating
{

       //Calculs the Viewport Position of the object
        Vector3 ViewportPosition = Camera.main.WorldToViewportPoint (transform.position);

        //Calculs it's Unity Canvas position (O.5f because UI anchor is middle)
        Vector2 WorldObject_ScreenPosition = new Vector2 (
                                                ((ViewportPosition.x * mainCanvasRect.sizeDelta.x) - (mainCanvasRect.sizeDelta.x * 0.5f)),
                                                ((ViewportPosition.y * mainCanvasRect.sizeDelta.y) - (mainCanvasRect.sizeDelta.y * 0.5f)));

        //Making sure it's forward (markRect is my UI Element's RectTransform)
        if (ViewportPosition.z > 0)
            markRect.anchoredPosition = WorldObject_ScreenPosition;
}   

Thank you for your responses. Have a nice one

tinkz
  • 100
  • 2
  • 17
0

You could as well use Camera.WorldToScreenPoint to calculate the "UI-Position" of your GameObjects and then calculate the distance using z-Coordinates of Gameobjects and Camera.

f.b.
  • 490
  • 1
  • 5
  • 17
  • Hey, thanks for the answer. Here's a bit of my code : Vector3 tempPos1 = arCam.WorldToScreenPoint (pos); tempPos1.z = -40; img1.rectTransform.localPosition = tempPos1; pos is the position of the gameObject. I lock the Z position to not count the distance of the object with is irrelevant to display the UI. Anyway, that does Display UI but not at the proper place, I dont know what I'm doing Wrong. In any case thank you for your time mate – tinkz Dec 02 '15 at 14:04
-1

Use a raycast from the player to the game object, and calculate the difference.

christopher clark
  • 2,026
  • 5
  • 28
  • 47
  • What do you mean the difference ? I believe that it's not enough to display UI at the right spot ? Sorry, I'm new to all of this. – tinkz Dec 01 '15 at 17:04
  • No need for raycast, just Vector3.Distance(objA, objB); – Everts Dec 02 '15 at 13:33
  • Thanks guys, this works nicely for the distance part. My biggest issue is about displaying UI in the right direction of the object – tinkz Dec 02 '15 at 14:02