Can I snap a game object to the face of another similar to vertex snapping?
Say I have a cube in the scene, and I create a child empty game object for it. I can use Vertex Snapping to snap the empty game object to any of the 8 vertexes of the cube, but I want to be able to snap the game object to any of the midpoints of the edges or midpoints of the faces as well, and also the center of the cube, giving 27 options instead of 8. Is this possible? Thanks!
Answer by conman79 · Oct 10, 2016 at 12:51 AM
You'd need to write a custom editor script to be able to do that for a primitive cube, ie. 8 vertices.
A workaround solution would be to use 8 smaller child cubes under your parent cube. You could then use the child cube meshes for vertex snapping only, and the parent cube for rendering/colliders etc.
Answer by Geometrical · Oct 10, 2016 at 05:13 AM
Have a look at the Mesh class which states that it can create and modify meshes. If I'm correct, you should be able to calculate the points of a face and subdivide it to get those extra vertices. Though I'd warn you that this can take a toll on performance.
Alternatively you could write a MonoBehvariour script which utilizes the [ExecuteInEditMode] attribute (which does what the name implies, of course). In the script you could perhaps cast a ray onto the face of an object and get a point on the face of the object to snap to. This requires the object to have a a collider, which isn't ideal.
All this vague, but I hope it helps.
Your answer
Follow this Question
Related Questions
How to make gameobject appear in center of camera irrespective of it's side?? 0 Answers
Create GameObjects and move them from JSON data 0 Answers
How to handle snapping to an "anchor" point? 1 Answer
Positioning a prefab correctly with Instantiate 3 Answers
Positioning Screen Space UI element in center of a grid square? 1 Answer