Search Unity

Resolved Connecting two objects together when they touch, and make them behave as if they are stuck together.

Discussion in 'Physics' started by TheBulletKin, Dec 6, 2023.

  1. TheBulletKin

    TheBulletKin

    Joined:
    Jan 5, 2022
    Posts:
    12
    Good day to you. I am currently in the process of learning unity, and something I want to try and make for practice is a mini game where you can snap different objects together to create a larger object that you can interact with.

    So far I have a cuboid shape here with two colliders as triggers, which when the other object moves into will fire the trigger specifying which trigger was touched.
    upload_2023-12-6_16-57-7.png
    upload_2023-12-6_16-57-25.png

    below I also have a method which aims to deal with the functionality after this has been triggered. My aim is to parent this selected cuboid to the one on the right using the code below.

    upload_2023-12-6_16-58-29.png

    Code (CSharp):
    1.  public void OnLeftTriggerEntered(Collider collider)
    2. {
    3.      otherObject = collider.gameObject;
    4.      otherObject.GetComponent<Rigidbody>().isKinematic = true;
    5.      otherObject.transform.position = transform.position + snapOffset;
    6.      otherObject.transform.rotation = transform.rotation;
    7.  
    8.      otherObject.transform.parent = transform;
    9.      Debug.Log(gameObject.name + " has entered the left trigger");
    10.  
    11.  
    12. }
    With this, the other plate will correctly follow the transformation of the first plate...
    upload_2023-12-6_16-59-28.png

    But the other object now behaves like it doesn't exist.

    upload_2023-12-6_17-0-24.png

    I was hoping to make it so that this other shape (which eventually will snap onto the face, and not float a few metres away) will also be considered in the main collider so that the whole object will lay flat and not fly through the floor.

    Would could I potentially try to implement this functionality?
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    A dynamic Rigidbody should only be in the root of an object, and all the parts of the object are (ideally) rigid, that is, they don't change positions or rotations relative to that root.

    If you want two Rigidbody objects to fuse into a single rigid object, then you choose one to be the parent and make the other into a child. The parent's center-of-mass should be re-calculated accordingly to encompass the total new shape of the combined object. The parent's mass should be re-calculated accordingly to encompass the total mass of the new object. And finally, the child's Rigidbody component should be destroyed.

    If you need moving assemblies of parts, like robot arms or forklifts or doors, then you should be working with Joints. Each separate part is at its own root with its own Rigidbody, but are associated (a.k.a. attached) with each other using various kinds of Joint components. Do not try to parent one Rigidbody to another.

    In other words, if you're trying for Tears of the Kingdom, you want the latter. Joints are like the glue.
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,000
    Remove line 4. You don't want it to be kinematic.
     
  5. TheBulletKin

    TheBulletKin

    Joined:
    Jan 5, 2022
    Posts:
    12
    I ended up doing that since a thread written elsewhere said that child objects needed to have their rigidbodies set to kinematic so that it didn't have it's own individual physics that was disconnected to the parent. If I kept it on then the other object would fall separately, and I'm trying to make it so they stick together and behave as one.

    Is there another way to do that besides changing IsKinematic? Does disabling that to prevent itself from falling by itself limit me?
     
  6. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    If you absolutely must have a Rigidbody that is not at the root of an assembly, yes, you can set it kinematic. There will be consequences of forming rigid assemblies in how collision events are routed to your scripts. That said, if you can destroy the now-useless Rigidbody component, it's better.
     
  7. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,000
    Ah yeah, after making the object a child you should remove the rigidbody from the child object.
     
  8. TheBulletKin

    TheBulletKin

    Joined:
    Jan 5, 2022
    Posts:
    12
    Thanks ever so much for the response. To a degree I guess I suppose I am making something like Tears. In regards to that solution, I'm not entirely sure how I would go about doing those things. At the moment i've got the plane that has the colliders on the side acting as the parent, and from what I've found the main way you disable a rigidbody is through setting IsKinematic to true, although that doesn't outright destroy it. The centre of mass doesn't seem to change while doing this, so is that something I'd need to recumpute manually since It still behaves as if just one cuboid is there?
     
  9. TheBulletKin

    TheBulletKin

    Joined:
    Jan 5, 2022
    Posts:
    12
    Oh, I found that I was looking in the wrong places. Someone suggested you use the Rigidbody.enabled function but that seems to be depreciated, and the thread after that said it simply wasn't possible.

    But I just found that you can destroy components with a simple Destroy method, which I didn't know before. And wouldn't you know, it works!

    upload_2023-12-6_17-56-57.png

    Thank you halley and Zulo!

    I'll keep on working through this project, more issues are bound to show up. But that's one more hurdle crossed :)
     

    Attached Files:

  10. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    Yes, you have to recalculate. When Rigidbody wakes up, if you left the center of mass alone, it calculates it assuming the mass is equally divided amongst the volumes of all colliders. You can trigger that again with ResetCenterOfMass(). Or you can figure out a better center of mass yourself, if you have more accurate insight into what parts of the assembly are more dense than others.
     
  11. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    Since there's no checkmark next to the name of the Rigidbody component, there's no .enabled either.