Esempio n. 1
0
    //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
    void FixedUpdate()
    {
        player1NearGround = false;
        player2NearGround = false;
        player2OnBottom   = false;
        onMoving          = false;
        onSpeed           = 0f;
        onMoving2         = false;
        onSpeed2          = 0f;
        moving            = false;

        if (inverted2 != otherScript.inverted2 && inverted2_2 == false)
        {
            invertOnCommand = true;
        }
        topOrBottom  = 0;
        topOrBottom2 = 0;
        if (changeGravity == true)
        {
            velocity  = 0;
            velocity2 = 0;
            if (changeGravityFreeze <= 4)
            {
                velocity            = velocity - (gravity);
                velocity2           = velocity2 - (gravity2);
                changeGravity       = false;
                changeGravityFreeze = 0;
            }
            changeGravityFreeze++;
        }
        //once at height of jump, hovers for 5 frames
        else if (velocity > 0 && velocity - gravity < 0 || floatTop == true)
        {
            velocity  = 0;
            velocity2 = 0;
            floatTop  = true;
            if (floatTimer > 8)
            {
                floatTop   = false;
                floatTimer = 0;
            }
            floatTimer++;
        }
        else if (topHold == true)
        {
            velocity  = 0;
            velocity2 = 0;
            if (topTimer > 5)
            {
                topHold  = false;
                topTimer = 0;
            }
            topTimer++;
        } //if in upward windtunnel sets velocity to .15f
        else if (inTunnel == true)
        {
            velocity  = 0.15f;
            velocity2 = 0.15f;
        }
        else if (grounded || grounded2)
        {
            velocity  = 0f;
            velocity2 = 0f;
        } //incorperates velocity to gravity
        else
        {
            velocity  = velocity - gravity;
            velocity2 = velocity2 - gravity2;
        }
        //sets a bunch of colliders based on where the player will be if you incorperate the velocity
        Collider2D[] topboi           = Physics2D.OverlapAreaAll(transform.position + new Vector3(-width / 2.1f, velocity + height / 2), transform.position + new Vector3(+width / 2.1f, velocity + height / 2 + .01f));
        Collider2D[] bottomboi        = Physics2D.OverlapAreaAll(transform.position + new Vector3(-width / 2.1f, velocity - height / 2), transform.position + new Vector3(+width / 2.1f, velocity - height / 2 - .01f));
        Collider2D[] bottomboi_Moving = Physics2D.OverlapAreaAll(transform.position + new Vector3(-width / 2.1f, velocity - height / 2), transform.position + new Vector3(+width / 2.1f, velocity - height / 2 - .01f));
        //invert gravity
        if (inverted)
        {
            bottomboi_Moving = Physics2D.OverlapAreaAll(transform.position + new Vector3(-width / 2.1f, velocity + height / 2), transform.position + new Vector3(+width / 2.1f, velocity + height / 2 + .01f));
        }
        //forgiving collider goes a bit further than regular colliders
        Collider2D[] bottom1_forgive = Physics2D.OverlapAreaAll(transform.position + new Vector3(-width / 2.1f, velocity - height / 2), transform.position + new Vector3(+width / 2.1f, velocity - height / 2 - .3f));
        //player2 colliders from VerticalOther script
        Collider2D[] topboi_2           = otherScript.GetTopBoi(velocity2);
        Collider2D[] bottomboi_2        = otherScript.GetBotBoi(velocity2);
        Collider2D[] bottomboi_2_Moving = otherScript.GetBotBoi_Moving(velocity2);
        Collider2D[] bottom1_2_Forgive  = otherScript.GetBot_Forgive(velocity2);

        //see if Player1 or Player2 collides with anything
        bool col  = false;
        bool col2 = false;

        bool colForgive = false;

        distToCol  = Mathf.Infinity;
        distToCol2 = Mathf.Infinity;

        //goes through collider topboi to see if Player 1 collided with anything on top of it
        foreach (var collide in topboi)
        {
            if (collide.gameObject.GetComponent <Collideable>() || collide.tag == "Ground")
            {
                col         = true;
                topOrBottom = 1;
                float newDist = GetComponent <BoxCollider2D>().Distance(collide).distance;
                if (newDist < distToCol)
                {
                    distToCol = newDist;
                }
            }
        }
        //see if player1 is colliding with something on it's bottom
        foreach (var collide in bottomboi)
        {
            if (collide.gameObject.GetComponent <Collideable>() || collide.tag == "Ground")
            {
                //Debug.Log("bottom player1");
                col         = true;
                topOrBottom = -1;
                if (collide.gameObject.GetComponent <MovingBox>())
                {
                    moving = true;
                    box    = collide.gameObject;
                }

                float newDist = GetComponent <BoxCollider2D>().Distance(collide).distance;
                if (newDist < distToCol)
                {
                    distToCol = newDist;
                }
            }
        }

        foreach (var collide in bottomboi_Moving)
        {
            if (collide.gameObject.tag == "MoveBox")
            {
                onMoving = true;
                onSpeed  = collide.gameObject.GetComponent <HorizontalBox>().speed;
            }
        }

        //same collider checks with player2
        foreach (var collide in topboi_2)
        {
            if (collide.gameObject.GetComponent <Collideable>() || collide.tag == "Ground")
            {
                float newDist = otherPlayer.GetComponent <BoxCollider2D>().Distance(collide).distance;
                if (newDist < distToCol2)
                {
                    distToCol2 = newDist;
                }
                col2         = true;
                topOrBottom2 = 1;
            }
        }

        foreach (var collide in bottomboi_2)
        {
            if (collide.gameObject.GetComponent <Collideable>() || collide.tag == "Ground")
            {
                //Debug.Log("bottom player2");
                col2         = true;
                topOrBottom2 = -1;
                if (collide.gameObject.GetComponent <MovingBox>())
                {
                    moving2 = true;
                    box     = collide.gameObject;
                }
                float newDist = otherPlayer.GetComponent <BoxCollider2D>().Distance(collide).distance;
                if (newDist < distToCol2)
                {
                    distToCol2 = newDist;
                }
            }
            if (collide.tag == "Ground")
            {
                player2OnBottom = true;
            }
        }

        foreach (var collide in bottomboi_2_Moving)
        {
            if (collide.gameObject.tag == "MoveBox")
            {
                Debug.Log("onMovingBox");
                onMoving2 = true;
                onSpeed2  = collide.gameObject.GetComponent <HorizontalBox>().speed;
            }
        }


        //same collider but with the forgiving
        //only needs to check bottom as this forgiving collision only affects the jumping
        foreach (var collide in bottom1_forgive)
        {
            if (collide.gameObject.GetComponent <Collideable>() || collide.tag == "Ground")
            {
                colForgive        = true;
                player1NearGround = true;
            }
        }
        foreach (var collide in bottom1_2_Forgive)
        {
            if (collide.gameObject.GetComponent <Collideable>() || collide.tag == "Ground")
            {
                colForgive        = true;
                player2NearGround = true;
            }
        }
        //if colForgive is found sets forgiving Ground to true allowing player to jump
        if (colForgive == true)
        {
            forgiveGround  = true;
            forgiveGround2 = true;
        }
        else
        {
            forgiveGround  = false;
            forgiveGround2 = false;
        }
        //this part of the script helps the players become have the same verticality
        //if player1 collides than player2 also collides as well
        if (col == true)
        {
            if (col2 == false && player2NearGround == false)
            {
                // sets player2 hiddenGround flag for hidden ground animation to play
                hiddenGroundFlag2 = true;
            }


            //col2 = true;
            topOrBottom2 = topOrBottom;
        }
        //if player2 collides than player1 also collides
        else if (col2 == true)
        {
            if (col == false && player1NearGround == false)
            {
                // sets player1 hiddenGround flag for hidden ground animation to play
                if (player2OnBottom == false)
                {
                    hiddenGroundFlag1 = true;
                }
            }

            //col = true;

            topOrBottom = topOrBottom2;
        }


        //if player isn't colliding with anything it is no longer grounded
        if (col == false)
        {
            if (grounded == true)
            {
                coyoteGround = true;
            }
            grounded = false;
            //hiddenGroundFlag1 = false;
        }
        if (col2 == false)
        {
            if (grounded2 == true)
            {
                coyoteGround2 = true;
            }
            grounded2 = false;
            //hiddenGroundFlag2 = false;
        }
        if (!grounded && !grounded2)
        {
            hiddenGroundFlag1 = false;
            hiddenGroundFlag2 = false;
        }
        //inverting everything also inverts whether you are hitting the ground or ceiling
        if (inverted == true)
        {
            topOrBottom = topOrBottom * -1;
        }
        if (inverted2 == true)
        {
            topOrBottom2 = topOrBottom2 * -1;
        }

        if (!grounded && (col || col2))
        {
            FlushCollision(col, col2, topOrBottom, topOrBottom2);
        }

        //if hit a ceiling or bottom of box
        if (col == true && topOrBottom == 1)
        {
            velocity = 0;
            grounded = false;
        }
        //if hit a ground then sets player to grounded and velocity to 0
        else if (col == true && topOrBottom == -1)
        {
            velocity = 0;
            grounded = true;
        }
        //same for player2
        if (col2 == true && topOrBottom2 == 1)
        {
            velocity2 = 0;
            grounded  = false;
        }
        else if (col2 == true && topOrBottom2 == -1)
        {
            velocity2 = 0;
            grounded2 = true;
        }

        //moves player1
        if (col == false && col2 == false)
        {
            if (Mathf.Abs(velocity) > .18f && inverted == true)
            {
                if (velocity > 0)
                {
                    velocity  = .18f;
                    velocity2 = .18f;
                }
            }
            transform.position             = transform.position + new Vector3(0, velocity);
            otherPlayer.transform.position = otherPlayer.transform.position + new Vector3(0, velocity2);
        }

        //used to move player up next to collideable object
        if ((col == true && topOrBottom == 1) || (col2 == true && topOrBottom2 == 1))
        {
            topHold = true;

            //FlushCollision(col, col2);
        }

        if (topOrBottom == -1)
        {
            if (moving)
            {
                if (!isSet)
                {
                    isSet = true;
                    ghost.transform.position = otherPlayer.transform.position;
                    ghost.transform.SetParent(box.transform);
                }
                transform.SetParent(box.transform);
                otherPlayer.transform.position = new Vector2(otherPlayer.transform.position.x, ghost.transform.position.y);
            }
        }
        else
        {
            isSet  = false;
            moving = false;
            //transform.SetParent(char_base.transform);
        }

        if (topOrBottom2 == -1)
        {
            if (moving2)
            {
                //t = box.GetComponent<MovingBox>().getT();
                if (!isSet2)
                {
                    isSet2 = true;
                    ghost.transform.position = transform.position;
                    ghost.transform.SetParent(box.transform);
                    //    start = new Vector2(transform.position.x - (box.GetComponent<MovingBox>().xEnd * t), transform.position.y - (box.GetComponent<MovingBox>().yEnd * t));
                    //    end = new Vector2(transform.position.x + (box.GetComponent<MovingBox>().xEnd * t), transform.position.y + (box.GetComponent<MovingBox>().yEnd * t));
                }
                //transform.position = Vector2.Lerp(start, end, t);
                otherPlayer.transform.SetParent(box.transform);
                transform.position = new Vector2(transform.position.x, ghost.transform.position.y);
            }
        }
        else
        {
            isSet2  = false;
            moving2 = false;
            //otherPlayer.transform.SetParent(char_base.transform);
        }

        //if either player is on ground, then stops player from moving vertically.
        if (grounded || grounded2)
        {
            charAnim.SetTrigger("grounded");
            otherCharAnim.SetTrigger("grounded");
        }

        charAnim.SetFloat("verticalSpeed", velocity);
        otherCharAnim.SetFloat("verticalSpeed", velocity2);
    }