using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
[SerializeField]
private float movementspeed;
private Rigidbody2D myRigidBody;
private Animator myAnimator;
private bool facingRight;
private bool attack;
private bool slide;
// 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;
}
if (Input.GetKeyDown(KeyCode.LeftControl))
{
slide = 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;
}