Advertisement
Cryorus

Player Controller V3

Sep 24th, 2020
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.38 KB | None | 0 0
  1. using JetBrains.Annotations;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using UnityEngine;
  7. using UnityEngine.Rendering.LWRP;
  8. using UnityEngine.Experimental.Rendering.Universal;
  9. using UnityEditor.Experimental.GraphView;
  10.  
  11. public class PlayerController : MonoBehaviour
  12. {
  13.     Rigidbody2D rigid;
  14.     Animator animator; //con esto adhiero el Animator de Unity
  15.  
  16.     bool isDead;
  17.     float moveHorizontal; //global para poder usarla en Update y FixedUpdate
  18.     public float moveSpeed = 2f;
  19.     public float jumpForce = 8f;
  20.     public float maxJump = 2;
  21.     public float jumpCount;
  22.  
  23.     bool isFalling;
  24.     bool isJumping;
  25.  
  26.     public Transform groundCheck;
  27.     public float checkRadius = 0.1f;
  28.     public LayerMask whatIsGround;
  29.     bool isGrounded; //confirma si estoy chocando con Ground
  30.  
  31.     float dashTime;
  32.     public float dashSpeed = 10f;
  33.     public float maxDashTime = 0.1f;
  34.  
  35.     public DashState dashState;
  36.     public float dashCoolDown;
  37.     public float maxDashCoolDown = 3f;
  38.     public float countDash;
  39.  
  40.     void Start()
  41.     {
  42.         animator = GetComponent<Animator>(); //invoca al Aniimator
  43.         rigid = GetComponent<Rigidbody2D>();
  44.         dashTime = maxDashTime;
  45.         dashState = DashState.Ready;
  46.         jumpCount = maxJump;
  47.     }
  48.  
  49.     //Detecta si estoy tocando collider con Tag Ground
  50.     //Requiere un collider con Trigger
  51.     private void OnTriggerEnter2D(Collider2D collision)
  52.     {
  53.         if (collision.CompareTag("Cave"))
  54.         {
  55.             GetComponent<Light2D>().enabled = true;
  56.         } else if (collision.CompareTag("Death"))
  57.         {
  58.             //desabilitar mas colliders para componente "Death".. si no choca mas veces
  59.             isDead = true;
  60.             rigid.AddForce(new Vector2(-transform.localScale.x, 3f), ForceMode2D.Impulse);
  61.             animator.SetBool("isDead", true);
  62.            
  63.         }
  64.     }
  65.     //Inverso
  66.     private void OnTriggerExit2D(Collider2D collision)
  67.     {
  68.         if (collision.CompareTag("Ground"))
  69.         {
  70.             isGrounded = false;
  71.             Falling();
  72.         }
  73.         else if (collision.CompareTag("Cave"))
  74.         {
  75.             GetComponent<Light2D>().enabled = false;
  76.         }
  77.     }
  78.  
  79.     private void Dead()
  80.     {
  81.         Destroy(this.gameObject);
  82.     }
  83.  
  84.     //Invierte el eje X del sprite segun la dirección del movimiento
  85.     private void Flip(float movement)
  86.     {
  87.         if (movement > 0f)
  88.         {
  89.             transform.localScale = new Vector3(1f, 1f, 1f);
  90.         }
  91.         else if (movement < 0f)
  92.         {
  93.             transform.localScale = new Vector3(-1f, 1f, 1f);
  94.         }
  95.     }
  96.  
  97.     //movimiento horizontal del personaje
  98.     private void horizontalMovement(float move)
  99.     {
  100.         Vector3 movement;
  101.         //no se mueve si la velocidad no es distinta a 0
  102.         if (move != 0f)
  103.         {
  104.             movement = new Vector3(move, 0, 0);
  105.             //transform.position += movement * moveSpeed * Time.deltaTime;
  106.             transform.Translate(movement * moveSpeed * Time.deltaTime);
  107.             //rigid.AddForce(new Vector2(move, 0), ForceMode2D.Impulse);
  108.  
  109.             //si la velocidad absoluta es mayor a 0
  110.             if (Mathf.Abs(move) > 0f)
  111.             {
  112.                 animator.SetFloat("speed", 1); //para animator
  113.             }
  114.  
  115.             Flip(move); //mueve el eje x segun corresponda
  116.         }
  117.         else
  118.         {
  119.             animator.SetFloat("speed", 0); //si el movimiento es igual a cero, para animator
  120.         }
  121.     }
  122.  
  123.     //Jumping, solo se activa si se presiona la tecla
  124.     private void Jump()
  125.     {
  126.         if(jumpCount > 0)
  127.         {
  128.             //rigid.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
  129.  
  130.             if (isFalling)
  131.             {
  132.                 jumpCount = 1;
  133.                 isFalling = false;
  134.                 animator.SetBool("isFalling", false);
  135.             }
  136.  
  137.             if (jumpCount == maxJump)
  138.             {
  139.                 isJumping = true;
  140.                 rigid.velocity = Vector2.up * jumpForce;
  141.                 animator.SetBool("jumping", true); //para el animator
  142.             } else if(jumpCount < maxJump)
  143.             {
  144.                 isJumping = true;
  145.                 rigid.velocity = Vector2.up * jumpForce/1.2f;
  146.                 animator.SetBool("jumping", false);
  147.                 animator.SetBool("doubleJumping", true);
  148.             }
  149.  
  150.             jumpCount -= 1;
  151.         }
  152.     }
  153.  
  154.     private void groundedCheck()
  155.     {
  156.         if(rigid.velocity.y == 0)
  157.         {
  158.             isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
  159.             if (isGrounded)
  160.             {
  161.                 animator.SetBool("jumping", false);
  162.                 animator.SetBool("isFalling", false);
  163.                 animator.SetBool("doubleJumping", false);
  164.                 if (isJumping || isFalling)
  165.                 {
  166.                     isFalling = false;
  167.                     isJumping = false;
  168.                     if (dashState == DashState.Cooldown)
  169.                     {
  170.                         dashState = DashState.Ready;
  171.                     }
  172.                     jumpCount = maxJump;
  173.                     dashCoolDown = maxDashCoolDown;
  174.                 }
  175.             }
  176.  
  177.         } else
  178.         {
  179.             isGrounded = false;
  180.         }
  181.     }
  182.  
  183.     private void Falling()
  184.     {
  185.         if(rigid.velocity.y < 0 && !isJumping)
  186.         {
  187.             animator.SetBool("isFalling", true);
  188.             isFalling = true;
  189.         }
  190.     }
  191.  
  192.       private void Dash()
  193.     {
  194.         switch (dashState)
  195.         {
  196.             case DashState.Ready:
  197.                 if (Input.GetKeyDown(KeyCode.LeftShift))
  198.                 {
  199.                     rigid.velocity = new Vector2(transform.localScale.x, 0) * dashSpeed;
  200.                     dashState = DashState.Dashing;
  201.                 }
  202.                 break;
  203.  
  204.             case DashState.Dashing:
  205.                 animator.SetBool("isDashing", true);
  206.                 dashTime -= 0.01f;
  207.                 if (dashTime <= 0)
  208.                 {
  209.                     rigid.velocity = new Vector2(transform.localScale.x, 0) * 2f;
  210.                     animator.SetBool("isDashing", false);
  211.                     countDash = 0;
  212.                     dashTime = maxDashTime;
  213.                     dashState = DashState.Cooldown;
  214.                 }
  215.                 break;
  216.             case DashState.Cooldown:
  217.                 dashCoolDown -= Time.deltaTime;
  218.                 if (dashCoolDown <= 0)
  219.                 {
  220.                     dashCoolDown = maxDashCoolDown;
  221.                     dashState = DashState.Ready;
  222.                 }
  223.                 break;
  224.         }
  225.     }
  226.  
  227.     public enum DashState
  228.     {
  229.         Ready,
  230.         Dashing,
  231.         Cooldown
  232.     }
  233.  
  234.     // Update is called once per frame
  235.     void Update()
  236.     {
  237.         //moveHorizontal = Input.GetAxis("Horizontal"); //detecta si se esta moviendo el eje horizontal
  238.         horizontalMovement(Input.GetAxis("Horizontal"));
  239.         if (Input.GetButtonDown("Jump") && !isDead) {
  240.             Jump();
  241.         }
  242.         groundedCheck();
  243.         Dash();
  244.         if (transform.localScale.x == 0)
  245.         {
  246.             Jump();
  247.         }
  248.     }
  249.  
  250.     void FixedUpdate()
  251.     {
  252.         if (!isDead)
  253.         {
  254.             //horizontalMovement(moveHorizontal);
  255.         }
  256.  
  257.     }
  258. }
  259.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement