Fredrik L Fredrik L - 1 year ago 157
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
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]

// Add modelNode to scene
print("Error loading character: "+name)

// Add Camera
func addCamera()
// Remove old model
cameraNode = SCNNode()

// Set up a new camera = SCNCamera()

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


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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download