- Home /
How would I align edges on instantiated prefabs?
Hey all, I have been wanting to make a track maker as a feature in my game, I have the sections that align perfectly as you can see in the pic.
I wanted to know how I would either weld each of the vertices so they snap together, or more realistic, snap the edges together.
I will have it so the user can tap a button, and it will automatically spawn the next track section aligned with the last one.
Any ideas?
Cheers, Tim.
Ive don't this befor, basically, when you instantiate the object (object instantiation has to be a var) Change the transform to snap it, basically, instantiate, make a var that gets in transform, then make a var, in update, make it so it gets old war, puts it in new var, and rounds it to a unit.
when you make the track pieces in your 3d editor take not of its size, i make all my models tile-able because they conform to there size, e.g each models s size are multiple s of 5 , for tracks i would say 20;
lets say you want to spawn a piece that is 20x20 units and the first pieces's transform is [0,0,0] and its size is also 20x20. the position for the new piece would be [20,0,0] if it goes right, [-20,0,0] if it goes left, [0,0,20] if it goes forward and [0,0,-20] if it goes backwards.
heheh I didn't think of modeling them like textures.
So..all the user will need to do is rotate 90 degree increments :)
I tested it in photoshop with pretend objects (as pictures of corners), and works great thanks!
Answer by jtok4j · Apr 19, 2019 at 05:04 AM
5+ years and no answer... Rats... I was hoping for the answer to this too... Keep On Creating!
Answer by DarkShadowsX5 · Apr 19, 2019 at 06:37 AM
6 years later... (a new player has appeared!)
One way is to place child empties at the center edge of each end of the track and align them by using the empties transform coordinates. then you can lock it onto the empty Objects position.
To make a snap feature you could either raytrace for the nearest empty within x distance or more simply use a temp sphere object for collision detection then fixate the one you moved to the others position.
Answer by Bunny83 · Apr 19, 2019 at 01:53 PM
Unity always had the vertex snapping feature as far as i can remember (and that goes back to Unity 2.6 in 2011). Just select an object in the sceneview. Now there are two options. Grab the pivot of the object then press and hold "v" to snap that pivot point to another vertex of another object.
However the more useful feature is to first press and hold "v". Now you can actually "grab" a vertex of the selected object and snap it to another vertex.
Note that when you search for "unity vertex snap" in google the first hit is the Positioning GameObjects documentation page in the manual which mentions vertex snapping at the bottom. There are also several videos (like this one from 2013 or an official one from Unity) which explain how vertex snapping works.
The search took me about 10 seconds (since i'm not that fast on the keyboard). Searching for information requires actual searching.
Your answer

Follow this Question
Related Questions
set guiText component to bottom left corner of screen in IDE 1 Answer
Bullets not orienting properly 0 Answers
Align 2 objets edge to edge 1 Answer
How can I align Axis? 0 Answers