# Player movement script for the 3×3 POC

I just wanted to show how I chose to do the player control script. After playing the game some more, I’ve decided that the game isn’t going to work on the mobile devises. The swipe to move the player would be very counter intuitive after level 8.

For the movement script, I wanted the player to move in a continuous motion and not in discreet jumps. The player should also be able to keep moving in one direction holding down a direction button.

To keep track of what the player is doing, I first made an enum with the possible motion states.
I guess the enums are self explanatory π

```enum Moving
{
NO,
UP,
DOWN,
LEFT,
RIGHT
};```

Now the the logic. If the player isn’t moving, check for player input.

```        if (direction == Moving.NO)
{
if (Input.GetKey(KeyCode.A))
{
direction = Moving.LEFT;
}
if (Input.GetKey(KeyCode.D))
{
direction = Moving.RIGHT;
}
if (Input.GetKey(KeyCode.W))
{
direction = Moving.UP;
}
if (Input.GetKey(KeyCode.S))
{
direction = Moving.DOWN;
}
}```

Now if the player is moving, the enum will be holding the direction of motion and we can move the player.

```            switch (direction)
{
case Moving.UP:
if (moveLeft > 0f)
{
Vector3 v3 = transform.position;
v3.y += movementSpeed;
moveLeft -= +movementSpeed;
if (v3.y > 1f)
{
v3.y = 1f;
moveLeft = 1.0f;
direction = Moving.NO;
}
transform.position = v3;
}
else
{
moveLeft = 1.0f;
direction = Moving.NO;
}
break;
case Moving.DOWN:
if (moveLeft > 0f)
{
Vector3 v3 = transform.position;
v3.y -= movementSpeed;
moveLeft -= +movementSpeed;
if (v3.y < -1f)
{
v3.y = -1f;
moveLeft = 1.0f;
direction = Moving.NO;
}
transform.position = v3;
}
else
{
moveLeft = 1.0f;
direction = Moving.NO;
}
break;
case Moving.LEFT:
if (moveLeft > 0f)
{
Vector3 v3 = transform.position;
v3.x -= movementSpeed;
moveLeft -= +movementSpeed;
if (v3.x < -1f)
{
v3.x = -1f;
moveLeft = 1.0f;
direction = Moving.NO;
} transform.position = v3;
}

else
{
moveLeft = 1.0f;
direction = Moving.NO;
}
break;
case Moving.RIGHT:
if (moveLeft > 0f)
{
Vector3 v3 = transform.position;
v3.x += movementSpeed;
moveLeft -= +movementSpeed;
if (v3.x > 1f)
{
v3.x = 1f;
moveLeft = 1.0f;
direction = Moving.NO;
}
transform.position = v3;
}
else
{
moveLeft = 1.0f;
direction = Moving.NO;
}
break;
default:
break;
}```

That’s it, not pretty but it works. If I have the time, I would like to make the motion even more fluent. The state change if you keep a key down makes the player give a little jump/jitter as the state goes through NO state before returning to a moving state. But for now it gets the job done π

All feedback is welcome, I can’t change if I don’t know what to change. π

– Henning

### 1 Responses to Player movement script for the 3×3 POC

Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /var/www/incd021.com/public_html/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048