Search Unity

Bug Child object not moving together with parent

Discussion in 'Editor & General Support' started by MinhocaNice, Apr 4, 2021.

  1. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    In my FPS game, before I add in the animations I made so when you pick up a weapon on the floor it is set as a child object to the player's camera, this way it should move when the camera moves.

    Yet, for some reason it does not. It only moves when I actually set it's transform in the code instead of moving together with the camera at all times. So I move my player, with my camera, but the gun just sits floating there in the middle of nowhere.

    Here is the code for when the player picks up a weapon (only the important part):
    Code (CSharp):
    1.                         if (InventorySlots [PrimarySlot - 1] == null)
    2.                         {
    3.                             InventorySlots [PrimarySlot - 1] = PickedItem;
    4.  
    5.                             ItemObject.SetActive(false);
    6.                             ItemObject.transform.parent = FPCamera.transform;
    7.                             ItemObject.transform.position = FPCamera.transform.position;
    8.                             ItemObject.transform.rotation = FPCamera.transform.rotation;
    9.                             Gun.Load();
    10.                         }
    I remember this code used to work before without this issue. It also seems that rotating objects doesn't change their gizmos rotation, like everything is hard coded to use global position values? I am using Unity 2019.4.14f1 Personal.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Does your Gun happen to have a Rigidbody on it?
     
  3. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    Yes it does, but it's disabled once picked up:


    Code (CSharp):
    1.         Debug.Log(gameObject.name + " picked up " + AimPoint.transform.name + ";");
    2.  
    3.         GameObject ItemObject = PickedItem.gameObject;
    4.         ItemObject.SetActive(false);
    5.         ItemObject.transform.position = transform.position;
    6.         ItemObject.transform.rotation = transform.rotation;
    7.         Collider ItemCollider = ItemObject.GetComponent<BoxCollider>();
    8.         if (ItemCollider != null)
    9.         {
    10.             ItemCollider.enabled = false;
    11.         }
    12.         Rigidbody ItemRigidbody = ItemObject.GetComponent<Rigidbody>();
    13.         if (ItemRigidbody != null)
    14.         {
    15.             ItemRigidbody.useGravity = false;
    16.             ItemRigidbody.velocity = new Vector3(0f, 0f, 0f);
    17.             ItemRigidbody.angularVelocity = new Vector3(0f, 0f, 0f);
    18.         }
    19.         UsableItem Usable = ItemObject.GetComponent<UsableItem>();
    This script happens before the other portion shown earlier
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Nothing I see here is disabling the Rigidbody.

    You want to set the Rigidbody as kinematic or destroy it.
     
    iiJacob likes this.
  5. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    Why would that matter anyway? I set it's gravity to false and the velocity to 0, and I disabled it's collider, so it doesn't do anything.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    It is still a dynamic rigidbody which can be affected by forces from scripts. It controls its own destiny as far as positioning goes. It matters because it means you will see the issue you are having. Set it to kinematic or destroy it.
     
    iiJacob likes this.
  7. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    But why? I made the object a child to another one and changed it's transform position, there are no other forces being applied to the rigidbody, it shouldn't move at all.
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    I didn't write Unity's physics engine.

    I am merely telling you how to navigate it.
     
    MinhocaNice likes this.
  9. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    Ok it worked, thank you. This seems to be a bug.
     
  10. devlinpaddock

    devlinpaddock

    Joined:
    Nov 3, 2022
    Posts:
    1
    Not a bug.... just you misunderstanding the engine..
     
    philipprapp95 likes this.