Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Child objects cause parent object to float up

Discussion in '2D' started by variax20, Sep 1, 2023.

  1. variax20

    variax20

    Joined:
    Apr 10, 2020
    Posts:
    5
    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

    upload_2023-9-1_8-14-55.png

    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:
    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.
    Code (CSharp):
    1. private void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         if (carrying)
    4.         {
    5.             return;
    6.         }
    7.  
    8.         if (collision.gameObject.CompareTag("Parcel")) {
    9.             collision.gameObject.transform.position = anchor.transform.position;
    10.             collision.gameObject.transform.SetParent(anchor,true);
    11.             collision.gameObject.GetComponent<Rigidbody2D>().isKinematic = true;
    12.             carriedObject = collision.gameObject;
    13.  
    14.             carrying = true;
    15.             touchingParcel = true;
    16.         }
    17.     }

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

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


    Thanks
     
    Last edited: Sep 1, 2023
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    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
     
    variax20 likes this.
  3. variax20

    variax20

    Joined:
    Apr 10, 2020
    Posts:
    5
    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 :) Thank you so much for your help, you've just made my weekend! :D I hope you have a brilliant day!
     
    Cornysam likes this.
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Happy to help and glad you got it fixed. Have a wonderful day as well
     
    variax20 likes this.