0
\$\begingroup\$

I'm trying to make a game where the player can connect objects Lego style with objects becoming children of any object they come into contact with, so if the parent object is moved the child is moved too, but the child object can be dragged away to disconnect it.

What is the best way to go about doing this?

\$\endgroup\$
2
  • \$\begingroup\$ Have you looked into physics queries to capture only nearby objects in specific physics layers, rather than searching all objects in the scene? Stuff like OnTriggerEnter, OverlapSphere, etc? \$\endgroup\$
    – DMGregory
    Feb 28, 2019 at 19:19
  • \$\begingroup\$ You've described a perfect solution, when two objects touch, make one child of another, have you tried that? \$\endgroup\$ Mar 4, 2019 at 16:16

1 Answer 1

1
\$\begingroup\$

OnCollisionEnter will probably work out better in the long run, because it means you don't have to go through every "lego" in your scene.

Without more information, I can't be sure, but I think the problem is both objects are trying to be each other's parent. You need to set a defined order for which object will be the parent object (maybe the one that's lower down in the world, or maybe you have a test to see if the object is the one being dragged) and exit the OnCollisionEnter script immediately in the child object.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .