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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to connect a "PickUp" to my Player's GameObject

Discussion in '2D' started by SFDCIVimyProject, Nov 21, 2015.

  1. SFDCIVimyProject

    SFDCIVimyProject

    Joined:
    Oct 21, 2015
    Posts:
    9
    The main Character in my game flies around and needs to pick up various gameObjects Tagged "PickUp" that are randomly placed in my scene.

    When my Character collides with the PickUps I would like them to attach to an "AttachmentPoint" (which is a child of my Character and only has a Transform Component).

    My understanding is that once I detect the collision all I should need to do is attach the transform as a child of the "AttachmentPoint" and it should 'stick' to the character at the proper position.

    Here is the code I have
    void OnCollisionEnter2D(Collision2D coll) {

    if (coll.gameObject.tag == "PickUp") {
    coll.transform.parent = transform.GetChild(0);

    When I Play the game, I can see in the hierarchy that my PickUp is attached as a child to the AttachmentPoint, but it doesn't attach to the Character as it flies around.
    However, even while I'm flying around it jumps slightly trying to change its position.

    How come the PickUp isn't attaching to my Character?
     
  2. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    Do you have a rigidbody attached to the PickUp? You may need to disable it and any colliders after collision. You may also need to set the transform.position of the PickUp object to 0, 0, 0 after you parent it, since its position is in world space prior to the collision, but parenting it will make its position local to the parent transform.
     
    Last edited: Nov 21, 2015
  3. SFDCIVimyProject

    SFDCIVimyProject

    Joined:
    Oct 21, 2015
    Posts:
    9
    Thanks, that makes sense. Unfortunately, you can't disable a RigidBody2D so I destroyed it instead.
    Here is what I came up with:
    coll.transform.parent = transform.GetChild(0);
    GameObject pickUp = GameObject.Find("PickUp");
    Destroy(pickUp.GetComponent<Rigidbody2D>());
    pickUp.transform.localPosition = newVector3(0, 0, 0);

    It's a bit taxing on the CPU but it is now attaching properly.

    The problem now, is that I would like the PickUp to maintain its weight so that when my Character is flying around with the PickUp it pulls him down more. By destroying the Rigidbody2D I lose that option... is there a solution???
     
  4. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    Oh I forgot about Rigidbody2D not having an enabled flag... sorry about that. You may not even need to remove it. If there are any forces being applied to the pickups in any scripts, just make sure that doesn't happen after its been picked up. You may also want to set any colliders attached to the PickUp to isTrigger, unless you want them to continue colliding with the environment.

    Here is an example :

    Code (CSharp):
    1. public class ExamplePlayerClass : MonoBehaviour
    2.     {
    3.         public Transform PickupAnchor;
    4.  
    5.         public void OnCollisionEnter2D(Collision2D coll)
    6.         {
    7.             if (coll.gameObject.tag == "PickUp")
    8.             {
    9.                 coll.gameObject.transform.SetParent(PickupAnchor);
    10.                 coll.gameObject.transform.position = Vector3.zero;
    11.                 coll.gameObject.GetComponent<Collider2D>().isTrigger = true;
    12.             }
    13.         }
    14.     }