- Home /
How to flip my player left and right?
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;
}
}
Answer by khooroko · Aug 10, 2018 at 01:49 AM
It's a typo. It should be if (facingRight == false)
and if (facingRight == true)
with double equals to check for equality. facingRight = false
will always return true because it's a simple assignment expression, so Flip() will be called every frame when you're holding down A or D. At least that's how I think it is, but I'm not sure why it does not get called in your case when holding D. Try changing this and see if it works.
Your answer
![](https://koobas.hobune.stream/wayback/20220612172525im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How do I check if a player is holding a trick and not just tapping it (but i want both) 0 Answers
Player movement starts after a few seconds after pressing the button 1 Answer
Multiple Cars not working 1 Answer
How can I have a CharacterController's height make it to where the feet are correctly on the ground? 0 Answers