0

I am trying to make a follow camera in scenekit. I have just started, so try to bear with me. I have a node (robotNode) and am trying to have the camera follow the robot. I have partially achived this by doing adding the camera as a child node of the robot:

cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
[robotNode addChildNode:cameraNode];

// place the camera
cameraNode.position = SCNVector3Make(0, 0, 10);

But the problem is when I start to rotate the camera, it doesn't follow the node anymore. See here: cameraNodeImage

How can I get it to continue to follow the node?

Minebomber
  • 1,209
  • 2
  • 12
  • 35

1 Answers1

1

What you've written will create a camera node a fixed distance from the robot, but you've done nothing to control where the camera points.

Create an SCNLookAtConstraint whose target is the robot node. Attach that to the camera node.

// warning, written in browser, untested

SCNLookAtConstraint *robotStare = [SCNLookAtConstraint lookAtConstraintWithTarget:robotNode];
// and maybe also
robotStare.gimbalLockEnabled = YES;

cameraNode.constraints = @[robotStare];

// OP added this, to make camera follow node. I'm skeptical.
cameraNode.camera.usesOrthographicProjection = YES;
Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
  • thanks, but the camera stays in place as the robot moves farther away. Is there any way to make it move with the robot? Also, zooming seems to not work. It seems to look at it though – Minebomber Nov 22 '15 at 22:05
  • Can we see your camera/view setup code? Is the camera you're manipulating the one the SCNView is using, or a separate one? – Hal Mueller Nov 22 '15 at 22:32
  • Actually I figured it out. I set the property `cameraNode.camera.usesOrthographicProjection = YES;` and it started working. – Minebomber Nov 22 '15 at 22:48
  • I'm skeptical that this orthographic/perspective change would make a difference in what the camera follows. Glad you got it working though. – Hal Mueller Nov 23 '15 at 23:26
  • Actually I was wrong. It looked like it worked because it made things the same size. Sorry. – Minebomber Nov 24 '15 at 01:47
  • I also noticed that if I don't set the _usesOrthographicProjection_ property to true, the camera doesn't follow the node. – Ramy Al Zuhouri Oct 16 '16 at 15:40
  • Orthographic projection has absolutely nothing to do with camera node motion. Zero. But because there is no perspective, it might appear that camera is following a moving node. Add some more objects to the scene and you'll get a better understanding of how things are moving and being drawn. – Hal Mueller Oct 16 '16 at 18:56