hello everyone, I am working on a 2d platformer game prototype and I am having trouble animating my player across the screen. He faces right when I press the right arrow key to move him. When I press the left key, he is still faces right but moves in the opposite direction. I can't seem to make him face to the left. His animations are very simple to make because he doesn't have any legs. Is there a solution to this? I would greatly appreciate it. I hope to show off my progress once I’m done.
You could set the transform.localScale.x to -1 when facing left, and to 1 when facing right. I've also noticed that SpriteRenderer has an option to flip a sprite on the x or y axis, so that might be an option as well.
You could use a boolean and a function to do this (It's pretty simple actually) Code (CSharp): public bool facingRight = true; //Depends on if your animation is by default facing right or left void FixedUpdate() { float h = Input.GetAxis("Horizontal"); if(h > 0 && !facingRight) Flip(); else if(h < 0 && facingRight) Flip(); } void Flip () { facingRight = !facingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; }
this will work for sprite but for animation i used this code void OnTriggerStay2D(Collider2D other) { Debug.Log ("TriggerStart"); if (other.gameObject.tag == "Archer") { foreach (Collider2D collider in other.gameObject.GetComponents<Collider2D>()) { collider.isTrigger = true; if (collider is BoxCollider2D) { BoxCollider2D myBoxCollider = (BoxCollider2D)collider; archer.transform.Rotate(new Vector3(0, -180, 0)); Debug.Log ("Flip"); //Do some stuff with the box collider } else if (collider is CircleCollider2D) { CircleCollider2D myCircleCollider = (CircleCollider2D)collider; //Do some stuff with the cyrcle collider } } } but this is not working can u help me to solve??
Hello Try this link below to solve your problem http://answers.unity3d.com/questions/849595/flipping-animation-in-2d-sprites.html I guess that will be helpfull
It's an old question, but probably newbies like me will be asking still. So here is my solution: First I get the original scale of the game object at Start(): Code (CSharp): scale = this.gameObject.transform.localScale; Then I do the magic in the FixedUpdate() altogether with the movement. The booleans "movDir" and "movEsq" are set n Update() when I get the keyboard commands of "x" movement of the gameobject. Code (CSharp): private void FixedUpdate() { if (movDir) { movMov = 1; this.gameObject.transform.localScale = scale; } else if (movEsq) { movMov = -1; this.gameObject.transform.localScale = new Vector3(-1 * scale.x, scale.y, scale.z); } else { movMov = 0; } if (pulo && movMov != 0) this.gameObject.transform.Translate(new Vector3(movMov * movMax, 0, 0)); } I tried using rotation 180 degrees but didn't work, don't know why there is no mirroring as negative scale.
I think what people are looking for is making an animation follow a mouse input. Isn't there a way to create an animation and just have the animation orient towards an input? You have an animation of a ball moving right, but if you rotate the gameobject so right is now up, then the animation would still play right, but look as if it's going up. Great for directional melee combat for a top down 2d game
My solution: Code (CSharp): private void RotateAnimation() { if (Input.GetAxis("Horizontal") > 0.01f) gameObject.GetComponent<SpriteRenderer>().flipX = false; else if (Input.GetAxis("Horizontal") < -0.01f) gameObject.GetComponent<SpriteRenderer>().flipX = true; }