Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using JetBrains.Annotations;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using UnityEngine;
- using UnityEngine.Rendering.LWRP;
- using UnityEngine.Experimental.Rendering.Universal;
- using UnityEditor.Experimental.GraphView;
- public class PlayerController : MonoBehaviour
- {
- Rigidbody2D rigid;
- Animator animator; //con esto adhiero el Animator de Unity
- bool isDead;
- float moveHorizontal; //global para poder usarla en Update y FixedUpdate
- public float moveSpeed = 2f;
- public float jumpForce = 8f;
- public float maxJump = 2;
- public float jumpCount;
- bool isFalling;
- bool isJumping;
- public Transform groundCheck;
- public float checkRadius = 0.1f;
- public LayerMask whatIsGround;
- bool isGrounded; //confirma si estoy chocando con Ground
- float dashTime;
- public float dashSpeed = 10f;
- public float maxDashTime = 0.1f;
- public DashState dashState;
- public float dashCoolDown;
- public float maxDashCoolDown = 3f;
- public float countDash;
- void Start()
- {
- animator = GetComponent<Animator>(); //invoca al Aniimator
- rigid = GetComponent<Rigidbody2D>();
- dashTime = maxDashTime;
- dashState = DashState.Ready;
- jumpCount = maxJump;
- }
- //Detecta si estoy tocando collider con Tag Ground
- //Requiere un collider con Trigger
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if (collision.CompareTag("Cave"))
- {
- GetComponent<Light2D>().enabled = true;
- } else if (collision.CompareTag("Death"))
- {
- //desabilitar mas colliders para componente "Death".. si no choca mas veces
- isDead = true;
- rigid.AddForce(new Vector2(-transform.localScale.x, 3f), ForceMode2D.Impulse);
- animator.SetBool("isDead", true);
- }
- }
- //Inverso
- private void OnTriggerExit2D(Collider2D collision)
- {
- if (collision.CompareTag("Ground"))
- {
- isGrounded = false;
- Falling();
- }
- else if (collision.CompareTag("Cave"))
- {
- GetComponent<Light2D>().enabled = false;
- }
- }
- private void Dead()
- {
- Destroy(this.gameObject);
- }
- //Invierte el eje X del sprite segun la dirección del movimiento
- private void Flip(float movement)
- {
- if (movement > 0f)
- {
- transform.localScale = new Vector3(1f, 1f, 1f);
- }
- else if (movement < 0f)
- {
- transform.localScale = new Vector3(-1f, 1f, 1f);
- }
- }
- //movimiento horizontal del personaje
- private void horizontalMovement(float move)
- {
- Vector3 movement;
- //no se mueve si la velocidad no es distinta a 0
- if (move != 0f)
- {
- movement = new Vector3(move, 0, 0);
- //transform.position += movement * moveSpeed * Time.deltaTime;
- transform.Translate(movement * moveSpeed * Time.deltaTime);
- //rigid.AddForce(new Vector2(move, 0), ForceMode2D.Impulse);
- //si la velocidad absoluta es mayor a 0
- if (Mathf.Abs(move) > 0f)
- {
- animator.SetFloat("speed", 1); //para animator
- }
- Flip(move); //mueve el eje x segun corresponda
- }
- else
- {
- animator.SetFloat("speed", 0); //si el movimiento es igual a cero, para animator
- }
- }
- //Jumping, solo se activa si se presiona la tecla
- private void Jump()
- {
- if(jumpCount > 0)
- {
- //rigid.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
- if (isFalling)
- {
- jumpCount = 1;
- isFalling = false;
- animator.SetBool("isFalling", false);
- }
- if (jumpCount == maxJump)
- {
- isJumping = true;
- rigid.velocity = Vector2.up * jumpForce;
- animator.SetBool("jumping", true); //para el animator
- } else if(jumpCount < maxJump)
- {
- isJumping = true;
- rigid.velocity = Vector2.up * jumpForce/1.2f;
- animator.SetBool("jumping", false);
- animator.SetBool("doubleJumping", true);
- }
- jumpCount -= 1;
- }
- }
- private void groundedCheck()
- {
- if(rigid.velocity.y == 0)
- {
- isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
- if (isGrounded)
- {
- animator.SetBool("jumping", false);
- animator.SetBool("isFalling", false);
- animator.SetBool("doubleJumping", false);
- if (isJumping || isFalling)
- {
- isFalling = false;
- isJumping = false;
- if (dashState == DashState.Cooldown)
- {
- dashState = DashState.Ready;
- }
- jumpCount = maxJump;
- dashCoolDown = maxDashCoolDown;
- }
- }
- } else
- {
- isGrounded = false;
- }
- }
- private void Falling()
- {
- if(rigid.velocity.y < 0 && !isJumping)
- {
- animator.SetBool("isFalling", true);
- isFalling = true;
- }
- }
- private void Dash()
- {
- switch (dashState)
- {
- case DashState.Ready:
- if (Input.GetKeyDown(KeyCode.LeftShift))
- {
- rigid.velocity = new Vector2(transform.localScale.x, 0) * dashSpeed;
- dashState = DashState.Dashing;
- }
- break;
- case DashState.Dashing:
- animator.SetBool("isDashing", true);
- dashTime -= 0.01f;
- if (dashTime <= 0)
- {
- rigid.velocity = new Vector2(transform.localScale.x, 0) * 2f;
- animator.SetBool("isDashing", false);
- countDash = 0;
- dashTime = maxDashTime;
- dashState = DashState.Cooldown;
- }
- break;
- case DashState.Cooldown:
- dashCoolDown -= Time.deltaTime;
- if (dashCoolDown <= 0)
- {
- dashCoolDown = maxDashCoolDown;
- dashState = DashState.Ready;
- }
- break;
- }
- }
- public enum DashState
- {
- Ready,
- Dashing,
- Cooldown
- }
- // Update is called once per frame
- void Update()
- {
- //moveHorizontal = Input.GetAxis("Horizontal"); //detecta si se esta moviendo el eje horizontal
- horizontalMovement(Input.GetAxis("Horizontal"));
- if (Input.GetButtonDown("Jump") && !isDead) {
- Jump();
- }
- groundedCheck();
- Dash();
- if (transform.localScale.x == 0)
- {
- Jump();
- }
- }
- void FixedUpdate()
- {
- if (!isDead)
- {
- //horizontalMovement(moveHorizontal);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement