2. Player Movement

From the game logic perspective, player can do few movements:

  • It can walk/run, either to the left, or to the right, relative to the screen space.

  • It can jump up.

  • It can fall off the rock into the void.

For this reason, we need to know what state/movement a player is in. We need properties to keep those states updated while the game is running.

  • With player selected, first add Properties > Game > Game Properties > Add Game Property, name it running, leave default type Float, and also default value 0.0. Save often.

  • In Logic Node Editor, add new node tree, name it player_2d_movement, and set ‘Fake User’.

  • Next add Get Object Property, Object > player, and Property > running.

  • Connect above node Property Value socket into Math > A socket, set B to 0.20, set Operation > Multiply.

  • Connect Math Result > Combine XYZ Y socket, leave X and Z at default value (0.0).

  • Below Combine XYZ node add Get Attribute - World Position, and Object > player.

  • Next add Vector Math, connect Combine XYZ Vector > Vector 1, World Position > Vector 2, Operation > Add.

  • Above Vector Math add On Update, next Rotate To, connect Out > Condition (red to red socket), and Result > Target (blue to blue socket).

  • Next add Walk, and above it Set Attribute - World Position; Rotate To Done > Condition - both nodes, Combine XYZ Vector > Walk Vector, uncheck Set World Position Y and Z, both nodes Object > player, also uncheck Walk Local.

  • For organizing purpose, select all > Ctrl-J to put all into a frame, with frame still selected > N-panel > Node > Node > Label > Change player rotation based on 'running' property.

This part is done.

../../../../_images/ln-2d-player_movement-1.png

Player movement setup - rotation part

Next we add collision detection - if player falls into the void, when it collides with Bedrock object, it will be ‘teleported’ back to the beginning. This is the sole purpose of Bedrock object.

  • First we need to add a property to Bedrock object > select it in Outliner, then Add Game Property, same as we did above for the player; name it bedrock, leave the rest as default.

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

Bedrock property

  • In Logic Node Editor, under Player movement frame, add Collision, and Set Attribute - World Position - it makes sense, doesn’t it, because when player will fall into the void, and touch (collide) with the Bedrock, we will reset its position, otherwise player would keep falling deeper and deeper into the void - forever.

  • Set Collision Property > bedrock, check Continuous, connect On Collision output to Set World Position > Condition, and last node Object > player.

Done. Simple as that.

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

Reset player position nodes setup

In case you need to ‘teleport’ player somewhere else than at the beginning, change Set World Position Value fields (from top to bottom - X, Y, Z). This is used in case when player manages to advance to some ‘checkpoint’ inside the level, but it fails to make it to the end - it is ‘teleported’ to last ‘checkpoint’.

If you run the game, not much will happen - the game is not yet functional. We need to add more functionality to the player. And Apply To Selected all logic trees to player, of course.