如何使“轨道”对齐还是Unity中的模块化对象? [英] How to align "tracks" or modular objects in Unity ?

查看:78
本文介绍了如何使“轨道”对齐还是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 ?

我的第一种方法是为每个模块对象创建一个隐藏的子对象(一个盒子),并将其放在可以放置其他对象的边界(请参见图片示例),因此我可以使用该坐标(x,y,z)对齐其他对象。

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.

谢谢

推荐答案

摘要:

1。定义什么是捕捉点

2。定义您的阈值

3。更新新的游戏对象位置

小解释

1。
因此,我想您需要一种方法来定义对象的哪些部分是捕捉点。
因为在某些示例中它们很清楚,例如多维数据集,其中整个顶点可能是捕捉点,但是很难定义每个顶点在无定形对象中。

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.

一个简单的解决方案可能是@PierreBaret公开的解决方案,其中包括在变换组件上定义捕捉点。
另一个是您所支持的对象,它创建空的游戏对象,这些对象将充当游戏对象上的捕捉点位置。

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。点,当您放下新的gameObject时,您需要定义一个阈值,只要您不希望每个对象都始终捕捉到最近的游戏对象即可。

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。因此,您要定义捕捉点之间的最小距离,因此,如果捕捉点在该阈值以下,则需要更新其位置,以适应捕捉点。

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.

视觉表示:

注意: 阈值距离在正方形的4个顶点上仅显示4个当前阈值检查之一,但是这个深蓝色圆圈应该再重复3次,每个红色正方形的绿色捕捉点一次。

当然,这种方法似乎很昂贵,您可以进行一些改进,例如在游戏对象之间设置第一个阈值,并且如果gameObject在此阈值之内,则检查捕捉阈值距离。

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.

希望它会有所帮助!

这篇关于如何使“轨道”对齐还是Unity中的模块化对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆