Setting weapon to active depending on player direction.

So I’ve got two sprites set up as weapons on both sides of the player and I want one of the weapons to be active and visible when the player is moving in one direction and the other to not be active and visible. Does anyone know how I can do this, and do they know how I can get the script to detect which direction the player is moving in?

Thanks

P.s I’m new to programming so I don’t really know what I’m doing so this might be something really simple sorry.

In a 2d platformer, the player usually flips on key press. So that would be a perfect time to change out the weapons.

public GameObject weapon1;
public GameObject weapon2;

private void Update()
{
    if(Input.GetKeyDown(Key.A))
    {
        weapon1.SetActive(false);
        weapon2.SetActive(true);
    }
    if(Input.GetKeyDown(Key.D))
    {
        weapon1.SetActive(true);
        weapon2.SetActive(false);
    }
}

This is untested and Key.A might be KeyCode.A I don’t remember. Unity also has an Input Manager that I never use but it’s there.

When your game progresses, you might want a Mechanim variable for this. Hopefully this is enough to get you started and have fun learning Unity.

Thanks I’ve tried the code and it works great. It did need to be KeyCode instead of Key.