- Home /
Drag a 2D texture from menu onto 3D grid
I have created a 3d grid game where you drag the playing pieces around using touch on an Android device. This part of the game works great. On the side of the screen I have a 2D menu with images of the playing pieces. I would like to be able to drag these 2D images onto the 3D game grid.
My thought is that I can spawn the 3D object as the player drags the 2D object past the edge of the 2D menu on the side. I have tried a few different solutions without success. Maybe I am going about this the wrong way.
Can anyone recommend an approach to solving this issue?
Answer by yackbladder · Dec 28, 2011 at 03:31 AM
Solution found. Instead of trying to figure when the tile was outside the 2D menu I just do a raycast to detect when it is over the game grid and spawn the 3D game piece at that time.
Answer by Julien-Lynge · Dec 27, 2011 at 07:35 PM
To get from a screen position (e.g. Input.mousePosition) to a real world position requires two steps. First, you convert the screen position to a ray
http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenPointToRay.html
Then, construct a raycaster with that ray and the collider of the game board
http://unity3d.com/support/documentation/ScriptReference/Collider.Raycast.html
This will return a RaycastHit, which gives you the projected screen position on the board. From there you simply use Object.Instantiate to create your object.
Good luck.
Your answer
Follow this Question
Related Questions
Move one GUI window when another is moved? 0 Answers
Fix a GUI's position relative to a 3D object 1 Answer
How to drag my armory from GUI to Terrain? 1 Answer
Place UI element on pointer up position. 1 Answer
GUI Draging 1 Answer