Why is this not working?

The idea is, that when I press the left arrow key, the sprite should turn left.
Note: This is a 2D game.

var speed = 5;

var isLookingLeft:int = 1;

function Start () {

}

function Update () {

if (Input.GetAxis("Horizontal") > 0) {

if (isLookingLeft > 0) {

transform.localScale += (Vector3(-1,0,0));

isLookingLeft = -1;

}


transform.Translate(Vector3(1 * speed * Time.deltaTime,0,0));

}

}

What you doing with the scale?

Your problem is here:

transform.Translate(Vector3(1 * speed * Time.deltaTime,0,0));

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 :slight_smile:

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);
  */
}

I actually managed to do this with this script:
Thanks to vexe for helping me move towards the right answer.

var speed:float = 10;

var isLookingLeft:int = 0;

var isLookingRight: int = 0;

function Start () {

}

function Update () {

if (Input.GetAxis(“Horizontal”) > 0) {

if (isLookingLeft <1) {

isLookingRight = 0;

transform.localScale += (Vector3(-2,0,0));

isLookingLeft =1;

}

transform.Translate(Vector3(1 * speed * Time.deltaTime,0,0));

}

if (Input.GetAxis(“Horizontal”) < 0) {

if (isLookingRight < 1) {

isLookingLeft = 0;

transform.localScale += (Vector3(2,0,0));

isLookingRight =1;

}

transform.Translate(Vector3(-1 * speed * Time.deltaTime,0,0));

}

}