Flip character to the left

Good Morning, could someone help me with any lines of code that would flip my character to face left the moment i pressed the left arrow so it would run to teh left? tried to find examples of it but due for what it uses i would like ot know if there would be any simple piece of coding to transform the character withn the space.world and leave it facing the pressed directionduring running and when its idle until its pressed the opposite direction

Best Regards

Change the X scale from 1 to -1. To achieve this you can use this.gameObject.transform.localScale.x property.

3 Likes

I heard some weird things can happen related to collision checking when you do negative scaling so I think it’s not a great idea to change localScale.
I think it would be better to rotate the sprite 180°:

transform.eulerAngles = new Vector3(0, 0, 0); // Normal

transform.eulerAngles = new Vector3(0, 180, 0); // Flipped
3 Likes

My issue with this is that if i press left he runs right if i use this code, any ideas on how to fix this?

I don’t know what your code for moving looks like but maybe it’s similar to this

http://forum.unity3d.com/threads/244412-2D-Character-rotation-Localscale-alternative

See last post (adding transform.right did the trick there) :wink:

using UnityEngine;
using System.Collections;

public class CharacterAnimatorScript : MonoBehaviour {

	Animator anim;
	bool isLeft;
	bool isRight;
	Vector3 X;

	// Use this for initialization
	void Start () {
		anim = GetComponent<Animator> ();
		X = transform.localScale;
		isRight = true;
	}
	
	// Update is called once per frame
	void Update () {
		if(!isLeft  (Input.GetKeyDown (KeyCode.A)||Input.GetKeyDown (KeyCode.LeftArrow))){
			isRight=false;
			isLeft=true;
//			X.z=-X.z;
//			transform.localScale = X;
			transform.eulerAngles = new Vector3(0, -90, 0);
		}
		if(!isRight  (Input.GetKeyDown (KeyCode.D)||Input.GetKeyDown (KeyCode.RightArrow))){
			isRight=true;
			isLeft=false;
//			X.z=-X.z;
//			transform.localScale = X;
			transform.eulerAngles = new Vector3(0, 90, 0);

		}


		float move = Input.GetAxis ("Vertical");
		if (move < 0) {
			move=-move;		
		}
		anim.SetFloat ("Speed", move);

		bool jumping = anim.GetCurrentAnimatorStateInfo(0).IsName("Jump");
		if (!jumping) {
			if (Input.GetKeyDown (KeyCode.Space)) {
					anim.SetBool ("Jump", true);
			}	
		} 
	}

}

Currently this is the code for changing the axis, tried with checking the link u showed me but didnt went much foward being that it resembled mine i tried adding that line but still nothing :\ (using a 3d character thus i rotated it and using the vertical so i have him move on the X)

I’m a bit confused, where in this code are you actually moving the character? Assuming that’s what you mean when you said:

ā€œMy issue with this is that if i press left he runs right if i use this codeā€

Or did you mean he faces right, not actually moving yet?

As long as my collider is squared up in the center, I’ve never had a problem with scaling from 1 to -1.

I don’t know, I heard people having trouble in the OnCollisionEnter/OnCollisionStay/OnCollisionExit functions when using -1 scaling but I might be wrong.

I also heard that in the next Unity update there should be an option for flipping your sprite :wink:

I know what you mean, here is how I have fixed that before:

if (Input.GetKey (KeyCode.D)) 
			{
				transform.Translate (Vector2.right * speed * Time.deltaTime);
				transform.eulerAngles = new Vector2(0,0); 
			}
			//Move Left
			if (Input.GetKey (KeyCode.A)) 
			{
				transform.Translate (Vector2.right * speed * Time.deltaTime);
				transform.eulerAngles = new Vector2(0,180); //flip the character on its x axis
			}

I kept the player sprite set to move right on the left direction key, then flipping it forced it to move left. Hope that makes sense.

1 Like

I dont have the variable speed since im using the 1st character prefab scripts (the solid figure)

1 Like

Ignoring my speed variable, you could probably use the same principle (guessing) as me in your code?

From reading your code and reading your problem in the first post I assume the character is facing right, when you press the D key it will stay looking right. When you press the left it still faces right but I’m not sure what part of the script is supposed to be making it actually move? You’d need a transform.Translate in there.

What I did was make both A+D move the character to the right, but on the transform.eulerAngles part for the ā€˜A’ key I told Unity to flip my character 180 on the x axis which then automatically made it go left.

Probably not the best method, and I’m sure someone else will explain a better way - but it worked for me!

Hi Disolution,

check out the 2D Character Controllers Video Tutorial from Unity. About minute 40 the Tutor implement what you want to do. Iam using this in my Plattformer also and it works fine for me.

best Regards

I second that. Definitely worth a watch. What I was suggesting was just a quick fix, if it worked in your case.

Gave it a watch and tried it issue is with rigidbody (taking off character controller/motor and fps input scripts) doesnt work tough the animations arent triggering

Fixed the issue with some tweaks.

In Unity 2018 and the new 2019 there is an option for Flipping Sprites in Sprite Renderer.

Oh btw I know I am posting a long time later.

can some one help me figure out why my ā€œflip playerā€ wont work.

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

public class PlayerMovment : MonoBehaviour
{
    public float speed;
   
    private Vector2 direction;
   
    public bool facingRight = true;
   
    // Start is called before the first frame update
    void Start()
    {
        direction = Vector2.up;
    }

    // Update is called once per frame
    void Update()
    {
        GetInput();
        Move();
    }
   
   
    public void Move()
    {
        transform.Translate(direction*speed*Time.deltaTime);
    }
   
    private void GetInput()
    {
        direction = Vector2.zero;
       
        if (Input.GetKey(KeyCode.W))
        {
            direction += Vector2.up;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            direction += Vector2.down;
        }
        if (Input.GetKey(KeyCode.A))
        {
            direction += Vector2.left;
        }
        else if (Input.GetKey(KeyCode.D))
        {
            direction += Vector2.right;
        }
    }
   
    void FlipPlayer()
    {
        facingRight = !facingRight;
       
        Vector2 localScale = gameObject.transform.localScale;
       
        localScale.x *= -1;
       
        transform.localScale = localScale;
       
    }
}
1 Like

I think, it’s because you don’t call function FlipPlayer() in Update()

1 Like