【问题标题】:Take the Camera away from the Player in Unity在 Unity 中将相机从玩家那里拿开
【发布时间】:2017-05-06 13:15:05
【问题描述】:

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

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

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多