Unity C#-将子对象从父对象中抛出

Unity C#-将子对象从父对象中抛出,c#,unity3d,C#,Unity3d,当按下输入时,我试图从父对象“抛出”特定的子对象(如球)。我让我的球员捡起一个球,让它成为自己的孩子(并随球移动),但我无法让投掷功能发挥作用。这是我的代码:请注意更新函数中的“//”,我相信代码会用到这里?我可能错了。我试了好几件事都没用 public static float playerDistance; bool hasGarbage; // Update is called once per frame void Update () { float h = Input.Ge

当按下输入时,我试图从父对象“抛出”特定的子对象(如球)。我让我的球员捡起一个球,让它成为自己的孩子(并随球移动),但我无法让投掷功能发挥作用。这是我的代码:请注意更新函数中的“//”,我相信代码会用到这里?我可能错了。我试了好几件事都没用

public static float playerDistance;
bool hasGarbage;

// Update is called once per frame
void Update () {

    float h = Input.GetAxis("Horizontal");
    transform.Translate(h * Time.deltaTime * 5f, 0f, 5f * Time.deltaTime);
    playerDistance = transform.localPosition.z;


    if (hasGarbage == true)
    {
        if (Input.GetButtonDown("Fire1"))
        {
            ////////////////////////////////////////////////////

        }
    }

}

private void OnTriggerEnter(Collider other)
{
    if (other.tag == "Garbage")
    {

        other.transform.parent = transform;
        hasGarbage = true;


    }
}
}尝试使用AddForce()。这将向指定的方向增加力

以下是unity文档:

下面是一个it实际应用的示例:

你应该考虑创建一个球,而不是实际将球放在球员下方。当你需要投球时,只需在对球施加任何力之前折断关节即可。