# Player not moving

I’m creating an 2d Game Currently, but my player object just isn’t moving like it should, do you have any idea why this could be?

``````	#region PlayerMovement
if (PlayerPosY < 199) {
PlayerPosY = playerInstance.transform.position.y;
if(! BossArrived){
playerInstance.transform.Translate (Vector3.up * Time.deltaTime * PlayerSpeed);

}

if (Input.GetKey (KeyCode.S)) {
if (PlayerSpeed > 2) {
PlayerSpeed = PlayerSpeed - 1;
}
}
if (Input.GetKey (KeyCode.W)) {
if (PlayerSpeed < 10) {
PlayerSpeed = PlayerSpeed + 1;
}
}

if (Input.GetKey (KeyCode.A)) {
PlayerPosX -= 0.5f;
playerInstance.transform.position += -transform.right * Time.deltaTime * PlayerSpeed;
}

if (Input.GetKey (KeyCode.D)) {
PlayerPosX += 0.5f;
playerInstance.transform.position += transform.right * Time.deltaTime * PlayerSpeed;
}
}

//REVERSED CONTROL ;D
if (PlayerPosY >= 198) {
PlayerPosY = 400;
if(PlayerRotX == 0) {
playerInstance.transform.Rotate (180, 0, 0, Space.World);
PlayerRotX = 1;
}
playerInstance.transform.Translate (Vector2.up * Time.deltaTime * PlayerSpeed);
if (Input.GetKey (KeyCode.S)) {
if (PlayerSpeed < 10) {
PlayerSpeed = PlayerSpeed + 1;
}
}
if (Input.GetKey (KeyCode.W)) {
if (PlayerSpeed > 2) {
PlayerSpeed = PlayerSpeed - 1;
}
}
}

if (Input.GetKey (KeyCode.A)) {
PlayerPosX += 0.5f;
playerInstance.transform.position += transform.right * Time.deltaTime * PlayerSpeed;

}
if (Input.GetKey (KeyCode.D)) {
PlayerPosX -= 0.5f;
playerInstance.transform.position += -transform.right * Time.deltaTime * PlayerSpeed;
}
if (!playerInstance.GetComponent<SpriteRenderer> ().isVisible) {
PlayerPosX = 10;
playerInstance.transform.position = new Vector3 (PlayerPosX, playerInstance.transform.position.y, PlayerPosZ);
}
if (Input.GetKeyDown (KeyCode.P)) {
if (Time.timeScale == 1) {
Time.timeScale = 0;
} else if (Time.timeScale == 0) {
Time.timeScale = 1;
}
}
if (Input.GetKeyDown (KeyCode.Escape)) {
Application.Quit ();
}
#endregion
``````

If you look at the code you posted here, at line 49, your “Reverse Control” if statement ends early!

That means that before reaching Y 199, you’d always Move Left (Normal + Reverse) and Right (Normal + Reverse) at the same time when pressing A or D, Normal + Reverse together meaning No Movement Sideways.

To fix this, move the } to line 59.

I noticed another danger and that is:

PlayerPosY < 199 combined with PlayerPosY >= 198, at player Y == 198, the player will move both normal and reverse, so it won’t move.

To fix that, make it an if else statement instead of a double if.

Noticed that you use “transform.right” instead of “playerTransform.right” for moving sideways, which will not make it invert correctly.

Noticed that you’re NOT applying PlayerPosX in your sideways movement, even though you set it.

Noticed (During refactoring, fixed that for you in the finished code):

``````        // When reaching 199, jumps to 400, warning this will lock the player on Y 400.
PlayerPosY = reverseControl ? 400 : playerTransform.position.y;
``````

Conceptually, I think you have a 2D Vertical Scroll Shooter game, where you move from 0 to 200 and then reverse from 400 to 200, is that correct?

On top of that I cleaned up your code a bit. You can adjust it to your needs, but this should look / feel cleaner.

``````using UnityEngine;
using System.Collections;

public class PlayerMovementFix : MonoBehaviour {

public GameObject playerInstance;
private Transform playerTransform;

public float PlayerSpeed_Current = 5f;
public float PlayerSpeed_Min = 2f;
public float PlayerSpeed_Max = 10f;
public float PlayerSpeed_Sideways = 0.5f;
public float PlayerPosX, PlayerPosY, PlayerPosZ,
PlayerRotX;

public bool BossArrived;

void Update ()
{
#region PlayerMovement

playerTransform = playerInstance.transform;

// Noticed original danger: PlayerPosY < 199 && PlayerPosY >= 198, at player Y == 198, the player will move both normal and inverse, so it won't move.
bool reverseControl = PlayerPosY >= 198;
int sideways = 0;

// Move up, down, left and right. And reversed if reverseControl == true.
if (Input.GetKey(KeyCode.S)) PlayerSpeed_Current = reverseControl ? PlayerSpeed_Current + 1 : PlayerSpeed_Current - 1;
if (Input.GetKey(KeyCode.W)) PlayerSpeed_Current = reverseControl ? PlayerSpeed_Current - 1 : PlayerSpeed_Current + 1;
if (Input.GetKey(KeyCode.A)) sideways = reverseControl ? 1 : -1;
if (Input.GetKey(KeyCode.D)) sideways = reverseControl ? -1 : 1;

if (reverseControl)
{
// Rotate player 180. (Only do this once)
if (PlayerRotX == 0)
{
PlayerPosY = 400; // Set Y to 400 once.
playerTransform.Rotate(180, 0, 0, Space.World);
PlayerRotX = 1;
}
}

// Makes sure PlayerSpeed is always at least 2 and at most 10.
PlayerSpeed_Current = Mathf.Clamp(PlayerSpeed_Current, PlayerSpeed_Min, PlayerSpeed_Max);

// Move up or down, but only if there's no boss arrived.
if (!BossArrived)
{
Vector3 upOrDown = (reverseControl ? Vector3.down : Vector3.up);
playerTransform.Translate(upOrDown * Time.deltaTime * PlayerSpeed_Current);
}

// Sideways movement.
if (sideways != 0)
{
bool left = (sideways == -1);

if (left)
{
playerTransform.position -= playerTransform.right * PlayerSpeed_Sideways * Time.deltaTime;
}
else
{
playerTransform.position += playerTransform.right * PlayerSpeed_Sideways * Time.deltaTime;
}
}

#endregion

// Check Player Visible, Pause and Exit.
_checkPlayerVisible();
_checkTogglePause();
_checkEscape();
}

private void _checkPlayerVisible()
{
// Reset X to 10 if Player is no longer visible.
if (!playerInstance.GetComponent<SpriteRenderer>().isVisible)
{
PlayerPosX = 10;
playerTransform.position = new Vector3(PlayerPosX, playerTransform.position.y, PlayerPosZ);
}
}

private void _checkTogglePause()
{
if (Input.GetKeyDown(KeyCode.P))
{
Time.timeScale = (Time.timeScale == 1) ? 0 : 1;
}
}

private void _checkEscape()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
}
``````

I hope that helps, if it did, please accept this answer, it’d be much appreciated!
If you have any other questions, let me know!

Cheers,

ThePersister