I’m trying to make my character reflect across the x axis to get him to face left when I press “A” or the left arrow key. He’ll face left but it quickly switches back and forth from facing left and right. I’m fairly new to unity and coding so it might just be that I’m being dumb. Here’s the code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
public float jumpHeight;
private KeyCode lastHitKey;
private PlayerController player;
public bool grounded;
public Rigidbody2D rb2d;
Animator animator;
bool facingRight;
void Start()
{
rb2d = GetComponent<Rigidbody2D> ();
animator = GetComponent<Animator>();
facingRight = true;
}
void Update()
{
movement ();
}
void flip(float horizontal)
{
if (!facingRight) {
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
void movement()
{
animator.SetFloat("Speed", Mathf.Abs (Input.GetAxis ("Horizontal")));
if (Input.GetKey (KeyCode.A))
{
facingRight = false;
}
if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(rb2d.velocity.x, jumpHeight);
}
if (Input.GetAxisRaw("Horizontal") > 0)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, rb2d.velocity.y);
}
if (Input.GetAxisRaw("Horizontal") < 0)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, rb2d.velocity.y);
flip(0);
}
}
}