SHARE
TWEET

Untitled

a guest Apr 21st, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Personaje : MonoBehaviour {
  2.  
  3. private Rigidbody2D myRigidBody;
  4.  
  5. private Animator myAnimator;
  6.  
  7. [SerializeField] //para poder editarlo en el inspector
  8. private float movementSpeed;
  9.  
  10. private bool attack;
  11.  
  12. private bool direccionPersonaje;
  13.  
  14. [SerializeField]
  15. private Transform[] groundPoints;
  16.  
  17. [SerializeField]
  18. private float groundRadius;
  19.  
  20. [SerializeField]
  21. private LayerMask whatIsGround;
  22.  
  23. private bool isGrounded;
  24.  
  25. private bool jump;
  26.  
  27. [SerializeField]
  28. private float jumpForce;
  29.  
  30. public GameObject leftBullet, rightBullet;
  31.  
  32. Transform firePos;
  33.  
  34. // Use this for initialization
  35. void Start () {
  36.  
  37.     direccionPersonaje = true;
  38.     //Referenciamos el rigidbody que tenemos en el player con la variable que hemos creado arriba
  39.     myRigidBody = GetComponent<Rigidbody2D>();
  40.     myAnimator = GetComponent<Animator>();
  41.     GetComponent<Rigidbody2D>().angularVelocity = 0;
  42.  
  43.     firePos = transform.FindChild ("firePos");
  44.  
  45. }
  46.  
  47. void Update()
  48. {
  49.     HandleInput();
  50. }
  51.  
  52. // Update is called once per frame
  53. void FixedUpdate()
  54. {
  55.     float horizontal = Input.GetAxis ("Horizontal");
  56.  
  57.     isGrounded = IsGrounded ();
  58.  
  59.     HandleMovement (horizontal);
  60.  
  61.     Flip (horizontal);
  62.  
  63.     if (Input.GetKeyDown (KeyCode.K))
  64.     {
  65.         Fire ();
  66.     }
  67.  
  68.     HandleAttacks();
  69.  
  70.     ResetValues();
  71.  
  72.  
  73. }
  74.  
  75. private void HandleMovement(float horizontal)
  76. {
  77.     if (!this.myAnimator.GetCurrentAnimatorStateInfo (0).IsTag ("Attack"))
  78.     {
  79.         myRigidBody.velocity = new Vector2 (horizontal * movementSpeed, myRigidBody.velocity.y);
  80.     }
  81.  
  82.     if (isGrounded && jump) {
  83.  
  84.         isGrounded = false;
  85.  
  86.         myRigidBody.AddForce (new Vector2 (0, jumpForce));
  87.  
  88.         myAnimator.SetTrigger ("Jump");
  89.     }
  90.  
  91.  
  92.  
  93.     myAnimator.SetFloat ("speed", Mathf.Abs (horizontal));
  94. }
  95.  
  96. private void HandleAttacks()
  97. {
  98.     if (attack)
  99.     {
  100.         myAnimator.SetTrigger("attack");
  101.         myRigidBody.velocity = Vector2.zero;
  102.     }
  103. }
  104.  
  105. private void HandleInput()
  106. {
  107.     if (Input.GetKeyDown (KeyCode.K))
  108.     {
  109.         attack = true;
  110.     }
  111.  
  112.     if (Input.GetKeyDown (KeyCode.Space)) {
  113.  
  114.         jump = true;
  115.     }
  116.  
  117. }
  118.  
  119. private void Flip(float horizontal)
  120. {
  121.     if (horizontal > 0 && !direccionPersonaje || horizontal < 0 && direccionPersonaje) {
  122.  
  123.         direccionPersonaje = !direccionPersonaje;
  124.  
  125.         Vector3 theScale = transform.localScale;
  126.  
  127.         theScale.x *= -1;
  128.  
  129.         transform.localScale = theScale;
  130.     }
  131. }
  132.  
  133. private void ResetValues()
  134. {
  135.     attack = false;
  136.     jump = false;
  137.  
  138. }
  139.  
  140. private bool IsGrounded (){
  141.  
  142.     if (myRigidBody.velocity.y <= 0) {  //si estamos parados en el eje y ( en el suelo)
  143.  
  144.         foreach (Transform point in groundPoints) {
  145.  
  146.             Collider2D[] colliders = Physics2D.OverlapCircleAll (point.position, groundRadius, whatIsGround);
  147.  
  148.             for (int i = 0; i < colliders.Length; i++) {
  149.  
  150.                 if (colliders [i].gameObject != gameObject) {
  151.  
  152.                     myAnimator.ResetTrigger ("Jump");
  153.                     return true;
  154.                 }
  155.             }
  156.         }
  157.     }
  158.     return false;
  159. }
  160.  
  161. void Fire()
  162. {
  163.     if (direccionPersonaje)
  164.     {
  165.         Instantiate(rightBullet, firePos.position, Quaternion.identity);
  166.     }
  167.  
  168.     if (!direccionPersonaje)
  169.     {
  170.         Instantiate(leftBullet, firePos.position, Quaternion.identity);
  171.     }
  172. }
RAW Paste Data
Top