简体   繁体   中英

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). 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. 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. 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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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