Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align "tracks" or modular objects in Unity ?

I'm developing a simple game, where user can place different but modular objects (for instance: tracks, road etc).

My question is: how to match and place different object when placed one near the other ?

My first approach is to create an hidden child object (a box) for each module objects, and put it in the border where is possible to place other object (see my image example), so i can use that coordinates (x,y,z) to align other object.

But i don't know if the best approach.

enter image description here

enter image description here

Thanks

like image 266
stighy Avatar asked May 01 '18 18:05

stighy


1 Answers

Summary:

1.Define what is a "snapping point"

2.Define which is your threshold

3.Update new game object position

Little Explanation

1. So I suppose that you need a way to define which parts of the object are the "snapping points". Cause they can be clear in some examples, like a Cube, where the whole vertex could be snapping points, but it's hard to define that every vertex in amorphous objects.

A simple solution could be the one exposed by @PierreBaret, whic consists in define on your transform component which are the "snapping points". The other one is the one you propouse, creating empty game objects that will act as snapping points locations on the game object.

2.After having those snaped points, when you will drop your new gameObject, you need to define a threshold, as long as you don't want that every object snaps allways to the nearest game object.

3.So you define a minimum distance between snapping points, so if your snapping point is under that threshold, you will need to update it's position, to adjust to the the snapped point.

Visual Representation:

enter image description here

Note: The Threshold distance is showing just ONE of the 4 current threshold checks on the 4 vertex in the square, but this dark blue circle should be repilcate 3 more times, one for each green snapping point of the red square

Of course this method seems expensive, you can make some improvements like setting a first threshold between gameobjects, and if the gameObject is inside this threshold, then check snapping threshold distance.

Hope it helps!

like image 138
Lotan Avatar answered Sep 29 '22 13:09

Lotan