- Home /
2D Modular Customization With Snapping
Hey :)
This might be quite a long and ambiguous question to ask, but after looking around a lot I am still pretty stuck so I'll just give it a go anyway :P
The general idea is that the user can click on a module and drag it onto his space ship, to customize it. I am only working on a 2D plane from above, and all the modules at the moment are square with 4 or less connectable sides. The modules will all provide different benefits, and have different requirements. Also just to say that I am writing this project in Javascript :P
Specific tasks I could do with help on:
Picking up and dragging the modules using the mouse.
Placing these modules onto the ship using a grid, so they snap to the other modules.
I honestly have no idea where to begin a task like this, so any help from relevant articles would still be appreciated. But help with the actual writing of the script would be a most useful to me.
Thanks for your time, Magnus :)
Awesome cheers $$anonymous$$ark, that breaks it down nicely. I'll have a thorough read through of that when I have got time later. I'll get back to you, to let you know how I get on :P
Also $$anonymous$$, I meant Javascript. Apologies, I sometimes shorten it to Java forgetting that it's a different thing
Finally, after a large amount of ball-ache, I managed to do it! :)
Just going to say now, if anyone else looking at this in the future wants to do a similar thing, I can put the scripts up for you.
I just cannot be bothered right now :P
its always simple once u wrap your brain around it ;) gl with the rest
Hello magnusm, would you please post your code or pm me with it. I would really appreciate it!
Answer by markedagain · Jul 17, 2013 at 03:46 AM
ok , ill post as comment cause im being lazy to use actual code and i use c#, but i will explain the logic.
so first thing to tackle is your mouse click, the easiest to get going but less favorable is the OnMouse event function attached to the dragable objects. the other would be a single script checking for mouse click and then sending out a raycast from screenToWorld. the latter is favorable because it will help traverse layers and allow u to select objects behind other objects ( ex: your outside transparent wall )
now you have a few choices using the mouse events, prob the simplest to get u going is using the OnMouseStay witch is fired off as long as your mouse is over the object, you shoujold first check if the mouse button is pressed , if so then we are in "drag" mode. from this point you need to start changing the transform.position to match the position of the mouse. in retrospec, u will still have to use a screenToWorld at this point just to get at least the 2 coords out of the mouse. so as long as button is pressed and mouse is over object, keep changing the transform.
once you get all this done, you will now have to get your "snapping" working, for this is relatively simple, only change your transform.position of your object, when the mouse has past half the distance u want to snap to, ie your invisible middle point where u snap to next tile. to figure out exact coord, try to see if u can figure it out, i always use the modules function for this ( 16 % 4 = 0 ) but this is a bit tricky for some, and i cant remeber the longer version off the top of my head.
hope it helps
Your answer
![](https://koobas.hobune.stream/wayback/20220613110233im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
setup a php master server 2 Answers
Need help with WeaponSwitch Script 0 Answers