[PROBLEM!] 2d character facing right/left

#pragma strict

var X: float;

function Start () {
	X = transform.localScale.x;
}

function Update () {
	if(Input.GetKey("a")  !Input.GetKeyDown("d")){
		transform.localScale.x = -X;
	}
	if(Input.GetKey("d")  !Input.GetKeyDown("a")){
		transform.localScale.x = X;
	}
}

This code is supposed to flip the character right if d is pressed and left if a is pressed.
It also checks if a or d is already pressed, so it shouldn’t face left when the player moves right. Reversed the same…
If i go right and press a, the character keeps moving right and facing right, so that’s not a problem,
but if i move left and press d, the character keeps moving left, but facing to the right.

I have the same code for left and right, so there shouldn’t be a problem.
What am I doing wrong?

Idk how much help this or if youve already figured it out but you could try doing the same thing a different way. i just replaced the !GetKeyDown(“”) with a boolean. good luck. :smile:

float X;
bool apressed;
bool dpressed;
 

void Start () {
    X = transform.localScale.x;
}


void Update () {
    if(Input.GetKey("a")  !dpressed){
        apressed=true;
        transform.localScale.x = -X;
    }

   if(Input.GetKey("d")  !apressed){
        dpressed=true;
        transform.localScale.x = -X;
    }

    if(Input.GetKeyUp("a") ){
       apressed =false;
    }

 
   if(Input.GetKeyUp("d") ){
       dpressed =false;
    }
 
}

Maybe I’m misunderstanding, but why not just do something like:

class TestFace (MonoBehaviour): 
	def Update ():
		if Input.GetKeyDown("a"):
			RotateY(180)
		elif Input.GetKeyDown("d"):
			RotateY(0)
		
		transform.Translate(Vector3.right * 2 * Time.deltaTime) //Simple movement test
	def RotateY(rotation as single):
		transform.eulerAngles.y = rotation

There’s no need to mess with the scale, and you should try and avoid it since it may have unforsen consequences.