Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Assertions;
  6. using HutongGames.PlayMaker;
  7.  
  8. public class Player : MonoBehaviour {
  9.  
  10.     public bool dead;
  11.     public float speed = 4f;
  12.     public int health = 3;
  13.     public PlayMakerFSM UIManagerFSM;
  14.     Animator anim;
  15.     Rigidbody2D rb2d;
  16.     Vector2 mov;  // Ahora es visible entre los métodos
  17.  
  18.     CircleCollider2D attackCollider;
  19.  
  20.     bool movePrevent;
  21.     float facingVertical;
  22.     float facingHorizontal;
  23.  
  24.     void Start () {
  25.         anim = GetComponent<Animator>();
  26.         rb2d = GetComponent<Rigidbody2D>();
  27.         attackCollider = transform.GetChild(0).GetComponent<CircleCollider2D>();
  28.         attackCollider.enabled = false;  
  29.     }
  30.  
  31.     void Update () {  
  32.  
  33.         Movements ();
  34.         Animations ();
  35.         SwordAttack ();
  36.         PreventMovement ();
  37.     }
  38.  
  39.     void FixedUpdate () {
  40.         rb2d.MovePosition(rb2d.position + mov * speed * Time.deltaTime);
  41.     }
  42.  
  43.     void Movements () {
  44.         mov = new Vector2(
  45.             ControlFreak2.CF2Input.GetAxisRaw("Horizontal"),
  46.             ControlFreak2.CF2Input.GetAxisRaw("Vertical")
  47.         );
  48.        
  49.     }
  50.  
  51.     void Animations () {
  52.         if (mov != Vector2.zero) {
  53.             anim.SetFloat("movX", mov.x);
  54.             anim.SetFloat("movY", mov.y);
  55.             anim.SetBool("walking", true);
  56.             setPlayerFacing(mov.x,mov.y);
  57.         } else {
  58.             anim.SetBool("walking", false);
  59.             anim.SetFloat("movX", facingHorizontal);
  60.             anim.SetFloat("movY", facingVertical);
  61.         }
  62.     }
  63.  
  64.     void SwordAttack () {
  65.         AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);
  66.         bool attacking = stateInfo.IsName("Player_Attack");
  67.  
  68.         if (ControlFreak2.CF2Input.GetKeyDown(KeyCode.Space) && !attacking ){  
  69.             anim.SetTrigger("attacking");
  70.         }
  71.      
  72.         if(attacking) { // El normalized siempre resulta ser un ciclo entre 0 y 1
  73.             float playbackTime = stateInfo.normalizedTime;
  74.  
  75.             if (playbackTime > 0.05 && playbackTime < 0.7)
  76.             {
  77.                attackCollider.enabled = true;
  78.                attackCollider.offset = new Vector2(facingHorizontal/2f, facingVertical/2f);
  79.                attackCollider.offset = new Vector2(facingHorizontal/2.1f, facingVertical/2.1f);
  80.             }
  81.             else
  82.             {
  83.                 attackCollider.enabled = false;
  84.             }
  85.         }
  86.  
  87.     }
  88.     void PreventMovement () {
  89.         if (movePrevent) {
  90.             mov = Vector2.zero;
  91.         }
  92.     }
  93.  
  94.     IEnumerator EnableMovementAfter(float seconds){
  95.         yield return new WaitForSeconds(seconds);
  96.         movePrevent = false;
  97.     }
  98.  
  99.     public void Attacked(){
  100.         --health;
  101.         UIManagerFSM.SendEvent("Update");
  102.         if(health <= 0){
  103.             anim.SetBool("dead", true);
  104.             dead = true;
  105.             movePrevent = true;
  106.         }
  107.     }
  108.  
  109.     void setPlayerFacing(float x, float y)
  110.     {
  111.             facingVertical = 0;
  112.             facingHorizontal = 0;
  113.             if(y>0)
  114.             {
  115.                 facingVertical = 1.0f;
  116.             }
  117.             else if(y<0)
  118.             {
  119.                 facingVertical = -1.0f;
  120.             }
  121.             else if(x>0)
  122.             {
  123.                 facingHorizontal = 1.0f;
  124.             }else if(x<0)
  125.             {
  126.                 facingHorizontal = -1.0f;
  127.             }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement