繁体   English   中英

在Unity中将相机从播放器上取下

[英]Take the Camera away from the Player in Unity

当我的播放器被销毁时,我想拿起相机(它是播放器的子对象)并将其带回层次结构。 但是我不知道如何获得层次结构的转换。

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方法的参数传递。

将转换父级设置为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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM