Exemplo n.º 1
0
 private void DoJump()
 {
     if (coll.onGround)
     {
         Jump(Vector2.up, false);
         anim.SetTrigger("jump");
     }
 }
    private void DoJump()
    {
        // Sets the jump animation


        // What states can you jump from?
        // Maybe move to IDLE and/or RUNNING
        if (coll.onGround)
        {
            Jump(Vector2.up, false);
            anim.SetTrigger("jump");
        }
    }
Exemplo n.º 3
0
    private void Update()
    {
        if (health.CheckDead())
        {
            if (Input.GetButtonDown("Attack") && canAttack && currentState != PlayerState.Attack)
            {
                Attack();
            }

            if (Input.GetButton("Attack") && move.coll.onGround)
            {
                combo = true;
            }
            else if (Input.GetButtonUp("Attack") && !move.coll.onGround)
            {
                combo = false;
            }
            else if (Input.GetButtonDown("StrongAttack"))
            {
                if (canAttack && !move.coll.onGround && currentState != PlayerState.StrongAttack)
                {
                    anim.SetTrigger(StrongAttackAnimatorMapping);
                }
            }
            else if (Input.GetButtonDown("Bow") && canFire && currentState != PlayerState.Fire)
            {
                if (inventory.TakeArrow())
                {
                    Fire();
                }
            }
            else if (Input.GetButtonDown("Evade") && move.coll.onGround && canEvade && currentState != PlayerState.Evade)
            {
                anim.SetTrigger(EvadeAnimatorMapping);
            }
            else if (Input.GetButtonDown("CastSpell") && magic.CheckMagic(2f) && move.coll.onGround && currentState != PlayerState.Cast)
            {
                CastSpell();
            }
            else if (Input.GetButtonDown("Heal") && magic.CheckMagic(3f) && move.coll.onGround && currentState != PlayerState.Heal && !health.CheckFullHealth())
            {
                CastHeal();
            }
        }
    }
Exemplo n.º 4
0
    private IEnumerator AttackCo()
    {
        var previousState = currentState;

        currentState = PlayerState.Attack;
        anim.SetTrigger(AttackAnimatorMapping);
        yield return(new WaitForSeconds(1f));

        currentState = previousState;
    }
Exemplo n.º 5
0
    private void Dash(float x, float y)
    {
        // Graphics effects
        Camera.main.transform.DOComplete();
        Camera.main.transform.DOShakePosition(.2f, .5f, 14, 90, false, true);
        FindObjectOfType <RippleEffect>().Emit(Camera.main.WorldToViewportPoint(transform.position));

        // Put dash on cooldown
        hasDashed = true;

        anim.SetTrigger("dash");


        rb.velocity = Vector2.zero;
        Vector2 dir = new Vector2(x, y);

        rb.velocity += dir.normalized * dashSpeed;
        StartCoroutine(DashWait());
    }
Exemplo n.º 6
0
    // Update is called once per frame
    void Update()
    {
        if (dead)
        {
            return;
        }
        if (knockback)
        {
            if (coll.onGround && !recentlyKnockbacked)
            {
                GroundTouch();
                dead = life <= 0;
                if (dead)
                {
                    rb.velocity = Vector2.zero;
                }
                else
                {
                    knockback = false;
                }
            }
            return;
        }
        float   x    = Input.GetAxis("Horizontal");
        float   y    = Input.GetAxis("Vertical");
        float   xRaw = Input.GetAxisRaw("Horizontal");
        float   yRaw = Input.GetAxisRaw("Vertical");
        Vector2 dir  = new Vector2(x, y);

        Walk(dir);
        anim.SetHorizontalMovement(x, y, rb.velocity.y);

        if (coll.onGround && !isDashing)
        {
            wallJumped            = false;
            betterJumping.enabled = true;
        }

        if ((int)unlockedMoves >= 2 && coll.onWall && !coll.onGround)
        {
            if (x != 0)
            {
                wallSlide = true;

                WallSlide();
                remainingJumps = 1;
            }
        }

        if (!coll.onWall || coll.onGround)
        {
            wallSlide = false;
        }

        if ((int)unlockedMoves >= 1 && Input.GetButtonDown("Jump"))
        {
            anim.SetTrigger("jump");

            if (coll.onGround || ((int)unlockedMoves >= 4 && !coll.onWall && remainingJumps > 0))
            {
                Jump(Vector2.up, false);
                remainingJumps--;
            }
            else if ((int)unlockedMoves >= 2 && coll.onWall && remainingJumps > 0)
            {
                WallJump();
                remainingJumps = (int)unlockedMoves == 4 ? 1 : 0;
            }
        }

        if ((int)unlockedMoves >= 3 && Input.GetButtonDown("Fire1") && !hasDashed)
        {
            if (xRaw != 0)
            {
                Dash(xRaw);
            }
        }

        if (coll.onGround && !groundTouch)
        {
            GroundTouch();
            groundTouch = true;
        }

        if (!coll.onGround && groundTouch)
        {
            groundTouch = false;
        }

        WallParticle(y);

        if (wallSlide || !canMove)
        {
            return;
        }

        if (x > 0)
        {
            side = 1;
            anim.Flip(side);
        }
        if (x < 0)
        {
            side = -1;
            anim.Flip(side);
        }
    }
Exemplo n.º 7
0
    private void StateMachine(PlayerState state)
    {
        // This is where the code for each state goes
        switch (state)
        {
        case PlayerState.IDLE:

            // Condition: Horizontal input, go to RUNNING state
            if (xInput > 0.01f || xInput < -0.01f)
            {
                currentState = PlayerState.RUNNING;
            }

            if (!coll.onWall || coll.onGround)
            {
                wallSlide = false;
            }

            break;

        case PlayerState.RUNNING:

            // Use input direction to move and change the animation
            Walk(inputDirection);
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);

            // Condition: No horizontal input, go to IDLE state
            if (xInput <= 0.01f || xInput >= 0.01f)
            {
                currentState = PlayerState.IDLE;
            }

            // Otherwise use the horizontal input to flip the sprite
            if (xInput > 0)
            {
                side = 1;
                anim.Flip(side);
            }
            if (xInput < 0)
            {
                side = -1;
                anim.Flip(side);
            }

            break;

        case PlayerState.CLIMBING:

            // Stop gravity
            rb.gravityScale = 0;

            // Limit horizontal movement
            if (xInput > .2f || xInput < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            // Vertical Movement, slower when climbing
            float speedModifier = yInput > 0 ? .5f : 1;
            rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier));

            // Leave Condition:
            if (!coll.onWall || !Input.GetButton("Fire2"))
            {
                // Change state to default
                currentState = PlayerState.IDLE;

                // Reset Gravity
                rb.gravityScale = 3;
            }

            // Flips sprite based on which wall
            if (side != coll.wallSide)
            {
                anim.Flip(side * -1);
            }

            // Bools for movement and animation
            wallGrab  = true;
            wallSlide = false;

            break;

        // More states here pls

        case PlayerState.ON_WALL:
            // Condition: when not moving on wall, go to on_wall state
            // Slide down the wall
            wallSlide = true;
            WallSlide();


            break;

        case PlayerState.JUMPING:

            // Sets the jump animation
            anim.SetTrigger("jump");

            if (coll.onGround)
            {
                Jump(Vector2.up, false);
            }

            currentState = PlayerState.IDLE;

            break;
        }
    }
Exemplo n.º 8
0
    private void StateMachine(PlayerState state)
    {
        // This is where the code for each state goes
        switch (state)
        {
        case PlayerState.IDLE:
            isDashing = false;
            // Condition: Horizontal input, go to RUNNING state
            if (xInput > 0.01f || xInput < -0.01f)
            {
                currentState = PlayerState.RUNNING;
            }
            //If Jump pressed jump and set state to jump
            if (Input.GetButtonDown("Jump"))
            {
                // Sets the jump animation
                anim.SetTrigger("jump");
                currentState = PlayerState.JUMPING;
            }

            break;

        case PlayerState.RUNNING:
            isDashing = false;
            // Use input direction to move and change the animation
            Walk(inputDirection);
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);


            // Condition: No horizontal input, go to IDLE state
            if (xInput <= 0.01f || xInput >= 0.01f)
            {
                currentState = PlayerState.IDLE;
            }
            //If Dash pressed jump and set state to dashing
            if (Input.GetButtonDown("Fire1") && !isDashing)
            {
                // As long as there is some directional input
                if (xRaw != 0 || yRaw != 0)
                {
                    // Dash using raw input values
                    Dash(xRaw, yRaw);
                    isDashing    = true;
                    currentState = PlayerState.DASHING;
                }
            }
            //If Jump pressed jump and set state to jump
            if (Input.GetButtonDown("Jump"))
            {
                // Sets the jump animation
                anim.SetTrigger("jump");
                currentState = PlayerState.JUMPING;
            }


            break;

        case PlayerState.CLIMBING:
            //isDashing = false;
            // Stop gravity
            rb.gravityScale = 0;

            // Limit horizontal movement
            if (xInput > .2f || xInput < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            // Vertical Movement, slower when climbing
            float speedModifier = yInput > 0 ? .5f : 1;
            rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier));

            // Leave Condition:
            if (!coll.onWall || !Input.GetButton("Fire2"))
            {
                // Change state to default
                currentState = PlayerState.IDLE;

                // Reset Gravity
                rb.gravityScale = 3;
            }
            //If Jump pressed jump and set state to jump
            if (Input.GetButtonDown("Jump"))
            {
                // Sets the jump animation
                anim.SetTrigger("jump");
                currentState = PlayerState.JUMPING;
            }


            break;

        //DASHING State
        case PlayerState.DASHING:

            //If collides with ground default to idle, sets dashing to false ^
            if (coll.onGround)
            {
                // GroundTouch() resets the dash, as you can only dash once per jump
                isDashing    = false;
                currentState = PlayerState.IDLE;
            }
            //if collides with wall default to climbing, climbing sets dashing to false
            if (coll.onWall)
            {
                isDashing    = false;
                currentState = PlayerState.CLIMBING;
            }


            break;

        //Jumping state
        case PlayerState.JUMPING:
            //when jumping dashing is false
            //isDashing = false;
            //If collides with ground, default to idle and stop upward movement
            if (Input.GetButton("Fire1") && !isDashing)
            {
                // As long as there is some directional input
                if (xRaw != 0 || yRaw != 0)
                {
                    // Dash using raw input values
                    Dash(xRaw, yRaw);
                    isDashing    = true;
                    currentState = PlayerState.DASHING;
                }
            }

            if (coll.onGround)
            {
                Jump(Vector2.up, false);
                currentState = PlayerState.IDLE;
            }

            //If collides with wall default to climbing
            if (coll.onWall)
            {
                Jump(Vector2.up, false);
                currentState = PlayerState.CLIMBING;
            }

            //If dash button pressed dash in direction and change state to dashing. MUST HAVE AN X VELOCITY OR IT WONT WORK!!!


            break;
        }
    }
Exemplo n.º 9
0
    private void StateMachine(PlayerState state)
    {
        // This is where the code for each state goes
        switch (state)
        {
        case PlayerState.IDLE:

            // Condition: Horizontal input, go to RUNNING state
            if (xInput > 0.01f || xInput < -0.01f)
            {
                currentState = PlayerState.RUNNING;
            }

            // Switch to Jump state.
            // Jump when hitting the space bar
            if (Input.GetButtonDown("Jump"))
            {
                currentState = PlayerState.JUMPING;
            }


            break;

        case PlayerState.RUNNING:

            // Use input direction to move and change the animation
            Walk(inputDirection);
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);

            // Condition: No horizontal input, go to IDLE state
            if (xInput <= 0.01f || xInput >= 0.01f)
            {
                currentState = PlayerState.IDLE;
            }

            // Switch to Jump state.
            // Jump when hitting the space bar
            if (Input.GetButtonDown("Jump"))
            {
                currentState = PlayerState.JUMPING;
            }

            // If left click and if dash is not on cooldown
            if (Input.GetButtonDown("Fire1") && !hasDashed)
            {
                currentState = PlayerState.DASHING;
            }

            break;

        case PlayerState.CLIMBING:

            // Stop gravity
            rb.gravityScale = 0;

            // Limit horizontal movement
            if (xInput > .2f || xInput < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            // Vertical Movement, slower when climbing
            float speedModifier = yInput > 0 ? .5f : 1;
            rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier));

            // Leave Condition:
            if (!coll.onWall || !Input.GetButton("Fire2"))
            {
                // Change state to default
                currentState = PlayerState.IDLE;

                // Reset Gravity
                rb.gravityScale = 3;
            }

            break;

        // More states here pls
        case PlayerState.JUMPING:
            // Sets the jump animation
            anim.SetTrigger("jump");

            // What states can you jump from?


            // Maybe move to IDLE and/or RUNNING
            if (coll.onGround)
            {
                Jump(Vector2.up, false);

                //currentState = PlayerState.IDLE;
            }

            // When you have left the ground
            if (!coll.onGround && groundTouch)
            {
                groundTouch  = false;
                currentState = PlayerState.FALLING;
            }


            break;

        case PlayerState.FALLING:
            // When you land on the ground
            if (coll.onGround && !groundTouch)
            {
                // GroundTouch() resets the dash, as you can only dash once per jump
                GroundTouch();
                groundTouch  = true;
                currentState = PlayerState.IDLE;
            }

            // If left click and if dash is not on cooldown
            if (Input.GetButtonDown("Fire1") && !hasDashed)
            {
                currentState = PlayerState.DASHING;
            }

            break;

        case PlayerState.DASHING:
            // As long as there is some directional input
            if (xRaw != 0 || yRaw != 0)
            {
                // Dash using raw input values
                Dash(xRaw, yRaw);
            }

            if (coll.onGround)
            {
                currentState = PlayerState.IDLE;
            }
            else
            {
                currentState = PlayerState.FALLING;
            }

            break;

        case PlayerState.WALL_JUMPING:
            // Maybe move to an ON_WALL state
            if (coll.onWall && !coll.onGround)
            {
                WallJump();
                //currentState = PlayerState.IDLE;
            }


            // When you have left the ground
            if (!coll.onGround)
            {
                // GroundTouch() resets the dash, as you can only dash once per jump
                GroundTouch();
                groundTouch  = false;
                currentState = PlayerState.FALLING;
            }
            else if (coll.onGround)
            {
                GroundTouch();
                groundTouch  = false;
                currentState = PlayerState.IDLE;
            }


            break;

        case PlayerState.ON_WALL:
            // If the player is moving towards the wall
            if (xInput != 0 && !wallGrab)
            {
                // Slide down the wall
                wallSlide = true;
                WallSlide();
            }


            // Switch to Jump state.
            // Jump when hitting the space bar
            if (Input.GetButtonDown("Jump"))
            {
                currentState = PlayerState.WALL_JUMPING;
                wallJumped   = true;
            }

            if (coll.onGround && !groundTouch)
            {
                // GroundTouch() resets the dash, as you can only dash once per jump
                GroundTouch();
                groundTouch  = true;
                currentState = PlayerState.IDLE;
            }

            break;
        }
    }
Exemplo n.º 10
0
    // Update is called once per frame
    void Update()
    {
        // Set input data for easy access
        SetInputVariables();

        // Reset Gravity
        rb.gravityScale = 3;

        // Use the statemachine
        StateMachine(currentState);

        Debug.Log(currentState);
        // Can enter the climbing state from any state
        // You may want to move this depending on the states you add
        if (coll.onWall && Input.GetButton("Fire2") && canMove)
        {
            // Change state
            currentState = PlayerState.CLIMBING;

            // Flips sprite based on which wall
            if (side != coll.wallSide)
            {
                anim.Flip(side * -1);
            }

            // Bools for movement and animation
            wallGrab  = true;
            wallSlide = false;
        }

        // Used when no longer on a wall
        if (Input.GetButtonUp("Fire2") || !coll.onWall || !canMove)
        {
            wallGrab  = false;
            wallSlide = false;
        }

        // Old Climbing code
        if (wallGrab && !isDashing)
        {
            // All this movement code is now in the CLIMBING state
        }
        else
        {
            // Moved this to the leave condition in the CLIMBING state
            //rb.gravityScale = 3;
        }

        // Jump when hitting the space bar
        if (Input.GetButtonDown("Jump"))
        {
            // Sets the jump animation
            anim.SetTrigger("jump");
            currentState = PlayerState.JUMPING;

            // Maybe move to IDLE and/or RUNNING
            if (coll.onGround)
            {
                Jump(Vector2.up, false);
            }

            // Maybe move to an ON_WALL state
            if (coll.onWall && !coll.onGround)
            {
                WallJump();
            }
        }

        // If left click and if dash is not on cooldown
        if (Input.GetButtonDown("Fire1") && !hasDashed)
        {
            // As long as there is some directional input
            if (xRaw != 0 || yRaw != 0)
            {
                // Dash using raw input values
                Dash(xRaw, yRaw);
            }
        }

        // When you have left the ground
        if (!coll.onGround && groundTouch)
        {
            groundTouch = false;
        }

        // Return if on a wall
        if (wallGrab || wallSlide || !canMove)
        {
            return;
        }

        // Otherwise use the horizontal input to flip the sprite
        if (xInput > 0)
        {
            side = 1;
            anim.Flip(side);
        }
        if (xInput < 0)
        {
            side = -1;
            anim.Flip(side);
        }
    }
Exemplo n.º 11
0
    private void StateMachine(PlayerState state)
    {
        // This is where the code for each state goes
        switch (state)
        {
        case PlayerState.IDLE:

            // Condition: Horizontal input, go to RUNNING state
            if (xInput > 0.01f || xInput < -0.01f)
            {
                currentState = PlayerState.RUNNING;
            }

            // Jump when hitting the space bar
            if (Input.GetButtonDown("Jump"))
            {
                currentState = PlayerState.JUMPING;
            }

            if (coll.onWall && Input.GetButton("Fire2") && canMove)
            {
                // Change state
                currentState = PlayerState.CLIMBING;
            }

            if (coll.onWall && !coll.onGround)
            {
                currentState = PlayerState.ON_WALL;
            }

            if (!coll.onWall || coll.onGround)
            {
                wallSlide = false;
            }

            // If left click and if dash is not on cooldown
            if (Input.GetButtonDown("Fire1") && !hasDashed)
            {
                // As long as there is some directional input
                if (xRaw != 0 || yRaw != 0)
                {
                    // Dash using raw input values
                    Dash(xRaw, yRaw);
                    currentState = PlayerState.DASHING;
                }
            }

            break;

        case PlayerState.RUNNING:

            // Use input direction to move and change the animation
            Walk(inputDirection);
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);

            // Condition: No horizontal input, go to IDLE state
            if (xInput <= 0.01f || xInput >= 0.01f)
            {
                currentState = PlayerState.IDLE;
            }

            // Jump when hitting the space bar
            if (Input.GetButtonDown("Jump"))
            {
                currentState = PlayerState.JUMPING;
            }

            if (coll.onWall && Input.GetButton("Fire2") && canMove)
            {
                // Change state
                currentState = PlayerState.CLIMBING;
            }

            if (coll.onWall && !coll.onGround)
            {
                currentState = PlayerState.ON_WALL;
            }


            // If left click and if dash is not on cooldown
            if (Input.GetButtonDown("Fire1") && !hasDashed)
            {
                // As long as there is some directional input
                if (xRaw != 0 || yRaw != 0)
                {
                    // Dash using raw input values
                    Dash(xRaw, yRaw);
                    currentState = PlayerState.DASHING;
                }
            }

            break;

        case PlayerState.CLIMBING:

            // Flips sprite based on which wall
            if (side != coll.wallSide)
            {
                anim.Flip(side * -1);
            }

            // Bools for movement and animation
            wallGrab  = true;
            wallSlide = false;

            // Stop gravity
            rb.gravityScale = 0;

            // Limit horizontal movement
            if (xInput > .2f || xInput < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            // Vertical Movement, slower when climbing
            float speedModifier = yInput > 0 ? .5f : 1;
            rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier));

            if (coll.onWall && !Input.GetButton("Fire2"))     // changes state if the player stops holding the climbing button, but is still on the wall
            {
                currentState = PlayerState.ON_WALL;
            }

            // Leave Condition:
            if (!coll.onWall)
            {
                // Change state to default
                currentState = PlayerState.IDLE;

                // Reset Gravity
                rb.gravityScale = 3;
            }

            break;

        // More states here pls

        case PlayerState.ON_WALL:

            // If the player is moving towards the wall
            if (xInput != 0 && !wallGrab)
            {
                // Slide down the wall
                wallSlide = true;
                WallSlide();
            }

            if (coll.onWall && Input.GetButton("Fire2") && canMove)
            {
                // Change state
                currentState = PlayerState.CLIMBING;
            }

            // Jump when hitting the space bar
            if (Input.GetButtonDown("Jump"))
            {
                WallJump();
            }

            // Used when no longer on a wall
            if (!coll.onWall || !canMove)
            {
                wallGrab  = false;
                wallSlide = false;
                if (wallJumped)
                {
                    currentState = PlayerState.WALL_JUMPING;
                }
                else
                {
                    currentState = PlayerState.FALLING;
                }
            }

            // changes state back to idle when touching the ground
            if (coll.onGround)
            {
                if (coll.onGround)
                {
                    // GroundTouch() resets the dash, as you can only dash once per jump
                    GroundTouch();
                    currentState = PlayerState.IDLE;
                }
            }

            break;

        case PlayerState.JUMPING:

            // Sets the jump animation
            anim.SetTrigger("jump");

            if (coll.onGround)
            {
                Jump(Vector2.up, false);
            }


            if (coll.onWall && Input.GetButton("Fire2") && canMove)
            {
                // Change state
                currentState = PlayerState.CLIMBING;
            }

            if (coll.onWall && !coll.onGround)
            {
                currentState = PlayerState.ON_WALL;
            }

            // If left click and if dash is not on cooldown
            if (Input.GetButtonDown("Fire1") && !hasDashed)
            {
                // As long as there is some directional input
                if (xRaw != 0 || yRaw != 0)
                {
                    // Dash using raw input values
                    Dash(xRaw, yRaw);
                    currentState = PlayerState.DASHING;
                }
            }

            // When you land on the ground
            if (coll.onGround)
            {
                // GroundTouch() resets the dash, as you can only dash once per jump
                GroundTouch();
                currentState = PlayerState.IDLE;
            }

            break;

        case PlayerState.FALLING:     // falling state prevents the player from dashing again

            // When you land on the ground
            if (coll.onGround)
            {
                // GroundTouch() resets the dash, as you can only dash once per jump
                GroundTouch();
                currentState = PlayerState.IDLE;
            }

            break;

        case PlayerState.DASHING:

            if (coll.onWall && Input.GetButton("Fire2") && canMove)
            {
                // Change state
                currentState = PlayerState.CLIMBING;
            }

            if (coll.onWall && !coll.onGround)
            {
                currentState = PlayerState.ON_WALL;
            }

            if (!isDashing && !coll.onGround)
            {
                currentState = PlayerState.FALLING;     // sets the state to falling when the dash ends
            }
            // When you land on the ground
            if (coll.onGround)
            {
                // GroundTouch() resets the dash, as you can only dash once per jump
                GroundTouch();
                currentState = PlayerState.IDLE;
            }

            break;

        case PlayerState.WALL_JUMPING:

            // Sets the jump animation
            anim.SetTrigger("jump");

            if (hasDashed)
            {
                currentState = PlayerState.FALLING;
            }
            else
            {
                if (coll.onWall && Input.GetButton("Fire2") && canMove)
                {
                    // Change state
                    currentState = PlayerState.CLIMBING;
                }

                if (coll.onWall && !coll.onGround)
                {
                    currentState = PlayerState.ON_WALL;
                }

                // If left click and if dash is not on cooldown
                if (Input.GetButtonDown("Fire1") && !hasDashed)
                {
                    // As long as there is some directional input
                    if (xRaw != 0 || yRaw != 0)
                    {
                        // Dash using raw input values
                        Dash(xRaw, yRaw);
                        currentState = PlayerState.DASHING;
                    }
                }

                // When you land on the ground
                if (coll.onGround)
                {
                    // GroundTouch() resets the dash, as you can only dash once per jump
                    GroundTouch();
                    currentState = PlayerState.IDLE;
                }
            }
            break;
        }
    }
Exemplo n.º 12
0
    // Update is called once per frame
    void Update()
    {
        float   x    = Input.GetAxis("Horizontal");
        float   y    = Input.GetAxis("Vertical");
        float   xRaw = Input.GetAxisRaw("Horizontal");
        float   yRaw = Input.GetAxisRaw("Vertical");
        Vector2 dir  = new Vector2(x, y);

        Walk(dir);
        anim.SetHorizontalMovement(x, y, rb.velocity.y);

        if (Input.GetKeyDown(KeyCode.G))
        {
            invertedGravity      = !invertedGravity;
            transform.localScale = new Vector3(transform.localScale.x, -transform.localScale.y, transform.localScale.z);
        }

        if (coll.onWall && Input.GetButton("Fire3") && canMove)
        {
            if (side != coll.wallSide)
            {
                anim.Flip(side * -1);
            }
            wallGrab  = true;
            wallSlide = false;
        }

        if (Input.GetButtonUp("Fire3") || !coll.onWall || !canMove)
        {
            wallGrab  = false;
            wallSlide = false;
        }

        if (coll.onGround && !isDashing)
        {
            wallJumped = false;
            GetComponent <BetterJumping>().enabled = true;
        }

        if (wallGrab && !isDashing)
        {
            rb.gravityScale = 0;
            if (x > .2f || x < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            float speedModifier = y > 0 ? .5f : 1;

            rb.velocity = new Vector2(rb.velocity.x, y * (speed * speedModifier));
        }
        else
        {
            rb.gravityScale = (invertedGravity) ? -customGravity : customGravity;
        }

        if (coll.onWall && !coll.onGround)
        {
            if (x != 0 && !wallGrab)
            {
                wallSlide = true;
                WallSlide();
            }
        }

        if (!coll.onWall || coll.onGround)
        {
            wallSlide = false;
        }

        if (Input.GetButtonDown("Jump"))
        {
            anim.SetTrigger("jump");

            if (coll.onGround || coll.onGrab)
            {
                Jump(Vector2.up, false);
                transform.SetParent(null);

                if (coll.onGrab)
                {
                    lastGrabbedObject = coll.grabbedObject;
                }
            }

            if (coll.onWall && !coll.onGround)
            {
                WallJump();
            }
        }

        if (Input.GetButtonDown("Fire1") && !hasDashed)
        {
            if (xRaw != 0 || yRaw != 0)
            {
                Dash(xRaw, yRaw);
            }
        }

        if (coll.onEnemy)
        {
            Jump(Vector2.up, false);

            Invoke("DestroyEnemy", 0.2f);
        }

        if (coll.onGround && !groundTouch)
        {
            GroundTouch();
            groundTouch = true;
        }

        if (!coll.onGround && groundTouch)
        {
            groundTouch = false;
        }

        WallParticle(y);

        if (wallGrab || wallSlide || !canMove)
        {
            return;
        }

        if (x > 0)
        {
            side = 1;
            anim.Flip(side);
        }
        if (x < 0)
        {
            side = -1;
            anim.Flip(side);
        }

        if (coll.onGrab)
        {
            if (coll.grabbedObject != lastGrabbedObject)
            {
                rb.velocity     = Vector2.zero;
                rb.gravityScale = 0;
            }
        }
        else
        {
            lastGrabbedObject = null;
        }

        // Limitar la velocidad maxima
        rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxFallSpeed);
    }
Exemplo n.º 13
0
    private void StateMachine(PlayerState state)
    {
        // This is where the code for each state goes
        switch (state)
        {
        case PlayerState.GROUNDED:

            if (coll.onGround && !groundTouch)
            {
                // GroundTouch() resets the dash, as you can only dash once per jump
                GroundTouch();
                groundTouch = true;
            }

            Jump(Vector2.up, false);
            wallSlide = false;
            Walk(inputDirection);
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);

            // When on the ground and not dashing
            // You might want to move these to a state
            if (coll.onGround && !isDashing)
            {
                wallJumped = false;
                GetComponent <BetterJumping>().enabled = true;
            }

            // Condition: Horizontal input, go to RUNNING state
            if (xInput > 0.01f || xInput < -0.01f)
            {
                isDashing = true;
            }
            else
            {
                isDashing = false;
            }

            // Leave Condition: Jump when hitting the space bar
            if (Input.GetButtonDown("Jump"))
            {
                // Sets the jump animation
                currentState = PlayerState.AIRBORNE;
            }

            break;

        case PlayerState.AIRBORNE:

            anim.SetTrigger("jump");
            // When you have left the ground
            if (!coll.onGround && groundTouch)
            {
                groundTouch = false;
            }

            if (Input.GetButtonUp("Fire2") || !coll.onWall || !canMove)
            {
                wallGrab  = false;
                wallSlide = false;
            }

            //Leave Conditions
            if (!coll.onWall || coll.onGround)
            {
                currentState = PlayerState.GROUNDED;
            }

            if (coll.onWall && !coll.onGround)
            {
                // If the player is moving towards the wall
                if (xInput != 0 && !wallGrab)
                {
                    // Slide down the wall
                    wallSlide = true;
                    WallSlide();
                }

                currentState = PlayerState.ON_WALL;
            }
            break;

        case PlayerState.ON_WALL:

            // Stop gravity
            rb.gravityScale = 0;

            // Return if on a wall
            if (wallGrab || wallSlide || !canMove)
            {
                return;
            }

            // Limit horizontal movement
            if (xInput > .2f || xInput < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            // Vertical Movement, slower when climbing
            float speedModifier = yInput > 0 ? .5f : 1;
            rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier));

            // Leave Conditions

            if (coll.onWall && Input.GetButton("Fire2"))
            {
                // Change state
                WallJump();

                // Flips sprite based on which wall
                if (side != coll.wallSide)
                {
                    anim.Flip(side * -1);
                }


                // Bools for movement and animation
                wallGrab  = true;
                wallSlide = false;

                currentState = PlayerState.AIRBORNE;
            }

            if (!coll.onWall || !Input.GetButton("Fire2"))
            {
                // Change state to default
                currentState = PlayerState.AIRBORNE;

                // Reset Gravity
                rb.gravityScale = 3;
            }
            if (coll.onGround)
            {
                coll.onWall = false;
            }
            currentState = PlayerState.GROUNDED;

            break;

            // More states here pls
        }
    }
Exemplo n.º 14
0
    private void StateMachine(PlayerState state)
    {
        // This is where the code for each state goes
        switch (state)
        {
        case PlayerState.IDLE:

            // Condition: Horizontal input, go to RUNNING state
            if (xInput > 0.01f || xInput < -0.01f)
            {
                currentState = PlayerState.RUNNING;
            }

            // When on the ground and not dashing
            if (coll.onGround && !isDashing)
            {
                wallJumped = false;
                GetComponent <BetterJumping>().enabled = true;
            }

            if (!coll.onWall || coll.onGround)
            {
                wallSlide = false;
            }

            // Maybe move to IDLE and/or RUNNING
            if (coll.onGround)
            {
                Jump(Vector2.up, false);
            }

            break;

        case PlayerState.RUNNING:

            // Use input direction to move and change the animation
            Walk(inputDirection);
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);

            // Condition: No horizontal input, go to IDLE state
            if (xInput <= 0.01f || xInput >= 0.01f)
            {
                currentState = PlayerState.IDLE;
            }

            // When on the ground and not dashing
            if (coll.onGround && !isDashing)
            {
                wallJumped = false;
                GetComponent <BetterJumping>().enabled = true;
            }

            // Maybe move to IDLE and/or RUNNING
            if (coll.onGround)
            {
                Jump(Vector2.up, false);
            }

            break;

        case PlayerState.CLIMBING:

            // Stop gravity
            rb.gravityScale = 0;

            // Limit horizontal movement
            if (xInput > .2f || xInput < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            // Vertical Movement, slower when climbing
            float speedModifier = yInput > 0 ? .5f : 1;
            rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier));

            if (coll.onWall || !Input.GetButton("Fire2"))
            {
                // Flips sprite based on which wall
                if (side != coll.wallSide)
                {
                    anim.Flip(side * -1);
                }

                // Bools for movement and animation
                wallGrab  = true;
                wallSlide = false;
            }
            // Leave Condition:
            if (!coll.onWall || !Input.GetButton("Fire2"))
            {
                // Change state to default
                currentState = PlayerState.IDLE;

                // Reset Gravity
                rb.gravityScale = 3;

                wallGrab  = false;
                wallSlide = false;
            }

            break;

        // More states here pls

        case PlayerState.JUMP:

            // Sets the jump animation
            anim.SetTrigger("jump");

            break;

        case PlayerState.WALL:

            // When on the wall and not on the gorund
            if (coll.onWall && !coll.onGround)
            {
                // If the player is moving towards the wall
                if (xInput != 0 && !wallGrab)
                {
                    // Slide down the wall
                    wallSlide = true;
                    WallSlide();
                }


                // Maybe there could be an ON_WALL state?
                // Try it out!
            }

            // Maybe move to an ON_WALL state
            if (coll.onWall && !coll.onGround)
            {
                WallJump();
            }



            break;
        }
    }
Exemplo n.º 15
0
    // Update is called once per frame
    void Update()
    {
        if (!dd.isDeath)
        {
            float   x    = Input.GetAxis("Horizontal");
            float   y    = Input.GetAxis("Vertical");
            float   xRaw = Input.GetAxisRaw("Horizontal");
            float   yRaw = Input.GetAxisRaw("Vertical");
            Vector2 dir  = new Vector2(xRaw, yRaw);

            Walk(dir);

            anim.SetHorizontalMovement(xRaw, yRaw, rb.velocity.y, rb.velocity.x, vertigo);

            if (grounded && !isDashing)
            {
                wallJumped = false;
                GetComponent <BetterJumping>().enabled = true;
            }


            if (Input.GetButtonDown("Jump"))
            {
                if (grounded)
                {
                    Jump(Vector2.up);
                    anim.SetTrigger("jump");
                }
            }


            //DASH
            if (Input.GetKeyDown(KeyCode.J) && !hasDashed)
            {
                if (xRaw != 0 || yRaw != 0)
                {
                    Dash(xRaw, yRaw);
                }
            }

            if (grounded && !groundTouch)
            {
                GroundTouch();
                groundTouch = true;
            }

            if (!grounded && groundTouch)
            {
                groundTouch = false;
            }



            if (x > 0)
            {
                side = 1;
                anim.Flip(side);
            }
            if (x < 0)
            {
                side = -1;
                anim.Flip(side);
            }


            float scalay = transform.GetScaleY();
            if (vertigo == true)
            {
                rb.gravityScale = -10;

                //transform.Rotate(new Vector3(180, 0, 0));
                //sp.flipY = true;
                time += Time.deltaTime;
                if (time >= 0.5f)
                {
                    transform.SetScaleY(-1 * Mathf.Abs(scalay));
                    time = 0;
                }

                /* if (Cam)
                 * {
                 *   CamPlay.GetComponent<Transform>().position = new Vector3(Camo.x, Camo.y, 10f);
                 * }*/
            }
            else
            {
                rb.gravityScale = 10;

                time += Time.deltaTime;
                if (time >= 0.5f)
                {
                    transform.SetScaleY(1 * Mathf.Abs(scalay));
                    time = 0;
                }

                /* if (Cam)
                 * {
                 *   CamPlay.GetComponent<Transform>().position = new Vector3(Camo.x, Camo.y, -10f);
                 * }
                 */
            }
        }
    }
Exemplo n.º 16
0
    private void StateMachine(PlayerState state)
    {
        // This is where the code for each state goes
        switch (state)
        {
        case PlayerState.IDLE:

            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);

            // Condition: Horizontal input, go to RUNNING state
            if (xInput > 0.01f || xInput < -0.01f)
            {
                currentState = PlayerState.RUNNING;
            }

            break;

        case PlayerState.RUNNING:

            // Use input direction to move and change the animation
            rb.velocity = new Vector2(inputDirection.x * speed, rb.velocity.y);

            // Upadate animation
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);

            // Condition: No horizontal input, go to IDLE state
            if (xInput <= 0.01f || xInput >= 0.01f)
            {
                currentState = PlayerState.IDLE;
            }

            break;

        case PlayerState.CLIMBING:

            // Stop gravity
            rb.gravityScale = 0;

            // Upadate animation
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);

            // Limit horizontal movement
            if (xInput > .2f || xInput < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            // Vertical Movement, slower when climbing
            float speedModifier = yInput > 0 ? .5f : 1;
            rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier));

            // Leave Condition:
            if (!coll.onWall || !Input.GetButton("Fire2"))
            {
                // Change state to default
                currentState = PlayerState.IDLE;

                // Reset Gravity
                rb.gravityScale = 3;
            }

            // Flips sprite based on which wall
            if (side != coll.wallSide)
            {
                anim.Flip(side * -1);
            }

            break;

        // More states here pls

        case PlayerState.ON_WALL:

            // Flip if needed
            if (coll.wallSide != side)
            {
                anim.Flip(side * -1);
            }

            if (!canMove)
            {
                return;
            }

            // If the player is holding towards the wall...
            bool pushingWall = false;
            if ((rb.velocity.x > 0 && coll.onRightWall) || (rb.velocity.x < 0 && coll.onLeftWall))
            {
                pushingWall = true;
            }
            float push = pushingWall ? 0 : rb.velocity.x;

            // Move down
            rb.velocity = new Vector2(push, -slideSpeed);
            wallSlide   = true;

            // Back to idle
            currentState = PlayerState.IDLE;

            break;

        case PlayerState.JUMPING:

            // Sets the jump animation
            anim.SetTrigger("jump");

            // What states can you jump from?

            // Maybe move to IDLE and/or RUNNING
            if (coll.onGround)
            {
                Jump(Vector2.up, false);
            }

            // Back to idle
            if (xInput <= 0.01f || xInput >= 0.01f)
            {
                currentState = PlayerState.IDLE;
            }
            else
            {
                currentState = PlayerState.RUNNING;
            }

            break;

        case PlayerState.FALLING:

            groundTouch = true;
            hasDashed   = false;
            isDashing   = false;

            side = anim.sr.flipX ? -1 : 1;

            // Back to idle
            currentState = PlayerState.IDLE;

            break;

        case PlayerState.DASHING:
            // Graphics effects
            Camera.main.transform.DOComplete();
            Camera.main.transform.DOShakePosition(.2f, .5f, 14, 90, false, true);
            FindObjectOfType <RippleEffect>().Emit(Camera.main.WorldToViewportPoint(transform.position));

            // Put dash on cooldown
            hasDashed = true;

            anim.SetTrigger("dash");


            rb.velocity = Vector2.zero;
            Vector2 dir = new Vector2(xRaw, yRaw);

            rb.velocity += dir.normalized * dashSpeed;
            StartCoroutine(DashWait());

            // Back to idle
            currentState = PlayerState.IDLE;

            break;

        case PlayerState.WALL_JUMPING:

            // Flip sprite if needed
            if ((side == 1 && coll.onRightWall) || side == -1 && !coll.onRightWall)
            {
                side *= -1;
                anim.Flip(side);
            }

            // Disable movement while wall jumping
            StopCoroutine(DisableMovement(0));
            StartCoroutine(DisableMovement(.1f));

            // Set direction based on which wall
            Vector2 wallDir = coll.onRightWall ? Vector2.left : Vector2.right;

            // Jump using the direction
            Jump((Vector2.up / 1.5f + wallDir / 1.5f), true);

            // Back to idle
            currentState = PlayerState.IDLE;

            break;
        }
    }
    private void StateMachine(PlayerState state)
    {
        switch (state)
        {
        case PlayerState.IDLE:
            if (xInput > 0.01f || xInput < -0.01f)
            {
                currentState = PlayerState.RUNNING;
            }
            break;

        case PlayerState.RUNNING:
            Walk(inputDirection);
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);

            if (xInput <= 0.01f || xInput >= 0.01f)     // no horizontal go to IDEL
            {
                currentState = PlayerState.IDLE;
            }
            break;

        case PlayerState.CLIMBING:
            rb.gravityScale = 0;
            if (xInput > .2f || xInput < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            float speedModifier = yInput > 0 ? .5f : 1;

            rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier));
            //Leave condition
            if (!coll.onWall || !Input.GetButton("Fire2"))
            {
                currentState = PlayerState.IDLE;

                rb.gravityScale = 3;
            }
            break;

        case PlayerState.JUMPING:
            anim.SetTrigger("jump");

            if (coll.onGround)
            {
                Jump(Vector2.up, false);
            }

            currentState = PlayerState.IDLE;
            break;

        case PlayerState.DASHING:
            Camera.main.transform.DOComplete();
            Camera.main.transform.DOShakePosition(.2f, .5f, 14, 90, false, true);


            hasDashed = true;

            anim.SetTrigger("dash");

            rb.velocity = Vector2.zero;
            Vector2 dir = new Vector2(xInput, yInput);

            rb.velocity += dir.normalized * dashSpeed;
            StartCoroutine(DashWait());

            currentState = PlayerState.IDLE;
            break;

        case PlayerState.WALL_JUMPING:
            // Flip sprite if needed
            if ((side == 1 && coll.onRightWall) || side == -1 && !coll.onRightWall)
            {
                side *= -1;
                anim.Flip(side);
            }

            // Disable movement while wall jumping
            StopCoroutine(DisableMovement(0));
            StartCoroutine(DisableMovement(.1f));

            // Set direction based on which wall
            Vector2 wallDir = coll.onRightWall ? Vector2.left : Vector2.right;

            // Jump using the direction
            Jump((Vector2.up / 1.5f + wallDir / 1.5f), true);
            currentState = PlayerState.IDLE;
            break;

        case PlayerState.ON_WALL:
            if (coll.wallSide != side)
            {
                anim.Flip(side * -1);
            }

            if (!canMove)
            {
                return;
            }

            // If the player is holding towards the wall...
            bool pushingWall = false;
            if ((rb.velocity.x > 0 && coll.onRightWall) || (rb.velocity.x < 0 && coll.onLeftWall))
            {
                pushingWall = true;
            }
            float push = pushingWall ? 0 : rb.velocity.x;

            // Move down
            rb.velocity = new Vector2(push, -slideSpeed);

            currentState = PlayerState.IDLE;
            break;

        case PlayerState.FALLING:
            hasDashed = false;
            isDashing = false;

            side = anim.sr.flipX ? -1 : 1;

            currentState = PlayerState.IDLE;
            break;
        }
    }
Exemplo n.º 18
0
    private void StateMachine(PlayerState state)
    {
        // This is where the code for each state goes
        switch (state)
        {
        case PlayerState.IDLE:
            Debug.Log("IDLE");
            wallJumped = false;
            GetComponent <BetterJumping>().enabled = true;
            // Condition: Horizontal input, go to RUNNING state
            if (!coll.onWall || coll.onGround)
            {
                wallSlide = false;
            }
            if (xInput > 0.01f || xInput < -0.01f)
            {
                currentState = PlayerState.RUNNING;
            }
            if (Input.GetButtonDown("Jump"))
            {
                Jump(Vector2.up, false);
                currentState = PlayerState.JUMPING;
                anim.SetTrigger("jump");
            }
            if (Input.GetButtonDown("Fire1") && !hasDashed)
            {
                // As long as there is some directional input
                if (xRaw != 0 || yRaw != 0)
                {
                    currentState = PlayerState.DASHING;
                }
            }
            break;

        case PlayerState.RUNNING:
            Debug.Log("RUNNING");
            // Use input direction to move and change the animation
            wallJumped = false;
            GetComponent <BetterJumping>().enabled = true;
            Walk(inputDirection);
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);
            if (Input.GetButtonDown("Jump"))
            {
                Jump(Vector2.up, false);
                currentState = PlayerState.JUMPING;
                anim.SetTrigger("jump");
            }
            if (Input.GetButtonDown("Fire1") && !hasDashed)
            {
                // As long as there is some directional input
                if (xRaw != 0 || yRaw != 0)
                {
                    currentState = PlayerState.DASHING;
                }
            }
            // Condition: No horizontal input, go to IDLE state
            if (xInput == 0.00f && xInput == 0.00f)
            {
                currentState = PlayerState.IDLE;
            }

            break;

        case PlayerState.CLIMBING:
            Debug.Log("CLIMBING");
            // Stop gravity
            rb.gravityScale = 0;

            // Limit horizontal movement
            if (xInput > .2f || xInput < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            // Vertical Movement, slower when climbing
            float speedModifier = yInput > 0 ? .5f : 1;
            rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier));

            // Leave Condition:
            if (!coll.onWall || !Input.GetButton("Fire2"))
            {
                // Change state to default
                if (!hasDashed && !coll.onGround)
                {
                    currentState = PlayerState.JUMPING;
                }
                else if (hasDashed && !coll.onGround)
                {
                    currentState = PlayerState.FALLING;
                }
                else
                {
                    currentState = PlayerState.IDLE;
                }


                // Reset Gravity
                rb.gravityScale = 3;
            }

            break;

        // More states here pls

        case PlayerState.JUMPING:
            Debug.Log("JUMPING");
            Walk(inputDirection);
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);
            wallGrab  = false;
            wallSlide = false;
            if (coll.onGround && !groundTouch)
            {
                // GroundTouch() resets the dash, as you can only dash once per jump
                // GroundTouch();
                // groundTouch = true;
                currentState = PlayerState.IDLE;
            }
            if (Input.GetButtonDown("Fire1") && !hasDashed)
            {
                // As long as there is some directional input
                if (xRaw != 0 || yRaw != 0)
                {
                    currentState = PlayerState.DASHING;
                }
            }
            if (coll.onWall && !coll.onGround)
            {
                currentState = PlayerState.SLIDING;
            }
            break;

        case PlayerState.DASHING:
            Debug.Log("DASHING");
            wallGrab  = false;
            wallSlide = false;
            Dash(xRaw, yRaw);
            if (!coll.onGround)
            {
                currentState = PlayerState.FALLING;
            }
            else if (coll.onWall && !coll.onGround)
            {
                currentState = PlayerState.SLIDING;
            }
            else
            {
                currentState = PlayerState.IDLE;
            }
            break;

        case PlayerState.SLIDING:
            Debug.Log("SLIDING");
            groundTouch = false;
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);
            if (Input.GetButtonDown("Jump"))
            {
                if (coll.onWall && !coll.onGround)
                {
                    WallJump();
                    if (!hasDashed && !coll.onGround)
                    {
                        currentState = PlayerState.JUMPING;
                    }
                    else if (hasDashed && !coll.onGround)
                    {
                        currentState = PlayerState.FALLING;
                    }
                }
            }
            if (xInput != 0 && !wallGrab && coll.onWall && !coll.onGround)
            {
                // Slide down the wall
                wallSlide = true;
                WallSlide();
            }

            else if (!hasDashed && !coll.onGround)
            {
                currentState = PlayerState.JUMPING;
            }
            else if (hasDashed && !coll.onGround)
            {
                currentState = PlayerState.FALLING;
            }
            else
            {
                currentState = PlayerState.IDLE;
            }


            break;

        case PlayerState.FALLING:
            Debug.Log("FALLING");
            Walk(inputDirection);
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);
            wallGrab  = false;
            wallSlide = false;
            if (coll.onGround && !groundTouch)
            {
                // GroundTouch() resets the dash, as you can only dash once per jump
                GroundTouch();
                groundTouch  = true;
                currentState = PlayerState.IDLE;
            }
            else if (coll.onWall && !coll.onGround)
            {
                currentState = PlayerState.SLIDING;
            }
            break;
        }
    }
Exemplo n.º 19
0
    private void StateMachine(PlayerState state)
    {
        // This is where the code for each state goes
        switch (state)
        {
        case PlayerState.IDLE:

            wallSlide = false;


            // When on the ground and not dashing reset a few values
            if (coll.onGround)
            {
                wallJumped = false;
                GetComponent <BetterJumping>().enabled = true;
            }

            // Condition: Horizontal input, go to RUNNING state
            if (xInput > 0.01f || xInput < -0.01f)
            {
                SetState(PlayerState.RUNNING);
            }

            // Condition: Player wants to jump and is on ground, go to JUMPING state
            if (coll.onGround && jumpInput)
            {
                SetState(PlayerState.JUMPING);
            }

            // Condition: Player is on wall and not the ground, go to ON_WALL state
            if (coll.onWall && !coll.onGround)
            {
                SetState(PlayerState.ON_WALL);
            }

            if (!coll.onWall && !coll.onGround)
            {
                SetState(PlayerState.FALLING);
            }


            break;

        case PlayerState.RUNNING:

            wallSlide = false;

            // Use input direction to move and change the animation
            Walk(inputDirection);
            if (coll.onGround)
            {
                anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);
            }

            // Condition: No horizontal input, go to IDLE state or FALLING state based on if grounded
            if (xInput <= 0.01f || xInput >= 0.01f)
            {
                if (coll.onGround)
                {
                    SetState(PlayerState.IDLE);
                }
                else
                {
                    SetState(PlayerState.FALLING);
                }
            }

            if (coll.onGround && jumpInput)
            {
                SetState(PlayerState.JUMPING);
            }

            if (coll.onWall && !coll.onGround)
            {
                SetState(PlayerState.ON_WALL);
            }

            break;

        case PlayerState.CLIMBING:

            // Stop gravity
            rb.gravityScale = 0;

            // Limit horizontal movement
            anim.SetHorizontalMovement(0, yInput, rb.velocity.y);

            if (xInput > .2f || xInput < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            // Vertical Movement, slower when climbing
            float speedModifier = yInput > 0 ? .5f : 1;
            rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier));

            // Leave Condition:
            if (!coll.onWall || !Input.GetButton("Fire2"))
            {
                // Change state to default
                SetState(PlayerState.IDLE);

                // Reset Gravity
                rb.gravityScale = 3;
            }

            // Wall jumping code was going to be added here too, but the player would immediately re-attach to the wall
            // because they were still holding "Fire2" :(

            break;

        case PlayerState.ON_WALL:

            // If the player is moving towards the wall
            if (xInput != 0 && !wallGrab)
            {
                // Slide down the wall
                wallSlide = true;
                WallSlide();
            }

            // Condition: Drop off wall into IDLE state if player is on ground or player is not on wall
            if (coll.onGround || !coll.onWall)
            {
                SetState(PlayerState.IDLE);
            }

            // Condition: Drop off wall into RUNNING state if player inputs away from the wall
            if (coll.wallSide == Mathf.Sign(xInput))
            {
                SetState(PlayerState.RUNNING);
            }

            // Condition: If player wants to jump and has not yet wall jumped, go to WALL_JUMPING state
            if (jumpInput && !wallJumped)
            {
                SetState(PlayerState.WALL_JUMPING);
            }

            break;

        case PlayerState.JUMPING:

            //Jump into the air and return control to the player
            anim.SetTrigger("jump");
            Jump(Vector2.up, false);
            SetState(PlayerState.FALLING);

            break;

        case PlayerState.WALL_JUMPING:

            //Jump off the wall and return control to the player
            anim.SetTrigger("jump");
            WallJump();
            SetState(PlayerState.FALLING);

            break;

        case PlayerState.FALLING:

            wallSlide = false;

            anim.SetTrigger("jump");

            // Condition: Touching ground, go to IDLE state
            if (coll.onGround)
            {
                SetState(PlayerState.IDLE);
            }

            // Condition: Horizontal input, go to RUNNING state
            if (xInput > 0.01f || xInput < -0.01f)
            {
                SetState(PlayerState.RUNNING);
            }

            break;

        case PlayerState.DASHING:

            // As long as there is some directional input
            if (xRaw != 0 || yRaw != 0)
            {
                // Dash using raw input values
                Dash(xRaw, yRaw);
            }

            SetState(PlayerState.FALLING);

            break;
        }
    }
Exemplo n.º 20
0
    void Update()
    {
        ownTBear.SetActive(canThrow ? true : false);

        float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Vertical");

        if (!canMove)
        {
            x = 0; y = 0;
        }

        Vector2 dir = new Vector2(x, y);

        Walk(dir);

        anim.SetHorizontalMovement(x, y, rb.velocity.y);

        if (coll.triggerPush)
        {
            WallJump(true);
        }

        if (coll.onGround)
        {
            wallJumped = false;
            GetComponent <BetterJumping>().enabled = true;
        }

        rb.gravityScale = 3;

        if (coll.onWall && !coll.onGround)
        {
            if (x != 0)
            {
                wallSlide = true;
                WallSlide();
            }
        }

        if (!coll.onWall || coll.onGround)
        {
            wallSlide = false;
        }

        isWalking = (!coll.onWall && coll.onGround && x != 0) ? true : false;

        if (!stepSource.isPlaying && isWalking)
        {
            stepSource.Play(); // Step sounds
        }

        if (!isWalking)
        {
            stepSource.Stop();
        }

        if (wallSlide)
        {
            GetComponent <SoundEffects>().playSlideAudio();
        }
        else
        {
            GetComponent <SoundEffects>().stopSlideAudio();
        }

        if (Input.GetButtonDown("Jump") && canMove)
        {
            anim.SetTrigger("jump");

            if (coll.onGround)
            {
                Jump(Vector2.up, false);
            }
            if (coll.onWall && !coll.onGround)
            {
                WallJump(false);
            }
        }

        if (Input.GetButtonDown("Fire1") && canThrow && (playerLevel > 0))
        {
            anim.PrepareThrow();
        }

        if (Input.GetButtonDown("Fire2") && !canThrow && (playerLevel > 0))
        {
            TBearRetrieve();
        }

        if (coll.onGround && !groundTouch)
        {
            GroundTouch();
            groundTouch = true;
        }

        if (!coll.onGround && groundTouch)
        {
            groundTouch = false;
        }

        WallParticle(y);

        if (wallSlide || !canMove)
        {
            return;
        }

        FlipAnim(x);
    }
Exemplo n.º 21
0
    private void StateMachine(PlayerState state)
    {
        // This is where the code for each state goes
        switch (state)
        {
        case PlayerState.IDLE:

            // Condition: Horizontal input, go to RUNNING state
            if (xInput > 0.01f || xInput < -0.01f)
            {
                currentState = PlayerState.RUNNING;
            }

            // When on the ground and not on a wall
            if (!coll.onWall || coll.onGround)
            {
                wallSlide = false;
            }


            // When on the ground and not dashing
            if (coll.onGround && !isDashing)
            {
                wallJumped = false;
                GetComponent <BetterJumping>().enabled = true;
            }

            // When you land on the ground
            if (coll.onGround && !groundTouch)
            {
                // GroundTouch() resets the dash, as you can only dash once per jump
                GroundTouch();
                groundTouch = true;
            }

            break;

        case PlayerState.RUNNING:

            // Use input direction to move and change the animation
            Walk(inputDirection);
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);

            // Condition: No horizontal input, go to IDLE state
            if (xInput <= 0.01f || xInput >= 0.01f)
            {
                currentState = PlayerState.IDLE;
            }

            break;

        case PlayerState.CLIMBING:

            // Stop gravity
            rb.gravityScale = 0;

            // Limit horizontal movement
            if (xInput > .2f || xInput < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            // Vertical Movement, slower when climbing
            float speedModifier = yInput > 0 ? .5f : 1;
            rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier));

            // Leave Condition:
            if (!coll.onWall || !Input.GetButton("Fire2"))
            {
                // Change state to default
                currentState = PlayerState.IDLE;

                // Reset Gravity
                rb.gravityScale = 3;
            }

            break;

        // STATE EXPANSION BEGINS HERE

        // New state: ON_WALL

        case PlayerState.ON_WALL:

            // When on the wall and not on the ground
            if (coll.onWall && !coll.onGround)
            {
                // If the player is moving towards the wall
                if (xInput != 0 && !wallGrab)
                {
                    // Slide down the wall
                    wallSlide = true;
                    WallSlide();
                }

                WallJump();
            }

            break;

        // New state: JUMPING

        case PlayerState.JUMPING:

            // Activates jump animation
            anim.SetTrigger("jump");

            // Sets jumping to false if player collides with ground
            if (coll.onGround)
            {
                Jump(Vector2.up, false);
            }

            // Switches player's state back to idle
            currentState = PlayerState.IDLE;

            break;

        // New state: DASHING

        case PlayerState.DASHING:

            // As long as there is some directional input
            if (xRaw != 0 || yRaw != 0)
            {
                // Dash using raw input values
                Dash(xRaw, yRaw);
            }

            // Switches player's state back to idle
            currentState = PlayerState.IDLE;

            break;

            // STATE EXPANSION ENDS HERE
        }
    }
Exemplo n.º 22
0
    // Update is called once per frame
    void Update()
    {
        grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);



        float   x    = Input.GetAxis("Horizontal");
        float   y    = Input.GetAxisRaw("Vertical");
        float   xRaw = Input.GetAxisRaw("Horizontal");
        float   yRaw = Input.GetAxisRaw("Vertical");
        Vector2 dir  = new Vector2(x, y);

        Walk(dir);
        anim.SetHorizontalMovement(xRaw, yRaw, rb.velocity.y);

        if (coll.onWall && Input.GetKey(KeyCode.C) && canMove)
        {
            if (side != coll.wallSide)
            {
                anim.Flip(side * -1);
            }
            wallGrab  = true;
            wallSlide = false;
        }

        if (Input.GetKeyUp(KeyCode.C) || !coll.onWall || !canMove)
        {
            wallGrab  = false;
            wallSlide = false;
        }

        if (coll.onGround && !isDashing)
        {
            wallJumped = false;
        }
        if (Input.GetKeyDown(KeyCode.X) && hasDashed == true)
        {
            source.clip = dashNullSound;
            source.Play();
        }

        if (wallGrab && !isDashing)
        {
            if (wallGrabTime >= 0)
            {
                rb.gravityScale = 0;
                if (x > .2f || x < -.2f)
                {
                    rb.velocity = new Vector2(rb.velocity.x, 0);
                }

                float speedModifier = y > 0 ? .5f : 1;

                rb.velocity   = new Vector2(rb.velocity.x, y * (speed * speedModifier));
                wallGrabTime -= Time.deltaTime;
            }
            else
            {
                rb.gravityScale = gravity;
                wallGrab        = false;
            }
        }
        else
        {
            rb.gravityScale = gravity;
        }

        if (coll.onWall && !coll.onGround)
        {
            if (x != 0 && !wallGrab && y <= 0)
            {
                wallSlide = true;
                WallSlide();
            }
        }
        if (!coll.onWall || coll.onGround)
        {
            wallSlide = false;
        }



        if (grounded)
        {
            hangCounter = hangTime;
        }
        else
        {
            hangCounter -= Time.deltaTime;
        }
        if (Input.GetKeyDown(KeyCode.Z))
        {
            jumpBufferCount = jumpBufferLength;
        }
        else
        {
            jumpBufferCount -= Time.deltaTime;
        }



        //JUMP
        if (Input.GetKeyDown(KeyCode.Z) && coll.onWall && !grounded)
        {
            WallJump();
        }

        if (jumpBufferCount >= 0 && hangCounter > 0f)
        {
            Jump(Vector2.up, false);
            anim.SetTrigger("jump");
            jumpBufferCount = 0;
        }



        //////// DASHING
        ///
        if (grounded)
        {
            numberOfDashes = numberOfNewDashes;
        }

        if (Input.GetKeyDown(KeyCode.X) && numberOfDashes >= 0)
        {
            if (xRaw != 0 || yRaw != 0)
            {
                source.clip = dashSound;
            }
            source.Play();
            Dash(xRaw, yRaw);
        }
        // Dream Dash
        if (Dream == true)
        {
            StartCoroutine("DreamDash");
        }



        if (coll.onGround && !groundTouch)
        {
            GroundTouch();
            groundTouch = true;
        }

        if (!coll.onGround && groundTouch)
        {
            groundTouch = false;
        }

        WallParticle(y);

        if (wallGrab || wallSlide || !canMove)
        {
            return;
        }

        if (x > 0)
        {
            side = 1;
            anim.Flip(side);
        }
        if (x < 0)
        {
            side = -1;
            anim.Flip(side);
        }
    }
    private void StateMachine(PlayerState state)
    {
        // This is where the code for each state goes
        switch (state)
        {
        //The default state
        case PlayerState.IDLE:
            //Things that happen in the IDLE state
            #region IDLE Tasks
            // If not on the wall and on the ground
            if (!coll.onWall || coll.onGround)
            {
                wallSlide = false;
            }

            //Reset all dashing stuff based on if you are touching the ground
            if (coll.onGround && !groundTouch)
            {
                GroundTouch();
                groundTouch = true;
            }

            // When on the ground and not dashing
            if (coll.onGround && !isDashing)
            {
                wallJumped = false;
                GetComponent <BetterJumping>().enabled = true;
            }
            #endregion

            //ENTER DIFFERENT STATES
            #region IDLE Switches
            // Condition: Horizontal input, go to RUNNING state
            if (xInput > 0.01f || xInput < -0.01f)
            {
                currentState = PlayerState.MOVING;
            }

            // Try to jump when hitting space bar
            if (Input.GetButtonDown("Jump"))
            {
                // Sets the jump animation
                anim.SetTrigger("jump");

                //Condition: On the ground + jump button, go to JUMPING state
                if (coll.onGround)
                {
                    Jump(Vector2.up, false);
                    currentState = PlayerState.JUMPING;
                }
            }

            // Condition: On wall, go to ON WALL state
            if (coll.onWall)
            {
                currentState = PlayerState.ON_WALL;
            }
            #endregion
            break;

        case PlayerState.MOVING:

            //Things that happen in the MOVING state
            #region MOVING Tasks
            // When on the ground and not dashing
            if (coll.onGround && !isDashing)
            {
                wallJumped = false;
                GetComponent <BetterJumping>().enabled = true;
            }

            // Use input direction to move and change the animation
            Walk(inputDirection);
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);
            #endregion

            //ENTER DIFFERENT STATES
            #region MOVING Switches
            // Condition: No horizontal input, go to IDLE state
            if (xInput <= 0.01f || xInput >= 0.01f)
            {
                currentState = PlayerState.IDLE;
            }

            // If left click and if dash is not on cooldown
            if (Input.GetButtonDown("Fire1") && !hasDashed)
            {
                //Condition: Directional Input (and Fire1), go into DASHING state
                if (xRaw != 0 || yRaw != 0)
                {
                    // Dash using raw input values
                    Dash(xRaw, yRaw);
                    currentState = PlayerState.DASHING;
                }
            }

            // Jump when hitting the space bar
            if (Input.GetButtonDown("Jump"))
            {
                // Sets the jump animation
                anim.SetTrigger("jump");

                //Condition: On the ground + jump button, go to JUMPING state
                if (coll.onGround)
                {
                    Jump(Vector2.up, false);
                    currentState = PlayerState.JUMPING;
                }
            }
            #endregion

            break;

        case PlayerState.CLIMBING:
            //Things that happen in the CLIMBING state
            #region CLIMBING Tasks
            // Stop gravity
            rb.gravityScale = 0;

            // Limit horizontal movement
            if (xInput > .2f || xInput < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            //Checks for vertical movement
            if (yInput > .2f || yInput < -.2f)
            {
                wallClimb = true;
            }
            else
            {
                wallClimb = false;
            }

            // Vertical Movement, slower when climbing
            float speedModifier = yInput > 0 ? .5f : 1;
            rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier));
            #endregion

            //ENTER DIFFERENT STATES
            #region CLIMBING Switches
            //Condition: Player no longer on wall OR releases the FIRE button, go to IDLE state
            if (!coll.onWall || !Input.GetButton("Fire2"))
            {
                currentState = PlayerState.IDLE;

                wallGrab  = false;
                wallSlide = false;

                //Condition: player still on wall but no longer climbing, go to ON_WALL state
                if (coll.onWall)
                {
                    currentState = PlayerState.ON_WALL;
                }
                // Reset Gravity
                rb.gravityScale = 3;
            }
            #endregion
            break;

        case PlayerState.ON_WALL:
            //Things that happen in the ON_WALL state
            #region ON_WALL Tasks
            // When on the wall and not on the gorund
            if (coll.onWall && !coll.onGround)
            {
                // If the player is moving towards the wall
                if (xInput != 0 && !wallGrab)
                {
                    // Slide down the wall
                    wallSlide = true;
                    WallSlide();
                }
            }
            #endregion

            //ENTER DIFFERENT STATES
            #region ON_WALL Switches
            // Condition: Input is being given to (horizontally) move, go to MOVING state
            if (xInput > 0.01f || xInput < -0.01f)
            {
                currentState = PlayerState.MOVING;
            }

            // Condition: On wall and hold Fire2, go to CLIMBING state
            if (coll.onWall && Input.GetButton("Fire2") && canMove)
            {
                // Change state
                currentState = PlayerState.CLIMBING;

                // Flips sprite based on which wall
                if (side != coll.wallSide)
                {
                    anim.Flip(side * -1);
                }

                // Bools for movement and animation
                wallGrab  = true;
                wallSlide = false;
            }

            // Condition: Jump input, perform checks
            if (Input.GetButtonDown("Jump"))
            {
                // Sets the jump animation
                anim.SetTrigger("jump");

                //Condition: On the ground, go to JUMPING state
                if (coll.onGround)
                {
                    Jump(Vector2.up, false);
                    currentState = PlayerState.JUMPING;
                }

                //Condition: On the wall and not on the ground, go to WALL_JUMPING state
                if (coll.onWall && !coll.onGround)
                {
                    WallJump();
                    currentState = PlayerState.WALL_JUMPING;
                }
            }
            #endregion
            break;

        case PlayerState.JUMPING:
            //Things that happen in the JUMPING state
            #region JUMPING Tasks
            groundTouch = false;
            #endregion

            //ENTER DIFFERENT STATES
            #region JUMPING Switches
            // If left click and if dash is not on cooldown
            if (Input.GetButtonDown("Fire1") && !hasDashed)
            {
                //Condition: Directional Input (and Fire1), go into DASHING state
                if (xRaw != 0 || yRaw != 0)
                {
                    // Dash using raw input values
                    Dash(xRaw, yRaw);
                    currentState = PlayerState.DASHING;
                }
            }

            //Condition: Vertical velocity less than 0.1, go to FALLING state
            if (rb.velocity.y <= 0.1)
            {
                currentState = PlayerState.FALLING;
            }
            #endregion
            break;

        case PlayerState.FALLING:
            //Things that happen in the FALLING state
            #region FALLING Tasks

            #endregion

            //ENTER DIFFERENT STATES
            #region FALLING Switches
            //Condition: Hit the ground, exit the FALLING state
            if (coll.onGround && !groundTouch)
            {
                currentState = PlayerState.IDLE;
            }

            //Condition: Collider on wall, go to ON_WALL state
            if (coll.onWall)
            {
                currentState = PlayerState.ON_WALL;
            }

            // Condition: Horizontal input, go to RUNNING state
            if (xInput > 0.01f || xInput < -0.01f)
            {
                currentState = PlayerState.MOVING;
            }

            if (Input.GetButtonDown("Fire1") && !hasDashed)
            {
                // As long as there is some directional input
                if (xRaw != 0 || yRaw != 0)
                {
                    //Condition: Directional Input (and Fire1), go into DASHING state
                    Dash(xRaw, yRaw);
                    currentState = PlayerState.DASHING;
                    return;
                }
            }
            #endregion
            break;

        case PlayerState.DASHING:
            //Things that happen in the DASHING state
            #region DASHING Tasks

            #endregion

            //ENTER DIFFERENT STATES
            #region DASHING Switches
            //Condition: You're in the air after dashing, enter the JUMPING state (JUMPING will check if FALLING)
            if (!coll.onGround)
            {
                currentState = PlayerState.JUMPING;
                return;
            }
            //CONDITION: You're still on the ground after initiating dash, enter the MOVING state
            else
            {
                currentState = PlayerState.MOVING;
            }
            #endregion
            break;

        case PlayerState.WALL_JUMPING:

            //Things that happen in the WALL_JUMPING state
            #region WALL_JUMPING Tasks
            groundTouch = false;
            #endregion

            //ENTER DIFFERENT STATES
            #region WALL_JUMPING Switches
            // If left click and if dash is not on cooldown
            if (Input.GetButtonDown("Fire1") && !hasDashed)
            {
                //Condition: Directional Input (and Fire1), go into DASHING state
                if (xRaw != 0 || yRaw != 0)
                {
                    // Dash using raw input values
                    Dash(xRaw, yRaw);
                    currentState = PlayerState.DASHING;
                }
            }

            //Condition: Vertical velocity is less than 0.1 (margin of error), go to FALLING state
            if (rb.velocity.y <= 0.1)
            {
                currentState = PlayerState.FALLING;
            }
            #endregion
            break;
        }
    }
Exemplo n.º 24
0
    // Update is called once per frame
    void Update()
    {
        if (New_Can_Move)
        {
            if (Ledge_Is != 0)
            {
                Ledge_Timer -= Time.deltaTime;
                if (Ledge_Timer <= 0)
                {
                    if (Ledge_Is > 0)
                    {
                        rb.velocity += Vector2.right * LedgeSpeed;
                    }
                    else if (Ledge_Is < 0)
                    {
                        rb.velocity += Vector2.left * LedgeSpeed;
                    }
                    HasLedged      = true;
                    Str_WallJumped = false;
                    Ledge_Is       = 0;
                }
            }
            else
            {
                Ledge_Timer = 0.2f;
            }

            if (HasLedged)
            {
                HasLedged_Time -= Time.deltaTime;
                if (HasLedged_Time > 0 && HasLedged_Time <= 0.05f)
                {
                    rb.velocity = Vector2.zero;
                }
                if (HasLedged_Time <= 0)
                {
                    HasLedged = false;
                }
            }
            else
            {
                HasLedged_Time = 0.15f;
            }
            if (!HasLedged && Ledge_Is == 0)
            {
                float x = 0;
                float y = 0;

                x = Input.GetAxis("Horizontal");
                y = Input.GetAxis("Vertical");



                float x_t = x;
                float y_t = y;

                if (Input.GetButton("Horizontal"))
                {
                    if (Mathf.Abs(x) >= 0.5)
                    {
                        if (x > 0)
                        {
                            x_t = 1;
                        }
                        else
                        {
                            x_t = -1;
                        }
                    }
                    else
                    {
                        x_t = (x_t * 2) % 1;
                    }
                    if (Mathf.Abs(y) >= 0.5)
                    {
                        if (y > 0)
                        {
                            y_t = 1;
                        }
                        else
                        {
                            y_t = -1;
                        }
                    }
                    else
                    {
                        y_t = (y_t * 2) % 1;
                    }
                }
                else
                {
                    x_t = x_t / 2.0f;
                    y_t = y_t / 2.0f;
                }

                float xRaw = Input.GetAxisRaw("Horizontal");
                float yRaw = Input.GetAxisRaw("Vertical");
                dir = new Vector2(x_t, y_t);

                Walk(dir);
                anim.SetHorizontalMovement(x, y, rb.velocity.y);



                if (coll.onGround)
                {
                    enduranceBar = 100;
                    graceTimer   = graceJumpTime;
                }
                else
                {
                    graceTimer--;
                }

                if (Str_WallJumped && rb.velocity.y <= 0)
                {
                    Str_WallJumped = false;
                }

                if (coll.onWall && Input.GetButton("Fire3") && canMove && !Str_WallJumped && !isDashing)
                {
                    if (side != coll.wallSide)
                    {
                        anim.Flip(side * -1);
                    }
                    if (enduranceBar <= 0)
                    {
                        wallGrab  = false;
                        wallSlide = true;
                        WallSlide();
                    }
                    if (enduranceBar > 0)
                    {
                        wallGrab  = true;
                        wallSlide = false;
                    }
                }

                if (Input.GetButtonUp("Fire3") || !coll.onWall || !canMove)
                {
                    wallGrab  = false;
                    wallSlide = false;
                }



                if (coll.onGround && !isDashing)
                {
                    // ghost.makeGhost = false;
                    wallJumped = false;
                    GetComponent <BetterJumping>().enabled = true;
                }

                if (wallGrab && !isDashing)
                {
                    rb.gravityScale = 0;
                    if (x > .2f || x < -.2f)
                    {
                        rb.velocity = new Vector2(rb.velocity.x, 0);
                    }

                    float speedModifier = y > 0 ? .5f : 1;

                    rb.velocity   = new Vector2(rb.velocity.x, y * (speed * speedModifier));
                    enduranceBar -= wallGrabDecrease * Time.deltaTime;
                }
                else if (rb.velocity.y > -20)
                {
                    rb.gravityScale = 3;
                }
                else
                {
                    rb.gravityScale = 0;
                }

                if (coll.onWall && !coll.onGround && !isDashing)
                {
                    if (x != 0 && !wallGrab)
                    {
                        wallSlide = true;
                        WallSlide();
                    }
                }

                if (!coll.onWall || coll.onGround)
                {
                    wallSlide = false;
                }


                if (Input.GetKeyDown("c"))
                {
                    jumpBufferTimer = jumpBuffer;
                }

                if (jumpBufferTimer > 0)
                {
                    anim.SetTrigger("jump");
                    if (coll.onGround || graceTimer > 0)
                    {
                        Jump(Vector2.up, false);
                        graceTimer      = 0;
                        jumpBufferTimer = 0;
                    }

                    if (coll.onWall && !coll.onGround && !wallGrab)
                    {
                        WallJump();
                        enduranceBar   -= walljumpDecrease;
                        jumpBufferTimer = 0;
                    }

                    if (coll.onWall && !coll.onGround && wallGrab &&
                        (Input.GetKey("right") || Input.GetKey("left")))
                    {
                        WallJump();
                        enduranceBar   -= walljumpDecrease;
                        jumpBufferTimer = 0;
                    }

                    if (coll.onWall && !coll.onGround && wallGrab &&
                        !(Input.GetKey("right") || Input.GetKey("left")))
                    {
                        wallGrab       = false;
                        Str_WallJumped = true;
                        Jump(Vector2.up, true);
                        enduranceBar   -= walljumpDecrease;
                        jumpBufferTimer = 0;
                    }
                    jumpBufferTimer--;
                }

                if (isBounce)
                {
                    _audioSource[0].PlayOneShot(SFX_Spring, 1);
                    rb.velocity  = new Vector2(rb.velocity.x, 0);
                    rb.velocity += Vector2.up * Bo_Speed;
                    anim.SetTrigger("jump");
                    isBounce       = false;
                    _SFX_Pernament = true;
                }

                if (Ledge_judge.OnLedge && wallGrab && Input.GetKey("up"))
                {
                    if (coll.onRightWall)
                    {
                        side = 1;
                        anim.Flip(side);
                        wallGrab       = false;
                        Str_WallJumped = true;
                        Jump(Vector2.up, true);
                        anim.SetTrigger("jump");
                        Ledge_Is        = 1;
                        jumpBufferTimer = 0;
                    }
                    else if (coll.onLeftWall)
                    {
                        side = -1;
                        anim.Flip(side);
                        wallGrab       = false;
                        Str_WallJumped = true;
                        Jump(Vector2.up, true);
                        anim.SetTrigger("jump");
                        Ledge_Is        = -1;
                        jumpBufferTimer = 0;
                    }
                    return;
                }

                WallParticle(y);

                Blink_To_Red();


                if (Input.GetKeyDown("x") && !hasDashed && enduranceBar > 0)
                {
                    if (xRaw != 0 || yRaw != 0)
                    {
                        Dash(xRaw, yRaw);
                    }
                }

                if (coll.onGround && !groundTouch)
                {
                    GroundTouch();
                    groundTouch = true;
                }

                if (!coll.onGround && groundTouch)
                {
                    groundTouch = false;
                }

                if (wallGrab || wallSlide || !canMove)
                {
                    return;
                }
                if (!isDashing)
                {
                    if (wallGrab && !isDashing)
                    {
                    }
                    else if (rb.velocity.y > -20)
                    {
                        rb.gravityScale = 3;
                    }
                    else
                    {
                        rb.gravityScale = 0;
                    }

                    // rb.velocity = savevelocity;
                    //rb.gravityScale = 3;
                    GetComponent <BetterJumping>().enabled = true;
                    //wallJumped = false;
                    isDashing       = false;
                    ghost.makeGhost = false;
                    DashTime        = StartDashTime;
                }
                if (x > 0)
                {
                    side = 1;
                    anim.Flip(side);
                }

                if (x < 0)
                {
                    side = -1;
                    anim.Flip(side);
                }
            }
        }
    }
    private void StateMachine(PlayerState state)
    {
        // This is where the code for each state goes
        switch (state)
        {
        case PlayerState.IDLE:

            // Condition: Horizontal input, go to RUNNING state
            if (xInput > 0.01f || xInput < -0.01f)
            {
                currentState = PlayerState.RUNNING;
            }

            if (!coll.onWall || coll.onGround)
            {
                wallSlide = false;
            }

            if (Input.GetButtonDown("Jump"))
            {
                // Sets the jump animation
                anim.SetTrigger("jump");

                // What states can you jump from?

                // Maybe move to IDLE and/or RUNNING
                if (coll.onGround)
                {
                    Jump(Vector2.up, false);
                    currentState = PlayerState.JUMPING;
                }
            }

            break;

        case PlayerState.RUNNING:

            // Use input direction to move and change the animation
            Walk(inputDirection);
            anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y);

            // Condition: No horizontal input, go to IDLE state
            if (xInput <= 0.01f || xInput >= 0.01f)
            {
                currentState = PlayerState.IDLE;
            }

            if (Input.GetButtonDown("Jump"))
            {
                // Sets the jump animation
                anim.SetTrigger("jump");

                // What states can you jump from?

                // Maybe move to IDLE and/or RUNNING
                if (coll.onGround)
                {
                    Jump(Vector2.up, false);
                    currentState = PlayerState.JUMPING;
                }
            }

            break;

        case PlayerState.CLIMBING:

            // Stop gravity
            rb.gravityScale = 0;

            // Limit horizontal movement
            if (xInput > .2f || xInput < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            // Vertical Movement, slower when climbing
            float speedModifier = yInput > 0 ? .5f : 1;
            rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier));

            // Leave Condition:
            if (!coll.onWall || !Input.GetButton("Fire2"))
            {
                // Change state to default
                currentState = PlayerState.FALLING;

                // Reset Gravity
                rb.gravityScale = 3;
            }

            break;

        // More states here pls

        case PlayerState.ON_WALL:

            if (Input.GetButtonDown("Jump"))
            {
                // Sets the jump animation
                anim.SetTrigger("jump");

                // Maybe move to an ON_WALL state
                if (coll.onWall && !coll.onGround)
                {
                    WallJump();
                }

                currentState = PlayerState.IDLE;
            }

            break;

        case PlayerState.JUMPING:

            if (Input.GetButtonUp("Fire2") || !coll.onWall || !canMove)
            {
                wallGrab     = false;
                wallSlide    = false;
                currentState = PlayerState.FALLING;
            }

            break;

        case PlayerState.DASHING:

            if (coll.onGround && !isDashing)
            {
                wallJumped = false;
                GetComponent <BetterJumping>().enabled = true;
                currentState = PlayerState.IDLE;
            }

            break;

        case PlayerState.FALLING:

            if (coll.onGround && !groundTouch)
            {
                // GroundTouch() resets the dash, as you can only dash once per jump
                GroundTouch();
                groundTouch  = true;
                currentState = PlayerState.IDLE;
            }

            break;
        }
    }
Exemplo n.º 26
0
    // Update is called once per frame
    void Update()
    {
        float   x    = Input.GetAxis("Horizontal");
        float   y    = Input.GetAxis("Vertical");
        float   xRaw = Input.GetAxisRaw("Horizontal");
        float   yRaw = Input.GetAxisRaw("Vertical");
        Vector2 dir  = new Vector2(x, y);


        Walk(dir);
        anim.SetHorizontalMovement(x, y, rb.velocity.y);

        if (coll.onWall && Input.GetButton("Fire3") && canMove)
        {
            if (side != coll.wallSide)
            {
                anim.Flip(side * -1);
            }
            wallGrab  = true;
            wallSlide = false;
        }

        if (Input.GetButtonUp("Fire3") || !coll.onWall || !canMove)
        {
            wallGrab  = false;
            wallSlide = false;
        }

        if (coll.onGround && !isDashing)
        {
            wallJumped = false;
            GetComponent <BetterJumping>().enabled = true;
        }

        if (wallGrab && !isDashing)
        {
            rb.gravityScale = 0;
            if (x > .2f || x < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            float speedModifier = y > 0 ? .5f : 1;

            rb.velocity = new Vector2(rb.velocity.x, y * (speed * speedModifier));
        }
        else
        {
            rb.gravityScale = 3;
        }

        if (coll.onWall && !coll.onGround)
        {
            if (x != 0 && !wallGrab)
            {
                wallSlide = true;
                WallSlide();
            }
        }

        if (!coll.onWall || coll.onGround)
        {
            wallSlide = false;
        }

        if (Input.GetButtonDown("Jump"))
        {
            anim.SetTrigger("jump");

            if (coll.onGround)
            {
                Jump(Vector2.up, false);
            }
            if (coll.onWall && !coll.onGround)
            {
                WallJump();
            }
        }


        if (Input.GetButtonDown("Fire1") && !hasDashed)
        {
            if (xRaw != 0 || yRaw != 0)
            {
                Dash(xRaw, yRaw);
            }
        }

        if (coll.onGround && !groundTouch)
        {
            GroundTouch();
            groundTouch = true;
        }

        if (!coll.onGround && groundTouch)
        {
            groundTouch = false;
        }

        WallParticle(y);

        if (wallGrab || wallSlide || !canMove)
        {
            return;
        }

        if (x > 0)
        {
            side = 1;
            anim.Flip(side);
        }
        if (x < 0)
        {
            side = -1;
            anim.Flip(side);
        }
    }
Exemplo n.º 27
0
    // Update is called once per frame
    void Update()
    {
        // Set input data for easy access
        SetInputVariables();

        // Reset Gravity
        rb.gravityScale = 3;

        // Use the statemachine
        StateMachine(currentState);


        // Can enter the climbing state from any state
        // You may want to move this depending on the states you add
        if (coll.onWall && Input.GetButton("Fire2") && canMove)
        {
            // Change state
            currentState = PlayerState.CLIMBING;

            // Flips sprite based on which wall
            if (side != coll.wallSide)
            {
                anim.Flip(side * -1);
            }

            // Bools for movement and animation
            wallGrab  = true;
            wallSlide = false;
        }

        // Used when no longer on a wall
        if (Input.GetButtonUp("Fire2") || !coll.onWall || !canMove)
        {
            wallGrab  = false;
            wallSlide = false;
        }

        // When on the ground and not dashing
        // You might want to move these to a state
        if (coll.onGround && !isDashing)
        {
            wallJumped = false;
            GetComponent <BetterJumping>().enabled = true;
        }

        // Old Climbing code
        if (wallGrab && !isDashing)
        {
            // All this movement code is now in the CLIMBING state
        }
        else
        {
            // Moved this to the leave condition in the CLIMBING state
            //rb.gravityScale = 3;
        }

        // When on the wall and not on the gorund
        if (coll.onWall && !coll.onGround)
        {
            // If the player is moving towards the wall
            if (xInput != 0 && !wallGrab)
            {
                // Slide down the wall
                wallSlide = true;
                WallSlide();
            }

            // Maybe there could be an ON_WALL state?
            // Try it out!
        }

        // If not on the wall and on the ground
        // Maybe move this to IDLE?
        if (!coll.onWall || coll.onGround)
        {
            wallSlide = false;
        }

        // Jump when hitting the space bar
        if (Input.GetButtonDown("Jump"))
        {
            // Sets the jump animation
            anim.SetTrigger("jump");

            // What states can you jump from?

            // Maybe move to IDLE and/or RUNNING
            if (coll.onGround)
            {
                Jump(Vector2.up, false);
            }

            // Maybe move to an ON_WALL state
            if (coll.onWall && !coll.onGround)
            {
                WallJump();
            }
        }

        // If left click and if dash is not on cooldown
        if (Input.GetButtonDown("Fire1") && !hasDashed)
        {
            // As long as there is some directional input
            if (xRaw != 0 || yRaw != 0)
            {
                // Dash using raw input values
                Dash(xRaw, yRaw);
            }
        }

        // When you land on the ground
        if (coll.onGround && !groundTouch)
        {
            // GroundTouch() resets the dash, as you can only dash once per jump
            GroundTouch();
            groundTouch = true;
        }

        // When you have left the ground
        if (!coll.onGround && groundTouch)
        {
            groundTouch = false;
        }


        // Return if on a wall
        if (wallGrab || wallSlide || !canMove)
        {
            return;
        }

        // Otherwise use the horizontal input to flip the sprite
        if (xInput > 0)
        {
            side = 1;
            anim.Flip(side);
        }
        if (xInput < 0)
        {
            side = -1;
            anim.Flip(side);
        }

        // This code may need to stay outside of the states
        // Since IDLE, RUNNING, JUMPING, FALLING all still need to use this code
    }
Exemplo n.º 28
0
    // Update is called once per frame
    void Update()
    {
        float x    = Input.GetAxis("Horizontal");
        float y    = Input.GetAxis("Vertical");
        float xRaw = Input.GetAxisRaw("Horizontal");
        float yRaw = Input.GetAxisRaw("Vertical");

        float xpos = rb.position.x;     // Player x position
        float ypos = rb.position.y;     // Player y position
        float run  = rb.velocity.x;     // Player x velocity

        if (run > 7)
        {
            run = 7;
        }
        Vector2 dir = new Vector2(x, y);

        OSCHandler.Instance.SendMessageToClient("pd", "/unity/xpos", xpos);
        OSCHandler.Instance.SendMessageToClient("pd", "/unity/ypos", ypos);
        OSCHandler.Instance.SendMessageToClient("pd", "/unity/run", 1 - (Math.Abs(run) / 10));

        // Only play walking noise if player is on the ground
        if (coll.onGround && !isOnGround)
        {
            OSCHandler.Instance.SendMessageToClient("pd", "/unity/grounded", "ready");
            isOnGround = true;
        }
        else if (!coll.onGround && isOnGround)
        {
            OSCHandler.Instance.SendMessageToClient("pd", "/unity/grounded", "ready");
            isOnGround = false;
        }

        var main = ps.main;

        main.simulationSpeed = particleSpeed / 40;
        main.startSize       = (float)((particleSize - 26) * .015);

        Walk(dir);
        anim.SetHorizontalMovement(x, y, rb.velocity.y);

        if (coll.onWall && Input.GetButton("Fire3") && canMove)
        {
            if (side != coll.wallSide)
            {
                anim.Flip(side * -1);
            }
            wallGrab  = true;
            wallSlide = false;
        }

        if (Input.GetButtonUp("Fire3") || !coll.onWall || !canMove)
        {
            wallGrab  = false;
            wallSlide = false;
        }

        if (coll.onGround && !isDashing)
        {
            wallJumped = false;
            GetComponent <BetterJumping>().enabled = true;
        }

        if (wallGrab && !isDashing)
        {
            rb.gravityScale = 0;
            if (x > .2f || x < -.2f)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }

            float speedModifier = y > 0 ? .5f : 1;

            rb.velocity = new Vector2(rb.velocity.x, y * (speed * speedModifier));
        }
        else
        {
            rb.gravityScale = 3;
        }

        if (coll.onWall && !coll.onGround)
        {
            if (x != 0 && !wallGrab)
            {
                wallSlide = true;
                WallSlide();
            }
        }

        if (!coll.onWall || coll.onGround)
        {
            wallSlide = false;
        }

        if (Input.GetButtonDown("Jump"))
        {
            anim.SetTrigger("jump");

            if (coll.onGround)
            {
                Jump(Vector2.up, false);
            }
            if (coll.onWall && !coll.onGround)
            {
                WallJump();
            }
        }

        // Play dash distortion when player dashed.
        if (Input.GetButtonDown("Fire1") && !hasDashed)
        {
            OSCHandler.Instance.SendMessageToClient("pd", "/unity/dash", 100);
            if (xRaw != 0 || yRaw != 0)
            {
                Dash(xRaw, yRaw);
            }
        }

        if (coll.onGround && !groundTouch)
        {
            GroundTouch();
            groundTouch = true;
        }

        if (!coll.onGround && groundTouch)
        {
            groundTouch = false;
        }

        WallParticle(y);

        if (wallGrab || wallSlide || !canMove)
        {
            return;
        }

        if (x > 0)
        {
            side = 1;
            anim.Flip(side);
        }
        if (x < 0)
        {
            side = -1;
            anim.Flip(side);
        }
    }
Exemplo n.º 29
0
    void Update()
    {
        // Set input data for easy access
        SetInputVariables();

        // Reset Gravity
        rb.gravityScale = 3;

        // Use the statemachine
        StateMachine(currentState);

        //CLIMBING =====================================================================================================
        if (coll.onWall && Input.GetButton("Fire2") && canMove)
        {
            // Change state
            currentState = PlayerState.CLIMBING;

            // Flips sprite based on which wall
            if (side != coll.wallSide)
            {
                anim.Flip(side * -1);
            }

            // Bools for movement and animation
            wallGrab  = true;
            wallSlide = false;
        }
        //=======================================================================================================

        // Used when no longer on a wall
        if (Input.GetButtonUp("Fire2") || !coll.onWall || !canMove)
        {
            wallGrab  = false;
            wallSlide = false;
        }

        // When on the ground and not dashing
        // You might want to move these to a state
        if (coll.onGround && !isDashing)
        {
            wallJumped = false;
            GetComponent <BetterJumping>().enabled = true;
        }

        //WALL_SLIDE ==================================================================================================
        if (coll.onWall && !coll.onGround)
        {
            // If the player is moving towards the wall
            if (xInput != 0 && !wallGrab)
            {
                // Slide down the wall
                wallSlide    = true;
                currentState = PlayerState.WALL_SLIDE;
            }
        }
        //======================================================================================================

        //JUMPING & WALL_JUMP =========================================================================================
        if (Input.GetButtonDown("Jump"))
        {
            // Sets the jump animation
            anim.SetTrigger("jump");

            //JUMPING STATE
            if (coll.onGround)
            {
                currentState = PlayerState.JUMPING;
            }

            //WALL_JUMP STATE
            if (coll.onWall && !coll.onGround)
            {
                currentState = PlayerState.WALL_JUMP;
            }
        }
        //======================================================================================================

        //DASHING =====================================================================================================
        if (Input.GetButtonDown("Fire1") && !hasDashed)
        {
            currentState = PlayerState.DASHING;
        }
        //======================================================================================================

        //GROUNDED ====================================================================================================
        if (coll.onGround && !groundTouch)
        {
            // GroundTouch() resets the dash, as you can only dash once per jump
            currentState = PlayerState.GROUNDED;
            groundTouch  = true;
        }
        if (!coll.onGround && groundTouch)
        {
            groundTouch = false;
        }
        //======================================================================================================

        // Return if on a wall
        if (wallGrab || wallSlide || !canMove)
        {
            return;
        }

        // Otherwise use the horizontal input to flip the sprite
        if (xInput > 0)
        {
            side = 1;
            anim.Flip(side);
        }
        if (xInput < 0)
        {
            side = -1;
            anim.Flip(side);
        }
    }
Exemplo n.º 30
0
    // Update is called once per frame
    void Update()
    {
        if (player.health.CheckDead())
        {
            float   x    = Input.GetAxis("Horizontal");
            float   y    = Input.GetAxis("Vertical");
            float   xRaw = Input.GetAxisRaw("Horizontal");
            float   yRaw = Input.GetAxisRaw("Vertical");
            Vector2 dir  = new Vector2(x, y);

            if (dir.Equals(Vector2.zero))
            {
                player.currentState = PlayerState.Idle;
            }

            Walk(dir);
            anim.SetHorizontalMovement(Mathf.Abs(x), y, rb.velocity.x, rb.velocity.y);

            if (xRaw != 0 && coll.onGround)
            {
                gameMaster.GetSoundManager().PlayRun();
            }
            // else if (x == 0 && coll.onGround)
            // gameMaster.GetSoundManager().Stop();

            if (coll.onWall && Input.GetButton("Slide") && canMove)
            {
                player.currentState = PlayerState.WallGrab;

                if (side != coll.wallSide)
                {
                    anim.Flip(side);
                }

                wallGrab  = true;
                wallSlide = false;
            }

            if (Input.GetButtonUp("Slide") || !coll.onWall || !canMove)
            {
                player.currentState = PlayerState.Jump;

                wallGrab  = false;
                wallSlide = false;
            }

            if (Input.GetButtonDown("Slide") && coll.onGround && player.currentState != PlayerState.Slide)
            {
                player.currentState = PlayerState.Slide;
                slide = true;
            }

            if (Input.GetButtonUp("Slide"))
            {
                slide = false;
            }

            if (coll.onGround && !isDashing)
            {
                wallJumped = false;
                GetComponent <BetterJumping>().enabled = true;
            }

            if (wallGrab && !isDashing)
            {
                rb.gravityScale = 0;
                if (x > .2f || x < -.2f)
                {
                    rb.velocity = new Vector2(rb.velocity.x, 0);
                }

                rb.velocity = new Vector2(rb.velocity.x, 0);
            }
            else
            {
                rb.gravityScale = 3;
            }

            if (coll.onWall && !coll.onGround)
            {
                if (x != 0 && !wallGrab)
                {
                    player.currentState = PlayerState.WallSlide;

                    wallSlide = true;
                    WallSlide();
                }
            }

            if (!coll.onWall || coll.onGround)
            {
                wallSlide = false;
            }

            if (Input.GetButtonDown("Jump"))
            {
                player.currentState = PlayerState.Jump;
                gameMaster.GetSoundManager().PlayJump();

                if (coll.onGround)
                {
                    anim.SetTrigger(JumpAnimatorMapping);
                    Jump(Vector2.up, false);
                }
                else if (coll.onWall)
                {
                    anim.SetTrigger(WallJumpAnimatorMapping);
                    WallJump();
                }
            }

            if (Input.GetButtonDown("Dash") && !hasDashed)
            {
                player.currentState = PlayerState.Dash;

                if (xRaw != 0 || yRaw != 0)
                {
                    Dash(xRaw, yRaw);
                }
            }

            if (coll.onGround && !groundTouch)
            {
                gameMaster.GetSoundManager().PlayFall();
                GroundTouch();
                groundTouch = true;
            }

            if (!coll.onGround && groundTouch)
            {
                groundTouch = false;
            }

            WallParticle(y);

            if (wallGrab || wallSlide || !canMove)
            {
                return;
            }

            if (x > 0)
            {
                side = 1;
                anim.Flip(side);
            }

            if (x < 0)
            {
                side = -1;
                anim.Flip(side);
            }
        }
    }