Portal.ExitPortal C# (CSharp) Method

ExitPortal() public method

public ExitPortal ( GameObject g ) : void
g GameObject
return void
    void ExitPortal(GameObject g)
    {
        Rigidbody2D rb;

        if (g.GetComponent<Rigidbody2D>() && !g.GetComponent<Rigidbody2D>().isKinematic) {
            rb = g.GetComponent<Rigidbody2D>();
        } else if (g.GetComponentInChildren<Rigidbody2D>() && !g.GetComponentInChildren<Rigidbody2D>().isKinematic) {
            rb = g.GetComponentInChildren<Rigidbody2D>();
        } else {
            return;
        }

        Vector2 pos = new Vector2(0, 0);
        if (gameObject == orangePortal) {
            pos = orangePortal.transform.position;
            g.transform.position = new Vector3(pos.x, pos.y, g.transform.position.z) + (Vector3) orangePortal.GetComponent<Portal>().normal;

            float magn = rb.velocity.magnitude;

            if (magn > maximumVelocity)
                magn = maximumVelocity;

            rb.velocity = magn * orangePortal.GetComponent<Portal>().normal;

        } else if (gameObject == bluePortal) {
            pos = bluePortal.transform.position;
            g.transform.position = new Vector3(pos.x, pos.y, g.transform.position.z) + (Vector3) bluePortal.GetComponent<Portal>().normal;

            float magn = rb.velocity.magnitude;

            if (magn > maximumVelocity)
                magn = maximumVelocity;

            rb.velocity = magn * bluePortal.GetComponent<Portal>().normal;
        }
    }