x


How can I get the 2D Position of a 3D gameObject

How Can i retrieve a 2D screenspace position for a 3D gameobject?

I'm trying to have a floating GUI.Label rollover identifying the object under the mouse position.

more ▼

asked Dec 03 '09 at 10:56 PM

davebuchhofer gravatar image

davebuchhofer
492 7 11 24

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Camera.WorldToScreenPoint(position : Vector3)

Keep in mind, Screen space has Y space going from bottom (0) to top (screen.height). GUI space y values go from top (0), to bottom (screen.height).

Although for hover effects on geometry you may want to use MonoBehaviour.OnMouseOver instead, and then just sample the mouse position.

more ▼

answered Dec 03 '09 at 11:05 PM

Brian Kehrer gravatar image

Brian Kehrer
2.8k 9 11 50

Thank you, the OnMouseOver is what i'd been playing with after i asked the question and it does appear to work, but requires the Geometry to have a collider, and the script on each object, I think a 'manager' type approach using the WorldToScreenPoint will fit my needs better, thank you!

Dec 04 '09 at 12:31 AM davebuchhofer
(comments are locked)
10|3000 characters needed characters left

Here is An OnMouseOver() version that will hover a label over the mouse of the object under the mouse, I went with this one because there is an apparent tradeoff between using a Raycast check and the OnMouseOver, in that the raycast can still be triggered while you are clicking through a GUI, whereas the OnMouseOver stuff takes care of that for you?

using UnityEngine;
using System.Collections;

public class RolloverLabel : MonoBehaviour
{
    private bool ShowRollover;
    private Rect objRect;
    private Vector2 MousePos;
    //move it away from the mouse cursor
    public Vector3 offset = new Vector3 (16, 16, 0);

    void Start()
    {
        ShowRollover = false;
        objRect = new Rect(0, 0, 200, 35);
        MousePos = new Vector2(0, 0);
    }

    void OnMouseEnter() { ShowRollover = true; }
    void OnMouseExit() { ShowRollover = false; }

    void OnGUI()
    {
        if (ShowRollover)
        {
            //this will place the label on the objects local 0,0
            //objPos = Camera.main.WorldToScreenPoint(transform.position);

            //this will place the label on the mouse cursor
            MousePos = Input.mousePosition + offset;
            objRect.x = MousePos.x;

            objRect.y = Mathf.Abs(MousePos.y - Camera.main.pixelHeight);
            GUI.Label(objRect, gameObject.name);
        } 
    }
}
more ▼

answered Dec 07 '09 at 09:56 PM

davebuchhofer gravatar image

davebuchhofer
492 7 11 24

You sir, rock my world. Thanks for this :)

Aug 19 '12 at 05:20 AM SpencerS
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5093
x3694

asked: Dec 03 '09 at 10:56 PM

Seen: 7765 times

Last Updated: Aug 19 '12 at 05:20 AM