How do I flip my walk left animation to the right?

So I have a 2D sprite sheet with my character. I set up all the animations and I have them working minus the walk right animation. Here is my script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {
    public float speed;
    public Animator animator;

    private bool facingRight;
    // Use this for initialization
    void Start () {
        animator = GetComponent<Animator> ();
    }
       
   
    // Update is called once per frame
    void Update () {
       
        float axisX = Input.GetAxis ("Horizontal");
        float axisY = Input.GetAxis ("Vertical");

        if (Input.GetKey (KeyCode.W)) {
            animator.SetInteger ("AnimState", 1);
        }
        else if (Input.GetKey (KeyCode.S)) {
            animator.SetInteger ("AnimState", 2);
        }
        else if (Input.GetKey (KeyCode.D)) {
            animator.SetInteger ("AnimState", 3);
        }
        else if (Input.GetKey (KeyCode.A)) {
            if (axisX <= 0 && !facingRight || axisX >= 0 && facingRight) {
                facingRight = !facingRight;
                Vector3 theScale = transform.localScale;
                theScale.x *= -1;
                transform.localScale = theScale;
            }
            animator.SetInteger ("AnimState", 4);

        }


        else {
            animator.SetInteger ("AnimState", 0);
        }



        transform.Translate (new Vector3 (axisX, axisY) * Time.deltaTime * speed);
    }

}

Don’t know that you have to check the axis if you already have the Key ‘A’
I would just check after the key, if it’s already facing just do the animation, otherwise set the variable that you’re facing, change the scale, etc…
And do the same for the ‘D’ key?

I am a bit confused could you give me an example?

I assume D = right.

if (Input.GetKey(KeyCode.D)) {
   if (!facingRight) {
      facingRight = true;
      transform.localScale *= new Vector3(-1,1,1);
    }
     // etc , animation...
}
if (Input.GetKey(KeyCode.A)) {
   if(facingRight) {
      facingRight = false;
      transform.localScale *= new Vector3(-1,1,1);
      }
     // etc , animation...
 }

Like that?
technically, you wouldn’t even have to multiply here; you could simply set the correct one.
Hopefully I understood your question – just that moving one way wasn’t working… otherwise, oops… and what was the problem, if not this? :slight_smile:

It is giving me an error. It is saying “Operator ‘*=’ cannot be applied to UnityEngine.Vector3”

Well, just set the right one, then. Like D would be 1,1,1 and A -1,1,1
with ‘=’ instead of ‘*=’ :slight_smile:

:hushed::hushed::hushed::hushed::hushed::hushed::hushed: It just shrunk to 1 pixel!

lol omg well scale it to the right amount, sorry… like if the normal scale is 20, then put that…

I mean you could use your code where you make a new variable and scale just the x portion also… doesn’t matter. both are okay.
I just want to get to the part to see if the flipping works :stuck_out_tongue:

Still isn’t working. Here is what I changed

else if (Input.GetKey (KeyCode.D)) {
            if (!facingRight) {
                facingRight = true;
                transform.localScale = new Vector3(-100,100,1);
            }
            animator.SetInteger ("AnimState", 4);
        }
        else if (Input.GetKey (KeyCode.A)) {
            if (facingRight) {
                facingRight = true;
                transform.localScale = new Vector3(100,100,1);
            }
            animator.SetInteger ("AnimState", 4);
        }

k… i notice a couple of things. Usually I associate right with positive scale. You have yours reversed … that isn’t necessarily a problem, could just be a choice, I guess.

In the ‘A’ key , you wrote if Facing Right, set facing Right = true - that should be set to false.

And this part, I don’t know, but you set animstate to 4 on both keys, but in your first post, you had 3 & 4.

Ok that part is working. Thank you so much. Do you think you could help me fix another small glitch with it though? Say I hold W and then a few seconds I hold D. It shows my walk up animation, instead of switch to the walk right animation.

Cool, glad that’s working… Um… So, showing walk up but not turning, you mean?
What would you want to see if both were held down? W or D? I’m sure you want it to turn regardless, but is there a preference for the other two ?

I would like it to be what ever the last key I press is. So if I hit W and D I would want it to registrar D and stop the W animation

Well, right now your logic is going through a list of 4 key codes and short circuiting when it finds a match.

I guess you could maybe add to a list with GetKeyDown and use that animation, remove them when GetKeyUp is found?

You could keep the axis info if you’re moving as well and don’t want that changed?

Let’s say: W,A,S,D = 0,1,2,3
KeyDown W … list add 0 … starting up animation…
KeyDown D … list add 3 … starting right animation…
KeyUp D … remove 3 … check if it was list Count - 1 … if it was play List.Count - 2 (if Count - 2 >= 0)
Otherwise, say it was Down: W, D, A and D was up… just remove D… but since it wasn’t the end, A is still the “last” ?

Just thinking out loud lol :slight_smile:

I fixed it. I had to go into the animator and mess with the logic to intertwine with every combination. Thank you so much for everything!

3068949--230738--Capture2.PNG

Cool… Whatever ya did :wink: Glad ya got it working how you’d like. You’re welcome … enjoy your game :slight_smile: