Fredrik L Fredrik L - 4 months ago 40
iOS Question

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.

Answer

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