- Home /
How To Make GUI/Dragable Object - Att: Video
Hello,
Basically, Right now I have a very simple GUI with a button. What I would like to do is to have that button to be able to be dragged to the scene, in where it will turn into a game object that follows the mouse.
Watch This To See What I Mean: http://olliejones.com/GUIHelp.mov
Like an inventory, but the reversal. So instead of picking up things from scene. I want to be able to drop onto scene from GUI. Preferably with a grid snap feature such as the one shown in the video.
Please help, or point me in the right direction.
Thanks
It doesn't have to be a button in the GUI - Later on, it will soon be a texture of some sort *
I haven't - I mealy edited it slightly and placed a bounty on it.
Answer by Kourosh · Dec 06, 2010 at 06:28 PM
Oliver, instead of pasting code, I thought it would be more helpful to send you the scene I've created so that you would understand it better. So I've sent it to your email.
Answer by _Petroz · Dec 02, 2010 at 09:05 AM
Break it down:
- Drag and store which item was dragged
- Detect where it is dropped
- Convert drop location from screen to world coordinates
- Snap the 3d position
- Create an object based on which item was dragged
Here are some tips:
setup: Use a GUITexture so you can use OnDrag, store a reference to the prefab you want to create inside that object.
- store the Prefab reference associated with the button inside OnDrag in a variable visible from (5.)
- Use Input.GetMouseButtonUp
- Use Camera.ScreenToWorldPoint
- Use the modulus operator for x and z coordinates
- Use Instantiate if your object is a prefab
This sounds like a good approach - I've only been scripting for about a week and I'm poor at making up code. So far I have tried receiving the mouses position according to the scene and having a prefab follow it but no luck there. Also tried initiating a prefab where I click - but that didn't even work too
You can use print statements to check that code is reachable and also to test values. Have a look at the parts which are broken, and ask yourself 'is this code getting executed?' and 'is this value what i expect?', then use print statements to confirm.
Yes, but it does not explain the scripts - I'm really bad at scripts - please help me!!
What I have given you should get you started. As requested I have "pointed you in the right direction". I don't mean any disrespect but we won't write your code for you. $$anonymous$$ake a start and ask specific questions if you get stuck on a certain step. Coding is not so hard, just give it a go and you'll be surprised how quickly you become quite good. :)
Answer by Uriel_96 · Dec 02, 2010 at 02:19 AM
Im not very sure but this is experimentation that maybe will work
if ( Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 100.0)){
hit.collider.transform.tag = "drag";
//here you put when you clic to the GUI
}
}else{
if ( Input.GetMouseButtonUp(0)){
if (Physics.Raycast (ray, hit, 100.0)){
hit.collider.transform.tag = "drop";
//Here you put when you clic and that construction its build
}
}
}
Recomendation: For this its not sure to put a GUI in the script, it will be better if you do it in the scene.
why has people voted this one down? please remember to explain when you dont agree.
Your answer
Follow this Question
Related Questions
On Click paramaters disappear from button prefab? 5 Answers
Loading scene with a prefab GUI button? 1 Answer
GUI Creates a prefab? 1 Answer
GUI and buttons 3 Answers