void PortalCollision(int whichPortal)
    {
        var   portalScript = GetComponent <PortalScript>();
        float offset       = 1.5f;

        if (whichPortal == 1)
        {
            if (portalScript.Portal2.activeSelf)
            {
                _framesJumped = 0;
                Vector2 newPos = portalScript.PPos.p2;
                PortalScript.WallOrientation orientation = portalScript.PPos.p2Or;
                if (orientation == PortalScript.WallOrientation.Left)
                {
                    newPos.x += offset;
                }
                else if (orientation == PortalScript.WallOrientation.Right)
                {
                    newPos.x -= offset;
                }
                else if (orientation == PortalScript.WallOrientation.Ceiling)
                {
                    newPos.y -= offset;
                }
                else
                {
                    newPos.y += offset;
                }
                transform.position = newPos;
                playerVelocity     = NewVelocity(2);
            }
        }
        else
        {
            if (portalScript.Portal1.activeSelf)
            {
                _framesJumped = 0;
                Vector2 newPos = portalScript.PPos.p1;
                PortalScript.WallOrientation orientation = portalScript.PPos.p1Or;
                if (orientation == PortalScript.WallOrientation.Left)
                {
                    newPos.x += offset;
                }
                else if (orientation == PortalScript.WallOrientation.Right)
                {
                    newPos.x -= offset;
                }
                else if (orientation == PortalScript.WallOrientation.Ceiling)
                {
                    newPos.y -= offset;
                }
                else
                {
                    newPos.y += offset;
                }
                transform.position = newPos;
                playerVelocity     = NewVelocity(1);
            }
        }

        // this is important, not useless! It makes the raycasts activate again after portaling
        _jump = false;
        GetComponent <playerCollisions>().onGround = false;
    }
예제 #2
0
    private void CollidedWithPortal(Collision2D coll)
    {
        var   portalScript = player.GetComponent <PortalScript>();
        float offset       = 1.5f;
        bool  teleport     = false;

        PortalScript.WallOrientation orientation = PortalScript.WallOrientation.Left;
        Vector2 newPos   = Vector2.zero;
        Vector2 newVeloc = Vector2.zero;

        if (coll.gameObject == portalScript.Portal1)
        {
            if (portalScript.Portal2.activeSelf)
            {
                teleport    = true;
                newPos      = portalScript.PPos.p2;
                orientation = portalScript.PPos.p2Or;
            }
        }
        else
        {
            if (portalScript.Portal1.activeSelf)
            {
                teleport    = true;
                newPos      = portalScript.PPos.p1;
                orientation = portalScript.PPos.p1Or;
            }
        }

        if (teleport)
        {
            float speed = rb2d.velocity.magnitude;
            speed -= 1f;
            if (speed < 15f)
            {
                speed = 15f;
            }

            if (orientation == PortalScript.WallOrientation.Left)
            {
                newPos.x += offset;
                newVeloc  = new Vector2(speed, 0);
            }
            else if (orientation == PortalScript.WallOrientation.Right)
            {
                newPos.x -= offset;
                newVeloc  = new Vector2(-speed, 0);
            }
            else if (orientation == PortalScript.WallOrientation.Ceiling)
            {
                newPos.y -= offset;
                newVeloc  = new Vector2(0, -speed);
            }
            else
            {
                newPos.y += offset;
                newVeloc  = new Vector2(0, speed);
            }

            rb2d.velocity      = newVeloc;
            transform.position = newPos;
        }
    }