Mirror player gameobject to face direction of travel

Hi

I’m very new to all this, and just starting to get my head around how scripting works.

I’m trying to set up a simple 2d character to move around a map, changing from an idle animation to a walking animation when he moves. So far i have that working and can move him all around the map. What I can’t figure out how to do, is get the game object to flip when he wants to walk left. At the moment he just moonwalks when moving left.

I’ve tried a couple of things (commented out below) that have had limited results. What I want to do is have the x scale move to -1 when his movement is negative in the x axis. I know about ‘flip sprite’ but this doesn’t help as I have a light as part of the player game object and it ends up in the wrong place (ie it doesn’t flip with the sprite).

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

public class tl : MonoBehaviour 
{
	public float moveSpeed;
	Animator anim;
	Transform trans;

	// Use this for initialization
	void Start ()
	{
		//starting speed
		moveSpeed = 10f;
		anim = GetComponent<Animator> ();
		trans = GetComponent<Transform> ();
	}

	// Update is called once per frame
	void Update ()
	{
		//simple arrow movement
		transform.Translate (moveSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime, moveSpeed * Input.GetAxis ("Vertical") * Time.deltaTime, 0f);

		//Animation state controls
		if (Input.GetKeyDown (KeyCode.RightArrow)) {
			anim.SetInteger ("state", 1);
		}
		if (Input.GetKeyUp (KeyCode.RightArrow)) {
			anim.SetInteger ("state", 0);
		}
		if (Input.GetKeyDown (KeyCode.LeftArrow)) {
			anim.SetInteger ("state", 1);
		}
		if (Input.GetKeyUp (KeyCode.LeftArrow)) {
			anim.SetInteger ("state", 0);
		}
	}
}
		//Flip Player

		//if (moveSpeed*Input.GetAxis ("Horizontal") * Time.deltaTime < 0) {

		//trans.localScale = new Vector3 (transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
		//}
		//OR
		//if (Input.GetKeyDown (KeyCode.LeftArrow)) {
		//trans.localScale = new Vector3 (transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
		//}
		//if (Input.GetKeyDown (KeyCode.RightArrow)) {
		//trans.localScale = new Vector3 (transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
		//}

		//Hmmm
		//if (player = Vector2(-1, 0))
		//trans.localScale = new Vector3 (transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);

I’ve been getting nowhere for a couple of days now and could really do with some advice!

TIA!

hey @andrew-lukasik Thanks again!!

Thanks for the suggestion @andrew-lukasik. I put in the code you suggested, but it didn’t work:( I just get my character flipping every tick when I ask it to walk left (I’d achieved the same result with some of my code!).
I can’t believe that this is so difficult to achieve! All I need is my game object to face the direction its walking!!!

That’s great! Thanks! He’s doing what I want him to do now! The only thing is when I stop moving left (stop pressing ‘A’) he flips back to facing right. What do I need to have him remain facing left until i press right? Sorry if these seem like very basic questions, I’m really struggling with this at the moment!!