- Home /
What's the best way to draw a 'pointer' from GUI layer to 3d scene?
At runtime (not Editor), and barring GL or some add-on, what's the best way to draw, say, a triangle that has two vertices anchored on a GUI element, and one vertex in the 3d scene?
I get the GUI element's screen rect and convert that to world coords (using Camera.main). They all look reasonable. Then I try to create a mesh consisting of 2 triangles (back to back) using those 3 coordinates. MeshRenderer and everything, but I'm not seeing anything in the scene.
Am I missing some 2D drawing class I should be using instead (convert the 3D point to screen space and draw my pointer in the GUI layer?)
Is there a better way? BTW: It doens't have to be a triangle, just something nice to tie the 2D to 3D.
I suppose. Info on the HUD that points to (and tracks) object(s) in the 3D scene.
Answer by DaveA · Mar 31, 2011 at 11:29 PM
Well it took some futzing. I'm this close (holding index finger very near thumb) away from selling this on the Asset Store, but here's the nugget anyway, I'll let you fill in the details:
in LateUpdate():
var p0 : Vector3 = targetTransform.position;
var p1 : Vector3 = Camera.main.ScreenToWorldPoint (Vector3 (left, top, Camera.main.nearClipPlane));
var p2 : Vector3 = Camera.main.ScreenToWorldPoint (Vector3 (left+20, top, Camera.main.nearClipPlane));
Answer by Zib Redlektab · Mar 26, 2011 at 02:07 AM
I can't speak from experience, but you probably want Gizmos. http://unity3d.com/support/documentation/ScriptReference/Gizmos.html
Answer by Owen-Reynolds · Mar 26, 2011 at 07:34 AM
Are you seeing the triangle in the Inspector for the Mesh during a run?
One thing that bit me: setting mesh.vertices = new Vector3[3]; mesh.Vertices[0]=...'
said it gave me a 3-vert, 2 face triangle but didn't really. You have to set mesh.Vertices
to a complete, pre-existing array (which is how the docs do it.)
If you set your corners to the actual world coords, you'll have to set the Transform to (0,0,0). Problem is, Unity now thinks your Mesh is at (0,0,0) and won't draw if that's behind you, too far away... . If you run towards the (0,0,0) corner and it pops up, that was the problem.
I would probably set the Transform to your first corner, so the Mesh counts as being in view, then set the other corners to (v2-v1) and (v3-v1). Actually, before that I'd draw a 0/1 square set 20ish in front of the camera, as a test.
Sounds very promising, will try ($$anonymous$$onday), Thanks
Your answer
Follow this Question
Related Questions
3d Object in GUI Layer 2 Answers
Object to always appear same size on HUD 3 Answers
How can i make changes like in example? 0 Answers
HUD animation with game in background 0 Answers