Flipping a player sprite?

So this is something that’s been bothering me probably the most out of all my Unity problems I’ve encountered throughout my game developing experience. Mainly because of how simple it should be, yet it’s so hard for me to figure out.

I do currently have something that sort of works, and I’m not sure if it will cause any issues later on, so I want to eliminate it.

if (Input.GetKeyDown (KeyCode.A)) {
            anim.SetBool ("Run", true);
            transform.localScale = new Vector3(-1f, 1, 1);
            anim.Play ("Walk");
        }
        else {
            anim.SetBool ("Run", false);
        }

        if ((Input.GetKeyDown (KeyCode.D))) {
            anim.SetBool ("Run", true);
            transform.localScale = new Vector3(1f, 1, 1);
        }
        else {
            anim.SetBool ("Run", false);
        }

This is what i have so far. The “Run” parameter is set to true when the player presses either the A or D key. And set to false when they aren’t pressing it. Therefore playing and stopping the “Walk” animation I have set up.

The problem here is that for flipping, I’m changing the local scale of the character’s sprite, and for some reason flipping it to the left, causes the animation to not play, so I have to make it play manually by calling it in the code. This is what i want to avoid, because I don’t know if it’s going to cause any problems.

I recently watched another tutorial, and this is what I got from it.

transform.localEulerAngles = new Vector3(0, 180, 0);

I used this instead of the first localScale line. The problem with this one, is that when I go left, the animation doesn’t play, and since the whole object is being span 180 degrees, the character moves to the right rather than to the left.

Could someone please tell me the easiest, and most up-to-date method of flipping the sprite, while still allowing for the animation to play when it’s flipped (without having to play it manually like I have done)?

SpriteRenderer.flipX

–Eric

1 Like

I’m getting an error when I set it to true.

Set the actual SpriteRenderer instance, not the class.

–Eric

1 Like

How would I do that?

GetComponent, as usual.

–Eric

1 Like

I keep getting errors. Here’s what I have so far.

void Start () {
        player = GetComponent<Player> ();
        anim = GetComponent<Animator> ();
        GetComponent<SpriteRenderer> ();
    }

        if (Input.GetKeyDown (KeyCode.A)) {
            SpriteRenderer.flipX = true;
            anim.SetBool ("Run", true);
        }
        else {
            anim.SetBool ("Run", false);
        }

        if (Input.GetKeyDown (KeyCode.D)) {
            SpriteRenderer.flipX = true;
            anim.SetBool ("Run", true);
        }
        else {
            anim.SetBool ("Run", false);
        }

The error is the same as in the first instance, where it says “an object reference is required…”. I know I must be doing this wrong.

When I tried to set a var name for “GetComponent” of the Sprite Renderer, and use that var name instead of “SpriteRenderer” I just get another weird error.

You have to assign the return value of GetComponent to a variable.

–Eric

1 Like

When I assign a variable name to it, I’m getting “The name “testFlip” Does not exist in the current context”.

Also, I just glanced at the above code I gave you, and I don’t know why, but I took out the Update method, and some other code.

Here is the full, and updated code (inside a public class):

    Animator anim;
    Player player;

    void Start () {
        player = GetComponent<Player> ();
        anim = GetComponent<Animator> ();
        testFlip = GetComponent<SpriteRenderer> ();
    }

    void Update () {
        Vector2 directionalInput = new Vector2(Input.GetAxisRaw("Horizontal"),
        Input.GetAxisRaw("Vertical"));
        player.SetDirectionalInput (directionalInput);

        if (Input.GetKeyDown (KeyCode.W) || (Input.GetKeyDown (KeyCode.Space))) {
            player.OnJumpInputDown ();
            anim.SetBool ("Jump", true);
        }

        if (Input.GetKeyUp (KeyCode.W) || (Input.GetKeyUp (KeyCode.Space))) {
            player.OnJumpInputUp ();
        }

        if (Input.GetKeyDown (KeyCode.A)) {
            testFlip.flipX = true;
            //transform.localScale = new Vector3(-1f, 1, 1);
            anim.SetBool ("Run", true);
            anim.Play ("Walk");
        }
        else {
            anim.SetBool ("Run", false);
        }

        if (Input.GetKeyDown (KeyCode.D)) {
            testFlip.flipX = true;
            //transform.localScale = new Vector3(1f, 1, 1);
            anim.SetBool ("Run", true);
        }
        else {
            anim.SetBool ("Run", false);
        }
    }
}

You didn’t make a testFlip variable.

–Eric

1 Like

Thx, did it now.

How should I set up the actual flipping? Because right now I have it to be changed to true, if either “A” or “D” is pressed. And what happens is when I press A, he flips but doesn’t play the animation, and when I then press D, he doesn’t flip, and the animation plays. So whenever I press A, he’s stuck facing left, and only playing the animation when moving right.

You would set it true if you want it to be flipped, and false otherwise.

–Eric

But that’s the problem, I have it set to true for both, and when I press “D”, it flips the sprite to the left, and plays the animation, and when I press “A”, either it turns to the left, or it doesn’t do anything (depending on whether it was already flipped or not).

The character starts facing to the right, and I don’t know how to make it so that it doesn’t flip if the button corresponding to the same side has been pressed.

And then there’s also the problem with the animation, where it doesn’t play when the character is flipped to the left.

If you set it true for both, then it will always be flipped.

–Eric

1 Like

Well how can I do it so it flips both ways, but does it properly?

I can’t set only one to true, because then it won’t flip when I go the opposite way.

but that’s exactly what you do, it’s always true.

Who are you replying to? Please use the reply button on the person’s post, as you’re clearly replying to what me and Eric have been discussing, so I don’t know who exactly you’re telling this. thanks.

His rely was immediately following yours; that’s what he’s replying to. Also that would be my response. It doesn’t seem that you understand the concept of flipping. If you set it true, then it’s flipped. If you set it false, then it’s not flipped.

–Eric

1 Like

@Denisowator I am replying to you from a mobile device, hence no formatting / quoting. Check line 26 & 36 of your code both set flipx to true.

Why don’t you just manually test it. Don’t execute the update function (comment out or return as first instruction) and add as the last line of the start function flipx = true then start, next time set it to false and start again. This way you ca see if it works

Edit: Eric’s and my response overlapped. We are both saying the same thing to you.

1 Like

From what I understand from both your replies (Eric and sngdan), you don’t seem to understand my issue. The flipping works, but it’s the way it works that’s the problem.

I did as sngdan said, and commented out the update function, then put the flipX into the start function. When it’s set to true, the character flips, and when it’s set to false, he doesn’t.

What I need, is for the character to flip in the direction he walks, so if I press “A” for left, he flips left, and to flip right, when I press “D”. But instead what happens, is he just flips once, even though both keys have the flip set to true (this is after I uncommented the update method).