using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
private Rigidbody2D myRigidbody;
private Animator myAnimator;
[SerializeField]
private float movementSpeed;
private bool attack;
private bool slide;
private bool facingright;
// Use this for initialization
void Start () {
{
facingright = true;
myRigidbody = GetComponent<Rigidbody2D> (); }
myAnimator = GetComponent<Animator> ();
}
void Update()
{
HandleInput ();
}
// Update is called once per frame
void FixedUpdate()
{
float horizontal = Input.GetAxis ("Horizontal");
HandleMovement (horizontal);
flip(horizontal);
HandleAttacks();
ResetValues();
}
private void HandleMovement(float horizontal)
{
if (!this.myAnimator.GetCurrentAnimatorStateInfo (0).IsTag ("Attack"))
{
myRigidbody.velocity = new Vector2 (horizontal * movementSpeed, myRigidbody.velocity.y);
}
if (slide && !this.myAnimator.GetCurrentAnimatorStateInfo (0).IsName ("Slide")) {
{
myAnimator.SetBool ("slide", true);
}
else if (!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsName("slide"))
{
myAnimator.SetBool("slide", false);
}
{
myAnimator.SetFloat ("speed", Mathf.Abs(horizontal));
}}}}
-
private void HandleAttacks ();
if (attack && !this.myAnimator.GetCurrentAnimatorStateInfo (0).IsTag ("Attack")) { myAnimator.SetTrigger ("attack"); myRigidbody.velocity = Vector2.zero;
}
-
private void HandleInput()
{
if (Input.GetKeyDown (KeyCode.LeftShift))
{
attack = true;
}}
-
private void flip (float horizontal)
if (horizontal > 0 && !facingright || horizontal < 0 && facingright) { { facingright = !facingright; Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale;} }
-
private void ResetValues ()
{
attack = false;
slide = false;
}