Child objects cause parent object to float up

Hi guys,

I am making a simple 2D drone game where you control a drone up/down/left/right with the arrow keys and can pick up boxes by touching the top of them. The player has a RigidBody2D and BoxCollider2D

1 = the top of the parcel object which detects when the drone picks it up
2 = the drone moves up when the parcel is collected

the player has the following child objects:
9265638--1296714--upload_2023-9-1_8-16-50.png

Box - has a single SpriteRenderer for drone body (light green color)

Winch - contains:

  • a SpriteRenderer for the part which picks up objects
  • a BoxCollider2D component with IsTrigger enabled
  • a custom Winch script which has an OnCollision method that checks for when the winch object touches a parcel object, e.g.
private void OnCollisionEnter2D(Collision2D collision)
    {
        if (carrying)
        {
            return;
        }

        if (collision.gameObject.CompareTag("Parcel")) {
            collision.gameObject.transform.position = anchor.transform.position;
            collision.gameObject.transform.SetParent(anchor,true);
            collision.gameObject.GetComponent<Rigidbody2D>().isKinematic = true;
            carriedObject = collision.gameObject;

            carrying = true;
            touchingParcel = true;
        }
    }

The Anchor child object is just an empty object to contain the location where the child object will be placed when collected, e.g.

9265638--1296735--drone anchor.PNG

Now, the problem I’m having is that when the winch touches a Parcel object, it adds it as a child and sets the correction location, which is fine. However the drone automatically flys up when an object is picked, which I don’t want.

If I scale the Parcel objects a little smaller, the drone still goes up, but not as fast.

If anyone could help I’d be really grateful :slight_smile:

Thanks

Seems a bit wonky indeed. My first guess is that the colliders are overlapping and both have rigidbodies. You set the parcel position to the exact same position as the drone, or box as you named it in the hierarchy, so the drone and parcel’s colliders are overlapping. The parcel’s rigidbody is set to kinematic so it wont fall and you’re getting a strange physics reaction which constantly pushes up because the parcel technically cannot move but is parented to the drone so it will constantly be at the transform.position (of the drone) you put it at.

Potential solution: Add a empty gameobject called Parcel Position or Pickup Position and have it be a child of the player, and lower it a bit below the box’s collider. Then, make a reference to your script to that new transform and have the parcel still parent to the Box or Player, but set its transform.position to that of the new transform object. If it still does it, lower that transform object a lot and see if it still does it.

There are probably 53.5 different ways to solve this but this is what my brain thought of. Let us know if it doesnt work

1 Like

You were absolutely spot it, it turned out to be that when the box was collected and re-positioned to the anchor, the anchors box collider was overlapping with the drones box collider :slight_smile: Thank you so much for your help, you’ve just made my weekend! :smile: I hope you have a brilliant day!

1 Like

Happy to help and glad you got it fixed. Have a wonderful day as well

1 Like