Basically i have a basic code that lets me move left and right with the A and D keys, this also plays a running animation. However the code i have only flips the player when the A button is pressed and then it flips constantly until i let go, but when i press D the player doesn’t flip. How can i get my player to only flip once and to flip left when i press A and right when i press D?
I’ll just paste my whole code down here so if anyone can help me out with this problem it will be much appreciated
public class PlayerController : MonoBehaviour
{
//delcare variables
float walkingSpeed = 0.02f;
float runningSpeed = 0.05f;
bool facingRight = true;
Animator anim;
void Start ()
{
anim = this.GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
Vector2 playerPos = this.transform.position;
if (Input.GetKey (KeyCode.D) && Input.GetKey (KeyCode.LeftShift) == false)
{
//Debug.Log ("running Right");
playerPos.x = playerPos.x + runningSpeed;
this.transform.position = playerPos;
anim.SetTrigger ("IsRunning");
if (facingRight = false)
{
Flip ();
}
}
else if (Input.GetKey (KeyCode.A) && Input.GetKey (KeyCode.LeftShift) == false)
{
//Debug.Log ("running left ");
playerPos.x = playerPos.x - runningSpeed;
this.transform.position = playerPos;
anim.SetTrigger ("IsRunning");
if (facingRight = true)
{
Flip ();
}
}
else if (Input.GetKey (KeyCode.D) && Input.GetKey (KeyCode.LeftShift) == true)
{
Debug.Log ("walking right");
playerPos.x = playerPos.x + walkingSpeed;
this.transform.position = playerPos;
}
else if (Input.GetKey (KeyCode.A) && Input.GetKey (KeyCode.LeftShift) == true)
{
//Debug.Log ("walking left");
playerPos.x = playerPos.x - walkingSpeed;
this.transform.position = playerPos;
}
else
{
anim.SetTrigger ("IsIdle");
}
}
void Flip ()
{
Debug.Log ("flipping the player");
facingRight = !facingRight;
Vector2 playerScale = this.transform.localScale;
playerScale.x = playerScale.x * -1;
this.transform.localScale = playerScale;
}
}