- Home /
Coding Advice Needed: Building a 2D ship editor
I'm trying to build a 2D ship [in-game] editor, where a player will select parts from collection and assemble them in a drag and drop interface. A player will start with an initial part (say hull) that has hard points (attachment points) on it where other parts may be attached. These in turn may also have hard points where further part attachments may occur. Parts will "snap" to the hard points when dragged near them. The end result will basically be a tree (hierarchy) of parts, where the root node is the base part (hull in this case) and first children are the ones directly linked to the part, and each part having possible child parts itself. Eventually I want to let the player then use/fly the finished product.
I've gone through several tutorials (see below) on how to use the new EventSystem to allow for drag and drop, but I'm still at a loss on how to create the "hard points" or how to make the sprites "snap" to them. I'm able to create "slots" where parts could be placed using tutorials aimed at card games, etc., but nothing like what I'm trying to do where a dynamic object is basically being built up. I need a coding strategy that will allow me to create various prefab parts that can be assembled in limited ways. I feel like I'm really close, but I'm missing the final bit on how to script up the hard points and creating code to "snap" to these hard points.
TL;DR Version:
I need: A way to dynamically assemble 2D sprites into a dynamic object while retaining the assembly hierarchy.
My thoughts: Script (Class) with List of Points (or something more complex to handle orientation as well) defining what the valid connection points are, as well as handling the connection ("snapping to") of other parts. I'm not sure how to do this part. :)
Can anyone help me?
Tutorials used so far: "Unity UI Drag and Drop Tutorial" https://www.youtube.com/watch?v=c47QYgsJrWc
"UI PANES, PANELS AND WINDOWS" https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/panels-panes-windows
I would suggest writing a "Ship-Part" class that includes a list of gameobjects. You could then make prefabs of each part and place empty gameobjects where you wanted the connecting points to be. Use the inspector to add those hardpoint objects to the list, and then you can reference the positions of the hardpoints through script.
Answer by rageingnonsense · Apr 07, 2015 at 10:54 PM
Add empty gameobjects for your hard points and attachment points to the prefab. Then when dragging, check to see which hardpoint is closest. If you are within a certain distance of that hardpoint, snap the part's position to the correct location.
Your answer
Follow this Question
Related Questions
Drag and Drop and snap between the edges! 1 Answer
Editor drag&drop override 1 Answer
UI drag and drop 1 Answer
Simple drag and drop - no physics 4 Answers
How to drag and drop in 2D game 0 Answers