Siddharth Shekar
Siddharth Shekar

Reputation: 440

SceneKit SCNLookAtConstraint could not overload init

In the example project of sceneKit I am trying to make the camera look at the ship object which I have offset from the center. But for some reason I am unable to do it.

I use the following code to look at the ship. but it still gives as error:

Could not find an overload for init.

I am using swift to code.

cameraNode.constraints = SCNLookAtConstraint(ship)

pls help. Thanks.

Upvotes: 0

Views: 1786

Answers (3)

FractalDoctor
FractalDoctor

Reputation: 2546

This kind of setup works for me, obviously you're better off not just unwrapping the node in case it doesn't exist, but it gives the general idea.

let targetNode = scene.rootNode.childNodeWithName("nodeName", recursively: false)    
let constraint               = SCNLookAtConstraint(target: targetNode!)
cameraNode?.constraints = [constraint]

Upvotes: 1

rickster
rickster

Reputation: 126177

I see the following in autocomplete, which looks promising:

SCNLookAtConstraint(target:)

This is the Swift form of the ObjC factory method +lookAtConstraintWithTarget:.

In general, there are three easy ways to find out how an ObjC factory method (like +[NSFoo fooWithBar:]) translates to a Swift initializer (like init(bar:), which you call with the syntax NSFoo(bar: heresABar)):

  • Type the class name into a Swift file, and an open paren, and autocomplete should show you the set of acceptable initializers
  • Type the class name into a Swift file and command-click it to see the auto-generated Swift interface for that class (based on the ObjC header), including initializer declarations
  • Look at the documentation for a class online or in Xcode, and you'll see both ObjC and Swift declarations for each API.

Upvotes: 1

mnuages
mnuages

Reputation: 13462

you have to create a look-at constraint with +lookAtConstraintWithTarget:. Also note that the constraints property expects an array.

Upvotes: 0

Related Questions