1. Camera Movement

In 2d scroller games, camera is usually showing player from the side, and it moves perpendicularly together with the player. If you press Numpad 0 (zero - camera perspective), or Numpad 3, you should see player from same perspective (press Numpad , (comma) to focus the player).

  • Add a new logic tree, name it ‘player_2d_camera’, and set ‘Fake User’.

  • Next add, from left to right, Get World Position, set ‘player’ as Object - it will get player’s position, so camera knows where to go, following the player.

  • Add Separate XYZ vector node, to separate 3D vector into separate XYZ coordinates from ‘World Position’; connect the sockets.

  • Under above node, add 2 Float nodes - one will set camera distance from player, the other will set following speed. Select both ‘Float’ nodes, hit Ctrl-J - this will group both nodes into a frame, for better organization. Rename top node to ‘camera distance’, and set value to i.e. 30.0; rename bottom one to ‘slow camera’, set value to i.e. 0.15 (with node selected - side N-panel > Node > Label). Also, with frame selected, rename it i.e. into ‘camera custom’, and also change frame color (below ‘Label’).

  • Next add Math node, set operation to Add, connect X coordinate from Separate XYZ into A socket, and Float output socket from camera distance into B socket. This will get player’s X position (horizontal) and set camera’s X position accordingly.

  • Add another Math node under the first one, connect Z coordinate from Separate XYZ into A socket, and set B value to 1.0.

  • Add Combine XYZ node, connect top Math node into X, bottom Math node into Z, and Y socket from Separate XYZ directly into Y socket.

  • Above first Math node, add Active Camera, next another Get World Position above Combine XYZ, and connect Camera as Object.

  • Next add Vector Math node, set ‘Operation’ to Add, connect Get World Position into top Vector 1 socket, Combine XYZ into Vector 2 socket.

  • Next add another Vector Math, set ‘Operation’ to Scale, connect previous node into Vector 1, and slow follow into Scale.

  • Add On Update above last node, next Set World Position node; connect On Update into Condition socket, Active Camera as Object, and last Vector Math into Value socket.

  • With ‘player’ selected, Apply To Selected.

Done.

../../../../_images/ln-2d-player_camera.png

2D player camera nodes setup