简体   繁体   English

如何让两个物体在碰撞时粘在一起? (统一 3d ) C#

[英]How can I make two objects stick together on collision? (unity 3d ) C#

In a game I'm making I'm trying to make two Game Objects stick together on collision.在我正在制作的游戏中,我试图让两个游戏对象在碰撞时粘在一起。 I've tried making the first a child of the other, so that when the parent moves the child moves with it.我试过让第一个成为另一个的孩子,这样当父母移动时,孩子也会随之移动。 But when I do that the child teleports, and his scale changes( i know it has something to do with World-location/Local-location and World-scale/Local-scale. The child's position and scale change in relative to the parent's position and scale).但是当我这样做时,孩子会传送,并且他的比例会发生变化(我知道这与世界位置/本地位置和世界比例/本地比例有关。孩子的 position 和比例变化相对于父母的 position和规模)。 But I don't know how to solve it.但我不知道如何解决它。 If anyone could help I would appreciate it.如果有人可以提供帮助,我将不胜感激。 (it doesn't have to be parent-child related, I just need a clean fix) (它不必与父子相关,我只需要一个干净的修复)

Reparenting is the default solution here. Reparenting 是这里的默认解决方案。 If you expereincing unexpected behaviour with that, its ususally a sign that you are using non-uniform scale somewhere in either of the parent chains.如果您遇到意外的行为,通常表明您在任一父链中的某处使用非均匀比例。 Best practice is to never use scales that have different x, y, z factors.最佳做法是永远不要使用具有不同 x、y、z 因子的比例尺。 If you need that to change shape of the box, make sure that you scale the box only, and have a dummy parent, to which you reparent your 'attaching' object.如果您需要它来改变盒子的形状,请确保只缩放盒子,并有一个虚拟父级,您将“附加”object 重新设置为父级。 Having a non uniform scale somewhere up in the chain (ie reparenting to an object that is non uniformly scaled) will skew rotation/scale pairs down the chain, and while this might give the desired effect when only one object is involved, it may bite you when reparenting.在链的某处有一个不均匀的比例(即重新成为非均匀缩放的 object 的父级)将使旋转/缩放对在链中倾斜,虽然当只涉及一个 object 时这可能会产生预期的效果,但它可能会咬人你在养育子女时。

Alternativelty, if that fails to solve your problem for any reason, in newer versions of Unity there is a component called ParentConstraint, which should enable you to achieve the same effect Alternativelty,如果由于任何原因无法解决您的问题,在较新版本的 Unity 中有一个名为 ParentConstraint 的组件,它应该使您能够达到相同的效果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM