Esempio n. 1
0
    void Update()
    {
        rb.gravityScale = 1f;

        float velX = Input.GetAxis("Horizontal") * xSpeed;
        float velY = rb.velocity.y;

        // Flip Player
        if (velX != 0 && state == State.idle)
        {
            facingRight = velX > 0;
            Vector3 scale = transform.localScale;
            scale.x = facingRight ? 1 : -1;
            transform.localScale = scale;
        }

        // Check ground and walls
        grounded   = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
        OnWall     = Physics2D.OverlapCircle(wallCheck.position, groundCheckRadius, groundLayer);
        OnWallBack = Physics2D.OverlapCircle(wallCheckBack.position, groundCheckRadius, groundLayer);


        if (unlock.Has("WallJump"))
        {
            // Trigger Wall Jump
            if (state == State.idle && !grounded && Input.GetButtonDown("Jump"))
            {
                if (OnWall)
                {
                    // normal
                    OnWall         = false;
                    OnWallBack     = false;
                    state          = State.walljump;
                    wallJumpTimer  = wallJumpTime;
                    wallStickTimer = 0f;
                    velY           = wallJumpSpeed.y;
                }
                if (OnWallBack)
                {
                    // facing away
                    OnWall         = false;
                    OnWallBack     = false;
                    state          = State.offwalljump;
                    wallJumpTimer  = wallJumpTime;
                    wallStickTimer = 0f;
                    velY           = wallJumpSpeed.y * (longJump ? longJumpMultipliers.y : 1);
                }
            }

            if (state == State.idle)
            {
                if (OnWall && !grounded && !OnWallBack)
                {
                    wallStickTimer = wallStickTime;
                }
                if (wallStickTimer > 0)
                {
                    wallStickTimer -= Time.deltaTime;
                    velX            = 0;
                }
                if (OnWall && Input.GetAxisRaw("Horizontal") != 0 && velY <= 0f)
                {
                    if (unlock.Has("WallStick"))
                    {
                        // user sticks to wall
                        velY            = 0;
                        rb.gravityScale = 0f;
                    }
                    else if (unlock.Has("WallSlide"))
                    {
                        // user slides down wall
                        rb.gravityScale = .3f;
                    }
                }
            }
        }

        // Jumping
        if (grounded && Input.GetButtonDown("Jump") && state == State.idle)
        {
            grounded = false;
            velY     = jumpHeight;
        }

        // Smart Jump
        if (state == State.idle && !OnWall && !OnWallBack)
        {
            if (velY < 0)
            {
                velY += Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
            }
            else if (velY > 0 && !Input.GetButton("Jump"))
            {
                velY += Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
            }
        }

        // Apply Wall Jump
        if (state == State.walljump || state == State.offwalljump)
        {
            wallJumpTimer -= Time.deltaTime;
            if (wallJumpTimer <= 0f)
            {
                state = State.idle;
            }
            else
            {
                float newVelX;
                if (state == State.walljump)
                {
                    newVelX = wallJumpSpeed.x * (!facingRight ? 1f : -1f);
                }
                else
                {
                    newVelX = wallJumpSpeed.x * (!facingRight ? -1f : 1f) * (longJump ? longJumpMultipliers.x : 1);
                }
                float i = wallJumpCurve.Evaluate((wallJumpTime - wallJumpTimer) / wallJumpTime);
                velX = (i * newVelX) + ((1f - i) * velX);
            }
        }

        // Set Velocity
        rb.velocity = new Vector2(velX, velY);

        // Component Updates
        melee.MeleeUpdate();
        projectile.ProjectileUpdate();
        dash.DashUpdate();
        knockback.KnockbackUpdate();

        // Switching Items
        if (Input.GetButtonDown("Fire3"))
        {
            if (equipped < 2)
            {
                equipped++;
            }
            else
            {
                equipped = 0;
            }
        }

        // Set playAnim triggers
        animator.SetBool("isWalking", velX != 0f);
        animator.SetBool("isGrounded", grounded);
        animator.SetFloat("verticalSpeed", rb.velocity.y);
    }
    void FixedUpdate()
    {
        grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
        float velX = Input.GetAxis("Horizontal") * xSpeed; // * Time.deltaTime;
        float velY = rb.velocity.y;

        if (velX != 0 && state != State.melee)
        {
            facingRight = velX > 0;
            Vector3 scale = transform.localScale;
            scale.x = facingRight ? 1 : -1;
            transform.localScale = scale;
        }

        // Set playerState
        if (state != State.melee)
        {
            if (velX == 0f)
            {
                state = State.idle;
            }
            else
            {
                state = State.walk;
            }
        }

        if (state == State.melee)
        {
            velX = 0;
        }

        // Jumping
        if (grounded && jumpButtonDown && state != State.melee)
        {
            jumpButtonDown = false;
            grounded       = false;
            velY           = jumpHeight;
        }

        // Smart Jump
        if (velY < 0)
        {
            velY += Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
        }
        else if (velY > 0 && !Input.GetButton("Jump"))
        {
            velY += Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
        }


        rb.velocity = new Vector2(velX, velY);

        // Component Updates
        melee.MeleeUpdate();
        projectile.ProjectileUpdate();

        // Switching Items
        if (Input.GetButtonDown("Fire3"))
        {
            if (equipped < 2)
            {
                equipped++;
            }
            else
            {
                equipped = 0;
            }
        }

        // Set playAnim triggers
        animator.SetBool("isWalking", state == State.walk);
        animator.SetBool("isGrounded", grounded);
        animator.SetFloat("verticalSpeed", rb.velocity.y);
    }