You’re always gonna be moving right - Cuz you’re passing in a positive value to x every time. - You don’t need to perform any checks on Input.GetAxis - For more info on how it works.
This is all you need for moving left/right:
int speed = 4;
private Transform _transform;
function Start()
{
// cache the transform component - for better performance
// because whenever you access your transform component with just 'transform'
// you are actually making a GetComponent call to get the transform
// and you don't want too many calls to GetComponent inside Update for example
_transform = transform;
}
function Update()
{
_transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0);
}
A side note: ints shouldn’t be used to check for two-states situations (looking left or right for ex) - this is what booleans are for - use a bool variable
EDIT: OK so you’re using the scale to flip your sprite/2dTexture, fine. Why are you doing a += everytime you look left? - you should just assign it. You also didn’t write anything for going right, try this out:
float speed - 10;
private float initHorizScale;
private bool isLookingLeft = false; // start by looking right
function Start()
{
_transform = transform;
initHorizScale = _transform.localScale.x; // store the initial x scale
}
function Update()
{
_transform.localScale = Vector3( isLookingLeft ? -initHorizScale : initHorizScale, 0, 0);
_transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0);
/*
if you're not familiar with the notation I used to set the scale, it's basically the same as:
if (isLookingLeft)
_transform.localScale = Vector3(-initHorizScale, 0, 0);
else
_transform.localScale = Vector3(initHorizScale, 0, 0);
*/
}