Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Assertions;
- using HutongGames.PlayMaker;
- public class Player : MonoBehaviour {
- public bool dead;
- public float speed = 4f;
- public int health = 3;
- public PlayMakerFSM UIManagerFSM;
- Animator anim;
- Rigidbody2D rb2d;
- Vector2 mov; // Ahora es visible entre los métodos
- CircleCollider2D attackCollider;
- bool movePrevent;
- float facingVertical;
- float facingHorizontal;
- void Start () {
- anim = GetComponent<Animator>();
- rb2d = GetComponent<Rigidbody2D>();
- //* Recuperamos el collider de ataque y lo desactivamos
- attackCollider = transform.GetChild(0).GetComponent<CircleCollider2D>();
- attackCollider.enabled = false;
- }
- void Update () {
- Movements ();
- Animations ();
- SwordAttack ();
- PreventMovement ();
- }
- void FixedUpdate () {
- rb2d.MovePosition(rb2d.position + mov * speed * Time.deltaTime);
- }
- void Movements () {
- mov = new Vector2(
- ControlFreak2.CF2Input.GetAxisRaw("Horizontal"),
- ControlFreak2.CF2Input.GetAxisRaw("Vertical")
- );
- }
- void Animations () {
- if (mov != Vector2.zero) {
- anim.SetFloat("movX", mov.x);
- anim.SetFloat("movY", mov.y);
- anim.SetBool("walking", true);
- setPlayerFacing(mov.x,mov.y);
- } else {
- anim.SetBool("walking", false);
- anim.SetFloat("movX", facingHorizontal);
- anim.SetFloat("movY", facingVertical);
- }
- }
- void SwordAttack () {
- AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);
- bool attacking = stateInfo.IsName("Player_Attack");
- if (ControlFreak2.CF2Input.GetKeyDown(KeyCode.Space) && !attacking ){
- anim.SetTrigger("attacking");
- }
- if(attacking) { // El normalized siempre resulta ser un ciclo entre 0 y 1
- float playbackTime = stateInfo.normalizedTime;
- if (playbackTime > 0.05 && playbackTime < 0.7)
- {
- attackCollider.enabled = true;
- //Moving the Collider so the Trigger Enter Work (I don´t know why)
- attackCollider.offset = new Vector2(facingHorizontal/2f, facingVertical/2f);
- attackCollider.offset = new Vector2(facingHorizontal/2.1f, facingVertical/2.1f);
- }
- else
- {
- attackCollider.enabled = false;
- }
- }
- }
- void PreventMovement () {
- if (movePrevent) {
- mov = Vector2.zero;
- }
- }
- IEnumerator EnableMovementAfter(float seconds){
- yield return new WaitForSeconds(seconds);
- movePrevent = false;
- }
- public void Attacked(){
- --health;
- UIManagerFSM.SendEvent("Update");
- if(health <= 0){
- anim.SetBool("dead", true);
- dead = true;
- movePrevent = true;
- }
- }
- void setPlayerFacing(float x, float y)
- {
- facingVertical = 0;
- facingHorizontal = 0;
- if(y>0)
- {
- facingVertical = 1.0f;
- }else if(y<0)
- {
- facingVertical = -1.0f;
- }else if(x>0)
- {
- facingHorizontal = 1.0f;
- }else if(x<0)
- {
- facingHorizontal = -1.0f;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement