Search Unity

Parenting an two rigid bodies

Discussion in 'Physics' started by Sydious, Mar 13, 2015.

  1. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    I have a rigid body game object that when it collides with another, it parents to that game object.

    Simply as follows:

    Code (JavaScript):
    1. riding = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsRideable);
    2.  
    3. function OnCollisionEnter2D(coll: Collision2D) {
    4.  
    5.     if (coll.gameObject.tag == "Ride_Surface_01" ){
    6.        
    7.         if(riding){
    8.             transform.parent = coll.transform;
    9.             transform.rigidbody2D.gravityScale = 0;
    10.             anim.SetBool("Ride", true);
    11.            
    12.         }
    13.                
    14.     }
    15.  
    16. }
    I have verified that both riding has gone true and that the object gets parented to the object it collided with.

    Since the object is now parented to the other, I expected it to move with the partent object. instead it continues to fall from gravity and just slides of the object.

    How can I get it to stick to the parent object?
     
  2. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    Yes, it doesn't work like that.

    You have two choices:

    If you want the object to get stuck with the parent then set isKinematic to true.

    If you want the object to follow the parent but still bounce around then don't parent and use a joint (like distance, or spring) to keep them together.

    But never parent two rigidbodies together unless one of them is a kinematic rigidbody.
     
    Kiwasi likes this.
  3. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    Gravity *is* a law :D
     
  4. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    I am a little confused at that statement since my Char object is set to isKinematic= true.

    Are you saying it should work to parent the objects IF one is set to isKinematic = true?

    Sorry for beating a dead horse, but I can't get this to work after all the posts I have read.
     
  5. sleekdigital

    sleekdigital

    Joined:
    May 31, 2012
    Posts:
    133
    I believe he means that the child object would have to be set to kinematic. Basically, physics makes the hierarchy meaningless in terms of how the game objects move. Make the child kinematic so it won't try to move based on physics OR you can use joints instead of parenting.
     
  6. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    If you want the child not to be affected with gravity and fall down, then make it into kinematic.
    Now if the parent is just a flat platform, and the child is on top of it, then it will move with the parent.
    But the moment the platform tilts, the child rigidbody will start to slide away and fall.
    Edit: I also read somewhere in the forums that coll.CompareTag("tagname") is more efficient than comparing the string with .tag==" "
     
  7. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    coll.CompareTag("tagname") doesn't seem to work with OnCollisionEnter2D