using UnityEngine; using System.Collections; public class player : MonoBehaviour { private Animator anim; private float h; private Rigidbody2D rig2d; public float moveForce = 50f; public float maxSpeed = 2f; private bool facingRight = true; // Use this for initialization void Start () { anim = GetComponent(); rig2d = GetComponent(); } // Update is called once per frame void Update () { h = Input.GetAxis("Horizontal"); } void FixedUpdate() { anim.SetFloat("Speed", Mathf.Abs(h)); if(h * rig2d.velocity.x < maxSpeed ) { //rig2d.velocity = new Vector2(Mathf.Sign(rig2d.velocity.x) * maxSpeed, rig2d.velocity.y); rig2d.AddForce(Vector2.right * h * moveForce); } 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; } }