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
, name itrunning
, leave default typeFloat
, and also default value0.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
, andProperty
>running
.Connect above node
Property Value
socket into Math >A
socket, setB
to0.20
, setOperation
>Multiply
.Connect Math
Result
> Combine XYZY
socket, leaveX
andZ
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), andResult
>Target
(blue to blue socket).Next add Walk, and above it Set Attribute - World Position; Rotate To
Done
>Condition
- both nodes, Combine XYZVector
> WalkVector
, uncheck Set World PositionY
andZ
, both nodesObject
>player
, also uncheck WalkLocal
.For organizing purpose, select all > Ctrl-J to put all into a frame, with frame still selected >
>Change player rotation based on 'running' property
.
This part is done.
![../../../../_images/ln-2d-player_movement-1.png](../../../../_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
, then , same as we did above for the player; name itbedrock
, leave the rest as default.
![../../../../_images/ln-2d-bedrock.png](../../../../_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
, checkContinuous
, connectOn Collision
output to Set World Position >Condition
, and last nodeObject
>player
.
Done. Simple as that.
![../../../../_images/ln-2d-player_movement-2.png](../../../../_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.