// Update is called once per frame
    void Update()
    {
        if (moveInput.x != 0)
        {
            lastDir = (int)Mathf.Sign(moveInput.x);
        }
        playerAnim.SetInteger("PlayerState", (int)currentState);
        playerAnim.SetBool("wasJustIdle", wasJustIdle);
        playerAnim.SetFloat("moveInput", Mathf.Abs(moveInput.x));
        playerAnim.SetInteger("DashDir", dashDir);

        //checks if you're grounded
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);

        touchingRightWall = Physics2D.OverlapCircle(rightWallCheck.position, checkRadius, whatIsWall);

        touchingLeftWall = Physics2D.OverlapCircle(leftWallCheck.position, checkRadius, whatIsWall);


        //canPossess acts as a dash charge, once you land it refills.
        if (isGrounded)
        {
            canPossess = true;
        }

        //gets horizontal and vertical input so you can move and dash in all 8 directions
        moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

        switch (currentState)
        {
        case PlayerStates.Idle:
            wasJustIdle = true;
            if (moveInput.x > 0 || moveInput.x < 0)
            {
                currentState = PlayerStates.Moving;
            }
            if (Input.GetButton("Jump"))
            {
                currentState = PlayerStates.JumpingUp;
            }
            //if you can possess then dash in whatever direction you're pressing
            if (Input.GetButtonDown("Possess") && canPossess)
            {
                dashing      = true;
                canPossess   = false;
                currentState = PlayerStates.DashStartUp;
            }
            if (!isGrounded)
            {
                currentState = PlayerStates.Falling;
            }
            break;

        case PlayerStates.Moving:
            if (moveInput.x < 0.01f && moveInput.x > -0.01f)
            {
                currentState = PlayerStates.Idle;
            }
            if (Input.GetButtonDown("Jump"))
            {
                wasJustIdle  = false;
                currentState = PlayerStates.JumpingUp;
            }

            //if you can possess then dash in whatever direction you're pressing
            if (Input.GetButtonDown("Possess") && canPossess)
            {
                dashing      = true;
                canPossess   = false;
                wasJustIdle  = false;
                currentState = PlayerStates.DashStartUp;
            }
            wasJustIdle = false;
            break;

        case PlayerStates.JumpingUp:
            //if you jump it changes your y velocity to the maxJumpVelocity
            Movement.JumpPlayer(ref rb, isGrounded, maxJumpVelocity);

            //if you release jump while your y velocity is above your minJumpVelocity, your velocity gets set to your min jump velocity (variable jump height)
            if (Input.GetButtonUp("Jump"))
            {
                Movement.JumpPlayerRelease(ref rb, minJumpVelocity);
                currentState = PlayerStates.Falling;
            }

            //if you can possess then dash in whatever direction you're pressing
            if (Input.GetButtonDown("Possess") && canPossess)
            {
                dashing      = true;
                canPossess   = false;
                currentState = PlayerStates.DashStartUp;
            }

            if (rb.velocity.y <= 0)
            {
                currentState = PlayerStates.Falling;
            }

            break;

        case PlayerStates.Falling:
            //if you get sent out of a push core, this must be set to false so that the same push core won't push you just from touching it again
            isCancelledPressed = false;
            if (isGrounded == true)
            {
                if (Mathf.Abs(moveInput.x) > 0)
                {
                    currentState = PlayerStates.Moving;
                }
                currentState = PlayerStates.Idle;
            }
            //if you can possess then dash in whatever direction you're pressing
            if (Input.GetButtonDown("Possess") && canPossess)
            {
                dashing      = true;
                canPossess   = false;
                currentState = PlayerStates.DashStartUp;
            }

            if (touchingRightWall /*&& moveInput.x > 0*/ || touchingLeftWall /*&& moveInput.x < 0*/)
            {
                currentState = PlayerStates.WallSliding;
            }
            break;

        case PlayerStates.WallSliding:
            //so the player can dash out of the wallsliding state
            if (Input.GetButtonDown("Possess") && canPossess)
            {
                dashing      = true;
                canPossess   = false;
                currentState = PlayerStates.DashStartUp;
            }
            if (isGrounded)
            {
                rb.drag      = 0;
                currentState = PlayerStates.Idle;
            }
            else if (touchingRightWall && moveInput.x > 0)
            {
                rb.drag = wallSlideDrag;
                if (Input.GetButtonDown("Jump"))
                {
                    rb.drag     = 0;
                    lastWallDir = 1;
                    Movement.WallJumpPlayer(ref rb, new Vector2((-lastWallDir) * wallJumpOffVelocity.x, wallJumpOffVelocity.y));
                    currentState = PlayerStates.JumpingOffWall;
                }
            }
            else if (touchingLeftWall && moveInput.x < 0)
            {
                rb.drag = wallSlideDrag;
                if (Input.GetButtonDown("Jump"))
                {
                    rb.drag     = 0;
                    lastWallDir = -1;
                    Movement.WallJumpPlayer(ref rb, new Vector2((-lastWallDir) * wallJumpOffVelocity.x, wallJumpOffVelocity.y));
                    currentState = PlayerStates.JumpingOffWall;
                }
            }

            else
            {
                rb.drag      = 0;
                currentState = PlayerStates.Falling;
            }


            break;

        case PlayerStates.JumpingOffWall:

            if (rb.velocity.y <= 0)
            {
                //rb.drag = 0;
                currentState = PlayerStates.Falling;
            }
            break;

        case PlayerStates.DashStartUp:
            if ((moveInput.x < 0.1f && moveInput.x > -0.1) && (moveInput.y < 0.1f && moveInput.y > -0.1f))
            {
                if (lastDir == 1)
                {
                    //right
                    dashDir = 1;
                    StartCoroutine(DashRight(rb));
                }
                if (lastDir == -1)
                {
                    //left
                    dashDir = 5;
                    StartCoroutine(DashLeft(rb));
                }
            }
            if (moveInput.x > 0.1f && (moveInput.y < 0.1f && moveInput.y > -0.1f))
            {
                //right
                dashDir = 1;
                StartCoroutine(DashRight(rb));
            }
            if (moveInput.x > 0.1f && moveInput.y < -0.1f)
            {
                //down right
                dashDir = 2;
                StartCoroutine(DashDownRight(rb));
            }
            if ((moveInput.x < 0.1f && moveInput.x > -0.1) && moveInput.y < -0.1f)
            {
                //down
                dashDir = 3;
                StartCoroutine(DashDown(rb));
            }
            if (moveInput.x < -0.1f && moveInput.y < -0.1f)
            {
                //down left
                dashDir = 4;
                StartCoroutine(DashDownLeft(rb));
            }
            if (moveInput.x < -0.1f && (moveInput.y < 0.1f && moveInput.y > -0.1f))
            {
                //left
                dashDir = 5;
                StartCoroutine(DashLeft(rb));
            }
            if (moveInput.x < -0.1f && moveInput.y > 0.1f)
            {
                //up left
                dashDir = 6;
                StartCoroutine(DashUpLeft(rb, facingRight));
            }
            if ((moveInput.x < 0.1f && moveInput.x > -0.1) && moveInput.y > 0.1f)
            {
                //up
                dashDir = 7;
                StartCoroutine(DashUp(rb));
            }
            if (moveInput.x > 0.1f && moveInput.y > 0.1f)
            {
                //up right
                dashDir = 8;
                StartCoroutine(DashUpRight(rb));
            }
            break;

        case PlayerStates.Dashing:
            //if you're currently possessing something and you press the possess button, you pop out. (doesn't work currently, jumping out will transfer velocity, possessing out will make you dash out)
            if (!dashing)
            {
                currentState = PlayerStates.Falling;
            }
            break;

        case PlayerStates.PossessingCollide:
            spriteRenderer.color = Color.clear;
            //if you're currently possessing something and you press the possess button, you pop out. (doesn't work currently, jumping out will transfer velocity, possessing out will make you dash out)
            if (possessing && Input.GetButtonDown("Jump") || possessionTimer <= 0)
            {
                possessionTimer = possessionTimerOriginal;
                RevertParent();
                if (coreRB.velocity == new Vector2(0, 0))
                {
                    canPossess = true;
                    isGrounded = true;
                    Movement.JumpPlayer(ref rb, isGrounded, maxJumpVelocity);
                    spriteRenderer.color = Color.white;
                    currentState         = PlayerStates.JumpingUp;
                }
                else
                {
                    rb.velocity = TransferVelocity(coreRB, rb);
                    if (rb.velocity.y >= 0)
                    {
                        canPossess           = true;
                        spriteRenderer.color = Color.white;
                        currentState         = PlayerStates.JumpingUp;
                    }
                    else if (rb.velocity.y == 0)
                    {
                        canPossess           = true;
                        rb.velocity          = new Vector2(rb.velocity.x, maxJumpVelocity);
                        spriteRenderer.color = Color.white;
                        currentState         = PlayerStates.Boosting;
                    }
                    else
                    {
                        canPossess           = true; // so that you can dash again after unPossessing an object
                        spriteRenderer.color = Color.white;
                        currentState         = PlayerStates.Falling;
                    }
                }
            }

            possessionTimer -= Time.deltaTime;
            break;

        case PlayerStates.PossessingNonCollide:
            spriteRenderer.color = Color.clear;
            //if you're not possessing a push core, then we'll run through the normal moving core code
            if (!isPushCore)
            {
                //if you're currently possessing something and you press the possess button, you pop out. (doesn't work currently, jumping out will transfer velocity, possessing out will make you dash out)
                if (possessing && Input.GetButtonDown("Jump") || possessionTimer <= 0)
                {
                    possessionTimer = possessionTimerOriginal;
                    RevertParent();
                    if (MovingCoreController.currentXVelocity == 0 && MovingCoreController.currentYVelocity == 0)
                    {
                        canPossess = true;
                        isGrounded = true;
                        Movement.JumpPlayer(ref rb, isGrounded, maxJumpVelocity);
                        spriteRenderer.color = Color.white;
                        currentState         = PlayerStates.JumpingUp;
                    }
                    else
                    {
                        rb.velocity = TransferVelocity(coreRB, rb);
                        if (rb.velocity.y > 0)
                        {
                            canPossess           = true;
                            spriteRenderer.color = Color.white;
                            currentState         = PlayerStates.JumpingUp;
                        }
                        else if (rb.velocity.y == 0)
                        {
                            canPossess           = true;
                            rb.velocity          = new Vector2(rb.velocity.x, maxJumpVelocity);
                            spriteRenderer.color = Color.white;
                            currentState         = PlayerStates.Boosting;
                        }
                        else
                        {
                            canPossess           = true; // so that you can dash again after unPossessing an object
                            spriteRenderer.color = Color.white;
                            currentState         = PlayerStates.Falling;
                        }
                    }
                }

                //Changing speed for moving cores
                if (possessing && Input.GetButton("FastButton"))
                {
                    MovingCoreController.currentState = MovingCore_Controller.CoreStates.SpedUp;
                }
                else if (possessing && Input.GetButton("SlowButton"))
                {
                    MovingCoreController.currentState = MovingCore_Controller.CoreStates.SlowedDown;
                }
                else
                {
                    MovingCoreController.currentState = MovingCore_Controller.CoreStates.Default;
                }
            }

            //if you are possessing a push core, then
            else
            {
                pushInput          = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
                isCancelledPressed = Input.GetButtonDown(AllText.someText /*"Jump"*/);
                if (PushCoreController.pushTimer <= 0 || isCancelledPressed)
                {
                    possessionTimer = possessionTimerOriginal;
                    RevertParent();
                    //rb.velocity = TransferVelocity(coreRB, rb);
                    canPossess           = true; // so that you can dash again after unPossessing an object
                    spriteRenderer.color = Color.white;
                    currentState         = PlayerStates.Falling;
                }
            }

            possessionTimer -= Time.deltaTime;
            break;

        case PlayerStates.Boosting:

            if (moveAfterLaunchTimer <= 0)
            {
                moveAfterLaunchTimer = moveAfterLaunchTime;
                currentState         = PlayerStates.Falling;
            }

            moveAfterLaunchTimer -= Time.deltaTime;



            //if (rb.velocity.y <= 0)
            //{
            //    currentState = PlayerStates.Falling;
            //}
            //if (isGrounded == true) {
            //    currentState = PlayerStates.Idle;
            //}
            //if (touchingRightWall /*&& moveInput.x > 0*/  || touchingLeftWall /*&& moveInput.x < 0*/)
            //{
            //    currentState = PlayerStates.WallSliding;
            //}
            //we can possibly make the player be able to dash out of the boosting state
            //if (Input.GetButtonDown("Possess") && canPossess)
            //{
            //    dashing = true;
            //    canPossess = false;
            //    currentState = PlayerStates.DashStartUp;
            //}
            break;
        }
    }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        /*
         * if (moveInput.x == 0)
         * {
         *  animator.Play("Idle");
         * }
         */
        if (moveInput.x != 0)
        {
            lastDir = (int)Mathf.Sign(moveInput.x);
            if (moveInput.x < 0)
            {
                spriteRenderer.flipX = true;
            }
            else if (moveInput.x > 0)
            {
                spriteRenderer.flipX = false;
            }
        }

        maxJumpVelocity = statManager.maxJump;
        minJumpVelocity = statManager.minJump;

        //checks if you're grounded
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);

        //canPossess acts as a dash charge, once you land it refills.
        if (isGrounded)
        {
            canPossess = true;
        }

        //gets horizontal and vertical input so you can move and dash in all 8 directions
        moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

        switch (currentState)
        {
        case PlayerStates.Idle:
            henryAnim.SetTrigger("back");
            wasJustIdle = true;
            if (moveInput.x > 0 || moveInput.x < 0)
            {
                currentState = PlayerStates.Moving;
            }
            if (Input.GetButton("Jump"))
            {
                currentState = PlayerStates.JumpingUp;
            }
            if (Input.GetButtonDown("Enter") && canPossess)
            {
                entering = true;
            }
            if (!isGrounded)
            {
                currentState = PlayerStates.Falling;
            }
            break;

        case PlayerStates.Moving:
            if (moveInput.x < 0.01f && moveInput.x > -0.01f)
            {
                henryAnim.SetTrigger("start");
                currentState = PlayerStates.Idle;
            }
            if (Input.GetButtonDown("Jump"))
            {
                wasJustIdle  = false;
                currentState = PlayerStates.JumpingUp;
            }
            wasJustIdle = false;
            break;

        case PlayerStates.JumpingUp:
            //if you jump it changes your y velocity to the maxJumpVelocity
            Movement.JumpPlayer(ref rb, isGrounded, maxJumpVelocity);
            //if you release jump while your y velocity is above your minJumpVelocity, your velocity gets set to your min jump velocity (variable jump height)

            if (Input.GetButtonUp("Jump"))
            {
                Movement.JumpPlayerRelease(ref rb, minJumpVelocity);
                currentState = PlayerStates.Falling;
            }
            if (rb.velocity.y <= 0)
            {
                currentState = PlayerStates.Falling;
            }

            break;

        case PlayerStates.Falling:
            isCancelledPressed = false;
            if (isGrounded == true)
            {
                if (Mathf.Abs(moveInput.x) > 0)
                {
                    currentState = PlayerStates.Moving;
                }
                currentState = PlayerStates.Idle;
            }
            if (Input.GetButtonDown("Jump"))
            {
                wasJustIdle  = false;
                currentState = PlayerStates.JumpingUp;
            }

            break;
        }
    }
Example #3
0
    // Update is called once per frame
    void Update()
    {
        if (moveInput.x != 0)
        {
            lastDir = (int)Mathf.Sign(moveInput.x);
        }

        //checks if you're grounded
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);

        //canPossess acts as a dash charge, once you land it refills.
        if (isGrounded)
        {
            canPossess = true;
        }

        //gets horizontal and vertical input so you can move and dash in all 8 directions
        moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

        switch (currentState)
        {
        case PlayerStates.Idle:
            wasJustIdle = true;
            if (moveInput.x > 0 || moveInput.x < 0)
            {
                currentState = PlayerStates.Moving;
            }
            if (Input.GetButton("Jump"))
            {
                currentState = PlayerStates.JumpingUp;
            }
            if (Input.GetButtonDown("Enter") && canPossess)
            {
                entering   = true;
                canPossess = false;
            }
            if (!isGrounded)
            {
                currentState = PlayerStates.Falling;
            }
            break;

        case PlayerStates.Moving:
            if (moveInput.x < 0.01f && moveInput.x > -0.01f)
            {
                currentState = PlayerStates.Idle;
            }
            if (Input.GetButtonDown("Jump"))
            {
                wasJustIdle  = false;
                currentState = PlayerStates.JumpingUp;
            }

            if (Input.GetButtonDown("Enter") && canPossess)
            {
                entering    = true;
                canPossess  = false;
                wasJustIdle = false;
            }
            wasJustIdle = false;
            break;

        case PlayerStates.JumpingUp:
            //if you jump it changes your y velocity to the maxJumpVelocity
            Movement.JumpPlayer(ref rb, isGrounded, maxJumpVelocity);
            jumpCount--;
            //if you release jump while your y velocity is above your minJumpVelocity, your velocity gets set to your min jump velocity (variable jump height)
            if (Input.GetButtonUp("Jump"))
            {
                Movement.JumpPlayerRelease(ref rb, minJumpVelocity);
                currentState = PlayerStates.Falling;
            }

            if (Input.GetButtonDown("Enter") && canPossess)
            {
                entering   = true;
                canPossess = false;
            }

            if (rb.velocity.y <= 0)
            {
                currentState = PlayerStates.Falling;
            }

            break;

        case PlayerStates.Falling:
            isCancelledPressed = false;
            if (isGrounded == true)
            {
                if (Mathf.Abs(moveInput.x) > 0)
                {
                    currentState = PlayerStates.Moving;
                }
                currentState = PlayerStates.Idle;
            }
            if (Input.GetButtonDown("Jump") && jumpCount > 0)
            {
                wasJustIdle  = false;
                currentState = PlayerStates.JumpingUp;
            }
            if (Input.GetButtonDown("Enter") && canPossess)
            {
                entering   = true;
                canPossess = false;
            }

            break;

        case PlayerStates.PossessingCollide:
            spriteRenderer.color = Color.clear;
            if (possessing && Input.GetButtonDown("Jump") || possessionTimer <= 0)
            {
                jumpCount       = curJumpCount;
                possessionTimer = possessionTimerOriginal;
                RevertParent();
                if (coreRB.velocity == new Vector2(0, 0))
                {
                    canPossess = true;
                    isGrounded = true;
                    Movement.JumpPlayer(ref rb, isGrounded, maxJumpVelocity);
                    spriteRenderer.color = Color.white;
                    currentState         = PlayerStates.JumpingUp;
                }
            }

            possessionTimer -= Time.deltaTime;
            break;

        case PlayerStates.PossessingNonCollide:
            spriteRenderer.color = Color.clear;
            if (possessing && Input.GetButtonDown("Jump") || possessionTimer <= 0)
            {
                pushInput          = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
                isCancelledPressed = Input.GetButtonDown("Jump");
                if (BoostPaintingController.pushTimer <= 0 || isCancelledPressed)
                {
                    possessionTimer = possessionTimerOriginal;
                    RevertParent();
                    canPossess           = true;
                    spriteRenderer.color = Color.white;
                    currentState         = PlayerStates.Falling;
                }
            }


            possessionTimer -= Time.deltaTime;
            break;
        }
    }