Problems with animation and movement

Hello, I’m currently trying to make my player character move around the map, but for some reason, it will not move left and right (it will play the animation trigger however) and it can only move up and down in a restricted space. Currently my code for movement looks like this:
public class PlayerMovement : MonoBehaviour {

[SerializeField]
private float speed;

[SerializeField]
private Animator animator;

private void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector2 currentVelocity = gameObject.GetComponent<Rigidbody2D>().velocity;

    float newVelocityX = 0f;
    if (moveHorizontal < 0 && currentVelocity.x <= 0)
    {
        newVelocityX = -speed;
        animator.SetInteger("DirectionX", -1);
    }
    else if (moveHorizontal > 0 && currentVelocity.x >= 0)
    {
        newVelocityX = speed;
        animator.SetInteger("DirectionX", 1);
    }
    else
    {
        animator.SetInteger("DirectionX", 0);
    }

    float newVelocityY = 0f;
    if (moveVertical < 0 && currentVelocity.y <= 0)
    {
        newVelocityY = -speed;
        animator.SetInteger("DirectionY", -1);
    }
    else if (moveVertical > 0 && currentVelocity.y >= 0)
    {
        newVelocityY = speed;
        animator.SetInteger("DirectionY", 1);
    }
    else
    {
        animator.SetInteger("DirectionY", 0);
    }

    gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(newVelocityX, newVelocityY);
}

I’m not entirely sure where I went wrong, and why my character isn’t moving left and right. I’m wondering if it has something to do with my map, since all objects are on one layer, meaning the ground and collidable objects such as trees have sort of overlapped, but I don’t know how to separate them into different layers.

If anyone knows how to help, I’d really appreciate it.

Hi GameCreater562,

In terms of functionality, there's nothing wrong with your script. You should be able to move left and right. It probably has to do with colliders like you said.

Luckily you can easily modify what collides with what in Unity. I refer to layer system. You can give every collider a layer just as you would a tag. Then if you go to Edit > Project Settings > Physics2D (considering you're working in a 2D project).

![128226-phy2d.png|501x780](upload://uc3AqnqOUcz0bnJnAaRHReqrY7V.png)

Then you'll get a collision matrix. Shown below is default. You will see your own custom added layers in here as well.

128227-phy2d-2.png

In here you can simply uncheck the boxes for the objects you wouldn't want to collide with one another (for instance if there are colliders on things you don't want to collide with the player).

Let me know if this helped, and good luck on your project :)