首页 > 解决方案 > 如何获取 SceneKit 场景中所有节点的当前位置

问题描述

我试图在我的 SceneKit 游戏中获取特定节点的 SCNVector3,以便我可以确定我的玩家在每一轮中如何与它们交互。我在玩家执行移动操作后运行此程序,但我的“可推送块”的 mapList 中的位置保持不变,即使它们位于游戏中的不同位置。

var mapList: [SCNNode] = []    

func mapLocationSave() {
        let pushableNode = self.gameScene.rootNode.childNode(withName: "Pushables", recursively: true)
        let wallNodes = gameScene.rootNode.childNode(withName: "Boxes", recursively: true)
        let coinNodes = gameScene.rootNode.childNode(withName: "Coins", recursively: true)
        
        for pushNode in pushableNode!.childNodes {
            mapList.append(pushNode)
        }
        
        for wallNode in wallNodes!.childNodes {
            mapList.append(wallNode)
        }
        
        for coinNode in coinNodes!.childNodes {
            mapList.append(coinNode)
        }
        
        for node in mapList {
            print(node.name!, node.worldPosition)
        }
    }

我怀疑这是因为我没有用当前的游戏位置更新 mapList 并且只是循环通过 gameScene.scn 文件?

我在 Apple 文档中找不到任何关于如何在游戏节点中循环当前的文档。

标签: iosswiftscenekit

解决方案


我想我已经修复了它购买通过相机节点可见的节点。

let nodes = scnView.nodesInsideFrustum(of: cameraNode)
        for node in nodes {
            mapList.append(node)
        }

然后我每次都保存位置。

有没有更好的办法?


推荐阅读