using UnityEngine;
using System.Collections;

public class CharacterMoving : MonoBehaviour
{
    Rigidbody2D playerRigidBody2D;
    private bool facingRight;
    public float speed = 4.0f;
    float moveVector;
    private Animator anim;

    void Start()
    {

    }

	// Use this for initialization
	void Awake ()
    {
        playerRigidBody2D = (Rigidbody2D)GetComponent(typeof(Rigidbody2D));
        anim = (Animator)GetComponent(typeof(Animator));
	}
	
	// Update is called once per frame
	void Update ()
    {
        moveVector = Input.GetAxis("Horizontal");
        anim.SetFloat("speed", Mathf.Abs(moveVector));
        playerRigidBody2D.velocity = new Vector2(moveVector * speed,playerRigidBody2D.velocity.y);

        if (transform.position.x <= -11f)
        {
            transform.position = new Vector2(-11f,transform.position.y);
        }
       else  if (transform.position.x >= 11f)
        {
            transform.position = new Vector2(11f, transform.position.y);
        }

        if (moveVector > 0 && !facingRight)
            Flip();
        else if (moveVector < 0 && facingRight)
            Flip();
	
	}

    void Flip()
    {
        facingRight = !facingRight;
        Vector2 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale; 
    }
}
