简体   繁体   中英

SceneKit: Camera to look at model

Is it possible to make the camera look at a model? Here is what I've tried:

// Add Character
func addModel(name:String)
{
    // Remove old model
    self.modelNode.removeFromParentNode()
    modelNode = SCNNode()

    // Load COLLADA Character
    if let myScene = SCNScene(named: "Assets.scnassets/"+name+"/"+name+".dae")
    {
        // Recurse through all the child nodes in the Character and add to modelNode
        for node in myScene.rootNode.childNodes as [SCNNode]
        {
            modelNode.addChildNode(node)
        }

        // Add modelNode to scene
        self.rootNode.addChildNode(modelNode)
    }
    else
    {
        print("Error loading character: "+name)
    }
}

// Add Camera
func addCamera()
{
    // Remove old model
    self.cameraNode.removeFromParentNode()
    cameraNode = SCNNode()

    // Set up a new camera
    cameraNode.camera = SCNCamera()

    // Set up LookAtConstraint
    let constraint = SCNLookAtConstraint(target: modelNode)
    cameraNode.constraints = [constraint]

    self.rootNode.addChildNode(cameraNode)
}

This results in nothing. Meaning, I can't see the model. If i don't run the addCamera() , the model shows up. But I wan't the camera too look at the model.

Using SCNLookAtConstraint(target: modelNode.childNodeWithName) instead did work out for me. I assume the the parent node did not hold the childs constraints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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