Player is not moving left

Hey guys,

I’ve been playing around with tutorials and such and I’m trying to get my player to move left and it’s only moving right for some reason. I don’t think the code has any problems if so please let me know what’s the error.

Code:

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

public float speed = 8f, maxVelocity = 4f;

private Rigidbody2D myBody;
private Animator animator;

void Awake() {
	myBody = GetComponent<Rigidbody2D>();
	animator = GetComponent<Animator>();
}

// Use this for initialization
void Start () {

}

void FixedUpdate () {
	PlayerMoveKeyboard();
}

void PlayerMoveKeyboard() {
	float forceX = 0f;
	float vel = Mathf.Abs(myBody.velocity.x);

	float xAxis = Input.GetAxisRaw("Horizontal"); 

	if(xAxis > 0) { 
		if(vel < maxVelocity) {
			forceX = speed;
		} else if(xAxis < 0) { 
			if(vel < maxVelocity) {
				forceX = -speed;
			}
		}
	}

	myBody.AddForce(new Vector2(forceX, 0));
}
}

Thanks!

Your if statements for xAxis > 0 is wrong.

This is the correct way to write it:

if(xAxis > 0) { 
    if(vel < maxVelocity) {
        forceX = speed;
    }
}
else if(xAxis < 0) { 
    if(vel < maxVelocity) {
        forceX = -speed;
    }
}