Tiptech
Tiptech

Reputation: 177

SceneKit camera follow object only on x-axis? - Swift

How can I move a camera according to the x position of a moving sphere, but ignore the y and z coordinates of that sphere at the same time? Here is my code for following the sphere on its 3 dimensions:

    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    cameraNode.camera?.usesOrthographicProjection = true
    cameraNode.camera?.orthographicScale = 2
    let constraint = SCNLookAtConstraint(target: sphere)
    constraint.gimbalLockEnabled = true
    self.cameraNode.constraints = [constraint]
    cameraNode.position = SCNVector3Make(20, 20, 20)
    cameraNode.eulerAngles = SCNVector3Make(0, 45, 0)
    sphere.addChildNode(cameraNode)

How can I just follow just the x position of the sphere, instead of it's y and z positions as well?

Upvotes: 0

Views: 1033

Answers (1)

bpedit
bpedit

Reputation: 1196

Un-child the camera from sphere, make it a child of root instead. Within whatever code is moving sphere, set the camera's x position to that of sphere.

// code moving sphere
cameraNode.position.x = sphere.position.x

Upvotes: 2

Related Questions