- Home /
C# 2D platformer: How to have player always face center?
I'm building a 2D platformer where I want the player to always face 0 on the x axis.
I tried it by making my player flip with transform.localScale when position.x <0. But my player keeps flipping back and forth when the condition is met (on the left side of the 0 point).
I tried using a bitflag, but not working... What am I doing wrong? (Highly descriptive answers about the why's and how's are much appreciated as I've only started learning Unity and C# a few days ago)
public Transform flipCheck;
private bool flipped = false;
// Use this for initialization
void Start () {
}
void FixedUpdate() {
}
// Update is called once per frame
void Update () {
if (gameObject.transform.position.x < 0 && !flipped) {
Debug.Log ("player is facing right");
transform.localScale = new Vector2 (transform.localScale.x *-1, transform.localScale.y);
}
else {
Debug.Log ("player is facing left");
}
Your answer
Follow this Question
Related Questions
How do you get your character to look the way you are walking? 1 Answer
Transform.Position when Play changes 1 Answer
How do i make a "summon" object - and let the player face in its direction? 0 Answers
(2D sidescrolling platformer) Flipping my character right or left depending on the mouse? 2 Answers