Unity child object breaks away from the parent object, cancels the parent-child relationship, and the child object does not move with the parent object

Create a grenade in Unity, and the requirement is that when the grenade is thrown, the grenade cannot follow the character to move

         This needs to call the gameObject.transform.DetachChildren method to release the parent-child relationship of all objects under GrenadePosition, then when throwing the grenade, the grenade will not move with the protagonist

    public void ThrowGrenade()
    {
        if (CurrentGrenadeNumber > 0)   // 扔出手雷
        {
            try
            {   // 首先,寻找手雷
                GameObject G = Grenades.transform.GetChild(0).gameObject;
                Grenades.transform.DetachChildren();    //  *******   注意: 通过DetachChildren方法使子物体脱离父物体   *********
                // 调用DetachChildren解触Grenade的父子关系 

            }
            catch
            {

            }
        }
    }

Guess you like

Origin blog.csdn.net/sbsbsb666666/article/details/126186421