Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        grounded = false;
        if (!wallChecker.IsNearWalls())
        {
            grounded = wallChecker.IsGrounded();
        }
        switch (wallChecker.CheckSidesForWalls())
        {
        case Utils.Direction.RIGHT:
            grounded = wallChecker.CheckGround(Utils.Direction.LEFT);
            break;

        case Utils.Direction.LEFT:
            grounded = wallChecker.CheckGround(Utils.Direction.RIGHT);
            break;

        case Utils.Direction.BOTH:
            grounded = wallChecker.CheckGround(Utils.Direction.BOTH);
            break;
        }
        if (grounded && (Time.time - jumpTime) > 0.2)
        {
            jumpCounter = 0;
            canJump     = true;
            isJumping   = false;
            wallLeft    = false;
            wallRight   = false;
        }
        if (!grounded)
        {
            wallLeft  = (facingRight) ? wallChecker.CheckWall(Utils.Direction.LEFT) : wallChecker.CheckWall(Utils.Direction.RIGHT);
            wallRight = (facingRight) ? wallChecker.CheckWall(Utils.Direction.RIGHT) : wallChecker.CheckWall(Utils.Direction.LEFT);

            if (rb2d.velocity.x > 0 && wallRight)
            {
                rb2d.velocity = new Vector2(0, rb2d.velocity.y / 2);
            }
            if (rb2d.velocity.x < 0 && wallLeft)
            {
                rb2d.velocity = new Vector2(0, rb2d.velocity.y / 2);
            }
        }
        if (Input.GetButtonDown("jump"))
        {
            //If wall jump dont count jump
            if (wallChecker.CheckSidesForWalls() != Utils.Direction.NONE &&
                !grounded
                )
            {
                //Set Timer so other animitions wont get played
                jumpTime = Time.time;
                //Do jump

                anim.SetBool("WallJump", true);

                float velocity = movementSpeed * 2;
                if (!facingRight)
                {
                    velocity *= -1;
                }

                if (wallChecker.CheckSidesForWalls() == Utils.Direction.RIGHT)
                {
                    velocity *= -1;
                }
                rb2d.velocity = new Vector2(velocity, jumpVelocity * 1.2f);

                jumpCounter = 1;
                canJump     = true;
            }
            else if (canJump)
            {
                //Set Timer so other animitions wont get played
                jumpTime = Time.time;
                //Do jump
                anim.Play("PlayerJump");
                rb2d.velocity = new Vector2(rb2d.velocity.x, jumpVelocity);


                isJumping = true;
                jumpCounter++;
                //Check if maximum jumps in a row has been reached
                if (jumpCounter == maxJumps)
                {
                    canJump = false;
                }
            }
        }
    }