Advertisement
Guest User

Movement

a guest
May 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 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.  
  28.         //* Recuperamos el collider de ataque y lo desactivamos
  29.         attackCollider = transform.GetChild(0).GetComponent<CircleCollider2D>();
  30.         attackCollider.enabled = false;  
  31.     }
  32.  
  33.     void Update () {  
  34.  
  35.         Movements ();
  36.         Animations ();
  37.         SwordAttack ();
  38.         PreventMovement ();
  39.     }
  40.  
  41.     void FixedUpdate () {
  42.         rb2d.MovePosition(rb2d.position + mov * speed * Time.deltaTime);
  43.     }
  44.  
  45.     void Movements () {
  46.         mov = new Vector2(
  47.             ControlFreak2.CF2Input.GetAxisRaw("Horizontal"),
  48.             ControlFreak2.CF2Input.GetAxisRaw("Vertical")
  49.         );
  50.        
  51.     }
  52.  
  53.     void Animations () {
  54.         if (mov != Vector2.zero) {
  55.             anim.SetFloat("movX", mov.x);
  56.             anim.SetFloat("movY", mov.y);
  57.             anim.SetBool("walking", true);
  58.             setPlayerFacing(mov.x,mov.y);
  59.         } else {
  60.             anim.SetBool("walking", false);
  61.             anim.SetFloat("movX", facingHorizontal);
  62.             anim.SetFloat("movY", facingVertical);
  63.         }
  64.     }
  65.  
  66.     void SwordAttack () {
  67.         AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);
  68.         bool attacking = stateInfo.IsName("Player_Attack");
  69.  
  70.         if (ControlFreak2.CF2Input.GetKeyDown(KeyCode.Space) && !attacking ){  
  71.             anim.SetTrigger("attacking");
  72.         }
  73.      
  74.         if(attacking) { // El normalized siempre resulta ser un ciclo entre 0 y 1
  75.             float playbackTime = stateInfo.normalizedTime;
  76.  
  77.             if (playbackTime > 0.05 && playbackTime < 0.7)
  78.             {
  79.                attackCollider.enabled = true;
  80.                //Moving the Collider so the Trigger Enter Work (I don´t know why)
  81.                attackCollider.offset = new Vector2(facingHorizontal/2f, facingVertical/2f);
  82.                attackCollider.offset = new Vector2(facingHorizontal/2.1f, facingVertical/2.1f);
  83.             }
  84.             else
  85.             {
  86.                 attackCollider.enabled = false;
  87.             }
  88.         }
  89.  
  90.     }
  91.     void PreventMovement () {
  92.         if (movePrevent) {
  93.             mov = Vector2.zero;
  94.         }
  95.     }
  96.  
  97.     IEnumerator EnableMovementAfter(float seconds){
  98.         yield return new WaitForSeconds(seconds);
  99.         movePrevent = false;
  100.     }
  101.  
  102.     public void Attacked(){
  103.         --health;
  104.         UIManagerFSM.SendEvent("Update");
  105.         if(health <= 0){
  106.             anim.SetBool("dead", true);
  107.             dead = true;
  108.             movePrevent = true;
  109.         }
  110.     }
  111.  
  112.     void setPlayerFacing(float x, float y)
  113.     {
  114.             facingVertical = 0;
  115.             facingHorizontal = 0;
  116.             if(y>0)
  117.             {
  118.                 facingVertical = 1.0f;
  119.             }else if(y<0)
  120.             {
  121.                 facingVertical = -1.0f;
  122.             }else if(x>0)
  123.             {
  124.                 facingHorizontal = 1.0f;
  125.             }else if(x<0)
  126.             {
  127.                 facingHorizontal = -1.0f;
  128.             }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement