C# 将相机从统一的玩家手中拿开

C# 将相机从统一的玩家手中拿开,c#,unity3d,C#,Unity3d,当我的播放器被销毁时,我想把相机(它是播放器的子对象)带回到层次结构。但我不知道如何得到层次结构的变换 private void OnCollisionEnter(Collision col) { if (col.gameObject.CompareTag("Player")) // player collision with the obstacle { ReplaceCamera(); // Take the camera aw

当我的播放器被销毁时,我想把相机(它是播放器的子对象)带回到层次结构。但我不知道如何得到层次结构的变换

private void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.CompareTag("Player")) // player collision with the obstacle
        {
            ReplaceCamera(); // Take the camera away from the player
            Destroy(col.gameObject); // Destroy the player
        }
    }

    void ReplaceCamera()
    {
        Camera.main.transform.SetParent(?); // Set the camera as a child of the hierarchy
    }

我只是不知道要传递什么作为我的ReplaceCamera方法的参数。

将transforms parent设置为
null
将把它放在层次结构的根中

    void ReplaceCamera()
    {
        Camera.main.transform.SetParent(null);
    }
稍后,当/如果新玩家生成时,您可以通过将变换设置为玩家的子级,将相机添加回生成的玩家,您可以使用此功能同时执行这两项操作,如果您提供了一个,则会将其设置为玩家

    void ReplaceCamera(Transform player = null)
    {
        Camera.main.transform.SetParent(player);
    }

// Usage Examples
    ReplaceCamera(); // will send it to the root
    ReplaceCamera(player); // will send it to be part of the player again.

将父项设置为
null
将把它放在层次结构的根中

    void ReplaceCamera()
    {
        Camera.main.transform.SetParent(null);
    }
稍后,当/如果新玩家生成时,您可以通过将变换设置为玩家的子级,将相机添加回生成的玩家,您可以使用此功能同时执行这两项操作,如果您提供了一个,则会将其设置为玩家

    void ReplaceCamera(Transform player = null)
    {
        Camera.main.transform.SetParent(player);
    }

// Usage Examples
    ReplaceCamera(); // will send it to the root
    ReplaceCamera(player); // will send it to be part of the player again.