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; private Transform groundCheck; public bool grounded = false; private bool jump = false; public float jumpForce = 30f; public bool isJump; // Use this for initialization void Start () { anim = GetComponent(); rig2d = GetComponent(); groundCheck = transform.Find("GroundCheck"); } // Update is called once per frame void Update () { h = Input.GetAxis("Horizontal"); grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("BlockingLayer")); if(Input.GetKey(KeyCode.Space) && grounded) { jump = true; } if (isJump) { if (grounded) { anim.StopPlayback(); isJump = false; } } } void JumpAniStop() { anim.StartPlayback(); isJump = true; } 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(); } if(jump) { jump = false; anim.SetTrigger("Jump_Start"); rig2d.AddForce(new Vector2(0f, jumpForce)); } } void Flip() { facingRight = !facingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; } }