Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug parenting a rotated object

Discussion in 'Physics' started by ayenawine, Nov 22, 2023.

  1. ayenawine

    ayenawine

    Joined:
    Nov 6, 2023
    Posts:
    11
    I recently made a post where I solved an issue I was having with allowing a player to pickup a rigidbody box (by having a hidden underlying box collider object): https://forum.unity.com/threads/rigidbody-carrying-a-rigidbody.1518217/#post-9484507

    I solved most of my issues there. However, I now have a new issue, when the rigidbody object is tipped over, the object has a weird offset to the desired pickup/hand location relative to the player. I've recorded an example here:


    Here is my pickup script:

    Code (CSharp):
    1.     void PickupObject() {
    2.         Debug.Log("Picking up object");
    3.         GameObject currentGameObject = currentObjectRB.transform.Find("WoodenBox_noRB").gameObject;
    4.  
    5.         currentObjectBC = (BoxCollider)currentGameObject.GetComponent<Collider>(); // get the collider we want to pickup
    6.      
    7.         currentObjectBC.gameObject.SetActive(true); // activate the collider we want to pickup
    8.      
    9.         currentObjectBC.transform.SetParent(this.transform); // pickedup object now belongs to player
    10.         currentObjectBC.transform.position = pickupTarget.position; // sets the position of the object to your hand position
    11.      
    12.  
    13.         currentObjectRB.gameObject.SetActive(false); // deactivate the rigidbody object
    14.     }
    15.  
    16.     void DropObject() {
    17.         Debug.Log("Dropping object");
    18.         currentObjectRB.gameObject.SetActive(true); // reactivate the rigidbody
    19.         currentObjectRB.transform.position = currentObjectBC.transform.position; // move the RB object to the BC current location
    20.         currentObjectRB.transform.rotation = currentObjectBC.transform.rotation;
    21.  
    22.         currentObjectBC.transform.SetParent(currentObjectRB.transform); // return the BC to the RB object
    23.         currentObjectBC.gameObject.SetActive(false); // deactivate the now hidden collider object
    24.  
    25.         currentObjectRB = null;
    26.         currentObjectBC = null;
    27.     }
    I have tried moving the functions around and setting the position before and after the box collider object is activated, but cannot seem to fix the issue. The collider itself is square, and it appears to be grabbing the bottom of the box, rather than the center, but I can't figure out why.
     
    Last edited: Nov 22, 2023