Exemple #1
0
    public void start()
    {
        meshFilter1      = top.GetComponent <MeshFilter>();
        meshFilter2      = bottom.GetComponent <MeshFilter>();
        meshFilter1.mesh = new Mesh();
        meshFilter2.mesh = new Mesh();

        topClippableObject    = top.GetComponent <ClippableObject>();
        bottomClippableObject = bottom.GetComponent <ClippableObject>();

        //context = new ExpressionContext();
        //context.Imports.AddType(typeof(System.Math));
        compile();

        Grid();
        Grid1();
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        foreach (GameObject obj in GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[])
        {
            if (obj.layer != layer && obj.layer != otherLayer &&
                obj.layer != 14 &&
                obj.layer != 22 &&
                obj.layer != 23 &&
                obj.layer != 8 &&
                obj.layer != 9)                 //Portal clone & Blockers
            {
                if (!objects.Contains(obj))
                {
                    if (obj.GetComponent <MeshRenderer>() != null)
                    {
                        objects.Add(obj);
                        GameObject clone = new GameObject();
                        clone.name  = obj.name + " Clip Clone (" + gameObject.name + ")";
                        clone.layer = layer;
                        clones.Add(clone);
                        Material oldMaterial = obj.GetComponent <MeshRenderer>().material;
                        clone.transform.parent                 = obj.transform;
                        clone.transform.localPosition          = Vector3.zero;
                        clone.transform.rotation               = obj.transform.rotation;
                        clone.transform.localScale             = Vector3.one;
                        clone.AddComponent <MeshFilter>().mesh = obj.GetComponent <MeshFilter>().mesh;
                        MeshRenderer MR = clone.AddComponent <MeshRenderer>();
                        if (obj.GetComponent <Renderer>().shadowCastingMode != UnityEngine.Rendering.ShadowCastingMode.Off)
                        {
                            clone.GetComponent <Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.TwoSided;
                        }
                        else
                        {
                            clone.GetComponent <Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
                        }
                        MR.material = clipPlaneMaterial;
                        ClippableObject CO = clone.AddComponent <ClippableObject>();
                        CO.clipPlanes     = 1;
                        CO.plane1Position = otherPortal.transform.position;
                        CO.plane1Rotation = otherPortal.transform.eulerAngles;
                        MR.material.CopyPropertiesFromMaterial(oldMaterial);
                    }
                }
            }
            else if (obj.layer == 14)
            {
                if (!portalObjects.Contains(obj))
                {
                    if (obj.GetComponent <MeshRenderer>() != null)
                    {
                        portalObjects.Add(obj);
                        GameObject clone = new GameObject();
                        clone.name  = obj.name + " Clip Clone";
                        clone.layer = layer;
                        portalClones.Add(clone);
                        Material oldMaterial = obj.GetComponent <MeshRenderer>().material;
                        clone.transform.parent                 = obj.transform;
                        clone.transform.localPosition          = Vector3.zero;
                        clone.transform.rotation               = obj.transform.rotation;
                        clone.transform.localScale             = Vector3.one;
                        clone.AddComponent <MeshFilter>().mesh = obj.GetComponent <MeshFilter>().mesh;
                        MeshRenderer MR = clone.AddComponent <MeshRenderer>();
                        clone.GetComponent <Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.TwoSided;
                        MR.material = clipPlaneMaterial;
                        ClippableObject CO  = clone.AddComponent <ClippableObject>();
                        ClippableObject CO2 = obj.GetComponent <ClippableObject>();
                        CO.clipPlanes     = 2;
                        CO.plane1Position = CO2.plane1Position;
                        CO.plane1Rotation = CO2.plane1Rotation;
                        CO.plane2Position = otherPortal.transform.position;
                        CO.plane2Rotation = otherPortal.transform.eulerAngles;
                        MR.material.CopyPropertiesFromMaterial(oldMaterial);
                    }
                }
            }
        }


        for (int i = 0; i < objects.Count; ++i)
        {
            GameObject obj = objects[i];
            if (obj == null || obj.layer == 14)
            {
                //Debug.Log(gameObject.name + ": Old count: " + objects.Count);
                //Debug.Log(gameObject.name + ": Destroying at " + i);
                Destroy(clones[i]);
                clones.RemoveAt(i);
                objects.RemoveAt(i);
                i = 0;
                //Debug.Log(gameObject.name + ": New count: " + objects.Count);
            }
        }
        for (int i = 0; i < portalObjects.Count; ++i)
        {
            GameObject obj = portalObjects[i];
            if (obj == null || obj.layer != 14)
            {
                //Debug.Log(gameObject.name + ": Old count: " + portalObjects.Count);
                //Debug.Log(gameObject.name + ": Destroying at " + i);
                Destroy(portalClones[i]);
                portalClones.RemoveAt(i);
                portalObjects.RemoveAt(i);
                i = 0;
                //Debug.Log(gameObject.name + ": New count: " + portalObjects.Count);
            }
        }
        for (int i = 0; i < portalClones.Count; ++i)
        {
            GameObject clone = portalClones[i];
            if (clone == null)
            {
                //Debug.Log(gameObject.name + ": Old count: " + portalObjects.Count);
                //Debug.Log(gameObject.name + ": Destroying at " + i);
                portalClones.RemoveAt(i);
                portalObjects.RemoveAt(i);
                i = 0;
                //Debug.Log(gameObject.name + ": New count: " + portalObjects.Count);
            }
        }
        foreach (GameObject clone in clones)
        {
            ClippableObject CO = clone.GetComponent <ClippableObject>();
            CO.clipPlanes     = 1;
            CO.plane1Position = otherPortal.transform.position;
            if (Vector3.Dot(portalCamera.transform.position - otherPortal.transform.position, otherPortal.transform.up) < 0)
            {
                CO.plane1Position -= otherPortal.transform.up * offset;
                CO.plane1Rotation  = otherPortal.transform.eulerAngles;
            }
            else
            {
                CO.plane1Position += otherPortal.transform.up * offset;
                CO.plane1Rotation  = (Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.rotation).eulerAngles;
            }
        }

        for (int i = 0; i < portalObjects.Count; ++i)
        {
            GameObject      obj   = portalObjects[i];
            GameObject      clone = portalClones[i];
            ClippableObject CO    = clone.GetComponent <ClippableObject>();
            ClippableObject CO2   = obj.GetComponent <ClippableObject>();
            CO.clipPlanes = 2;
            if (CO2 != null)
            {
                CO.plane1Position = CO2.plane1Position;
                CO.plane1Rotation = CO2.plane1Rotation;
                CO.plane2Position = otherPortal.transform.position;
                if (Vector3.Dot(portalCamera.transform.position - otherPortal.transform.position, otherPortal.transform.up) < 0)
                {
                    CO.plane2Position -= otherPortal.transform.up * offset;
                    CO.plane2Rotation  = otherPortal.transform.eulerAngles;
                }
                else
                {
                    CO.plane2Position += otherPortal.transform.up * offset;
                    CO.plane2Rotation  = (Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.rotation).eulerAngles;
                }
            }
        }
    }
Exemple #3
0
    void FixedUpdate()
    {
        Vector3 currVel       = GetComponent <Rigidbody>().velocity;
        Vector3 velDifference = currVel - prevVel;
        Vector3 gravAccel     = -Vector3.up * gravity * Time.fixedDeltaTime;

        localAccel = (velDifference - gravAccel) / Time.fixedDeltaTime;
        prevVel    = currVel;

        currVel          = otherPortal.GetComponent <Rigidbody>().velocity;
        velDifference    = currVel - otherPrevVel;
        gravAccel        = -Vector3.up * gravity * Time.fixedDeltaTime;
        otherPortalAccel = (velDifference - gravAccel) / Time.fixedDeltaTime;
        otherPrevVel     = currVel;

        for (int index = 0; index < objects.Count; ++index)
        {
            GameObject obj   = objects[index];
            GameObject clone = clones[index];

            Rigidbody RBobj   = obj.GetComponent <Rigidbody>();
            Rigidbody RBclone = clone.GetComponent <Rigidbody>();
            Rigidbody RBthis  = GetComponent <Rigidbody>();
            Rigidbody RBother = otherPortal.GetComponent <Rigidbody>();
            if (RBobj != null)
            {
                Vector3 vel_obj    = RBobj.velocity;
                Vector3 angVel_obj = RBobj.angularVelocity;

                vel_obj -= RBthis.velocity;
                //angVel_obj -= RBthis.angularVelocity;

                vel_obj /= transform.localScale.magnitude;
                //angVel_obj /= transform.localScale.magnitude;

                Vector3 vel_clone    = Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.TransformDirection(transform.InverseTransformDirection(vel_obj));
                Vector3 angVel_clone = Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.TransformDirection(transform.InverseTransformDirection(angVel_obj));

                vel_clone *= otherPortal.transform.localScale.magnitude;
                //angVel_clone *= otherPortal.transform.localScale.magnitude;

                vel_clone += RBother.velocity;
                //angVel_clone += RBother.angularVelocity;

                vel_clone    = Vector3.Lerp(RBclone.velocity, vel_clone, 0.5f);
                angVel_clone = Vector3.Lerp(RBclone.angularVelocity, angVel_clone, 0.5f);

                RBclone.velocity        = vel_clone;
                RBclone.angularVelocity = angVel_clone;

                vel_clone -= RBother.velocity;
                //angVel_clone -= RBother.angularVelocity;

                vel_clone /= otherPortal.transform.localScale.magnitude;
                //angVel_clone /= otherPortal.transform.localScale.magnitude;

                vel_obj    = Quaternion.AngleAxis(180, transform.forward) * transform.TransformDirection(otherPortal.transform.InverseTransformDirection(vel_clone));
                angVel_obj = Quaternion.AngleAxis(180, transform.forward) * transform.TransformDirection(otherPortal.transform.InverseTransformDirection(angVel_clone));

                vel_obj *= transform.localScale.magnitude;
                //angVel_obj *= transform.localScale.magnitude;

                vel_obj += RBthis.velocity;
                //angVel_obj += RBother.angularVelocity;

                RBobj.velocity        = vel_obj;
                RBobj.angularVelocity = angVel_obj;



                if (applyGravity)
                {
                    Vector3 normal = transform.up;
                    if (directions[index] == false)
                    {
                        normal = -transform.up;
                    }

                    float d = Vector3.Dot(normal, obj.transform.position - transform.position);
                    float x = Mathf.Clamp(d / obj.GetComponent <Collider>().bounds.extents.magnitude, 0f, 1f) * 0.5f + 0.5f;

                    Vector3 COM1 = obj.transform.InverseTransformDirection(normal) * (1f - x) / 2;
                    Vector3 COM2 = obj.transform.InverseTransformDirection(normal) * (x) / 2;

                    RBobj.AddForceAtPosition(
                        -localAccel * RBobj.mass * 2 * x,
                        obj.transform.TransformPoint(COM1));
                    RBobj.AddForceAtPosition(
                        Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.TransformDirection(transform.InverseTransformDirection(-otherPortalAccel)) * RBobj.mass * 2 * (1f - x),
                        obj.transform.TransformPoint(-COM2));
                }
            }

            Vector3 scale = obj.transform.localScale;
            scale.x *= otherPortal.transform.lossyScale.x / transform.lossyScale.x;
            scale.y *= otherPortal.transform.lossyScale.y / transform.lossyScale.y;
            scale.z *= otherPortal.transform.lossyScale.z / transform.lossyScale.z;
            clone.transform.parent = otherPortal.transform;
            Vector3    pos = clone.transform.localPosition;
            Quaternion rot = clone.transform.localRotation;
            clone.transform.parent        = transform;
            clone.transform.localPosition = pos;
            clone.transform.localRotation = rot;
            clone.transform.parent        = null;
            clone.transform.localScale    = scale;
            clone.transform.RotateAround(transform.position, transform.forward, 180);

            obj.transform.position   = Vector3.Lerp(clone.transform.position, obj.transform.position, 0.5f);;
            clone.transform.position = obj.transform.position;
            obj.transform.rotation   = Quaternion.Lerp(clone.transform.rotation, obj.transform.rotation, 0.5f);
            clone.transform.rotation = obj.transform.rotation;

            clone.transform.parent = transform;
            pos = clone.transform.localPosition;
            rot = clone.transform.localRotation;
            clone.transform.parent        = otherPortal.transform;
            clone.transform.localPosition = pos;
            clone.transform.localRotation = rot;
            clone.transform.parent        = null;
            clone.transform.localScale    = scale;
            clone.transform.RotateAround(otherPortal.transform.position, otherPortal.transform.forward, 180);

            ClippableObject COobj   = obj.GetComponent <ClippableObject>();
            ClippableObject COclone = clone.GetComponent <ClippableObject>();

            COobj.plane1Position   = transform.position;
            COclone.plane1Position = otherPortal.transform.position;

            if (directions[index] == true)
            {
                COobj.plane1Position   += transform.up * offset;
                COclone.plane1Position += otherPortal.transform.up * offset;
                COobj.plane1Rotation    = transform.eulerAngles;
                COclone.plane1Rotation  = otherPortal.transform.eulerAngles;
            }
            else
            {
                COobj.plane1Position   -= transform.up * offset;
                COclone.plane1Position -= otherPortal.transform.up * offset;
                COobj.plane1Rotation    = (Quaternion.AngleAxis(180, transform.forward) * transform.rotation).eulerAngles;
                COclone.plane1Rotation  = (Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.rotation).eulerAngles;
            }

            bool direction = true;
            if (Vector3.Dot(obj.transform.position - transform.position, transform.up) < 0)
            {
                direction = false;
            }

            if (!(direction == directions[index]) && obj.transform.parent == null)
            {
                Vector3    oldPos    = obj.transform.position;
                Quaternion oldRot    = obj.transform.rotation;
                Vector3    oldScale  = obj.transform.localScale;
                Vector3    oldVel    = RBobj.velocity;
                Vector3    oldAngVel = RBobj.angularVelocity;

                obj.transform.position   = clone.transform.position;
                obj.transform.rotation   = clone.transform.rotation;
                obj.transform.localScale = clone.transform.localScale;
                RBobj.velocity           = RBclone.velocity;
                RBobj.angularVelocity    = RBclone.angularVelocity;

                clone.transform.position   = oldPos;
                clone.transform.rotation   = oldRot;
                clone.transform.localScale = oldScale;
                RBclone.velocity           = oldVel;
                RBclone.angularVelocity    = oldAngVel;

                Vector3 oldClipPos = COobj.plane1Position;
                Vector3 oldClipRot = COobj.plane1Rotation;
                COobj.plane1Position   = COclone.plane1Position;
                COobj.plane1Rotation   = COclone.plane1Rotation;
                COclone.plane1Position = oldClipPos;
                COclone.plane1Rotation = oldClipRot;
            }
        }
    }
Exemple #4
0
    void OnTriggerEnter(Collider coll)
    {
        GameObject obj = coll.gameObject;

        if (obj.layer != 14 && obj.layer != 18 && obj.layer != 21)
        {
            Debug.Log("Adding " + obj.name);
            objects.Add(obj);

            GameObject clone = new GameObject();
            clone.name  = obj.name + " Clone (" + gameObject.name + " -> " + otherPortal.name + ")";
            clone.layer = 14;
            layers.Add(obj.layer);
            obj.layer = 14;
            clone.AddComponent <MeshFilter>().mesh = obj.GetComponent <MeshFilter>().mesh;
            MeshRenderer MRclone = clone.AddComponent <MeshRenderer>();
            clone.GetComponent <Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.TwoSided;
            MeshRenderer MRobj       = obj.GetComponent <MeshRenderer>();
            Material     oldMaterial = MRobj.material;
            materials.Add(oldMaterial);
            MRobj.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.TwoSided;
            clones.Add(clone);

            //Vector3 normal;
            ClippableObject COclone = clone.AddComponent <ClippableObject>();
            ClippableObject COobj   = obj.AddComponent <ClippableObject>();
            COobj.clipPlanes       = 1;
            COclone.clipPlanes     = 1;
            COobj.plane1Position   = transform.position;
            COclone.plane1Position = otherPortal.transform.position;
            if (Vector3.Dot(obj.transform.position - transform.position, transform.up) > 0)
            {
                directions.Add(true);
                //normal = transform.up;
                COobj.plane1Rotation   = transform.eulerAngles;
                COclone.plane1Rotation = otherPortal.transform.eulerAngles;
            }
            else
            {
                directions.Add(false);
                //normal = -transform.up;
                COobj.plane1Rotation   = (Quaternion.AngleAxis(180, transform.forward) * transform.rotation).eulerAngles;
                COclone.plane1Rotation = (Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.rotation).eulerAngles;
            }

            clone.transform.position = obj.transform.position;
            clone.transform.rotation = obj.transform.rotation;

            Vector3 scale = obj.transform.localScale;
            scale.x *= otherPortal.transform.lossyScale.x / transform.lossyScale.x;
            scale.y *= otherPortal.transform.lossyScale.y / transform.lossyScale.y;
            scale.z *= otherPortal.transform.lossyScale.z / transform.lossyScale.z;
            clone.transform.parent = transform;
            Vector3    pos = clone.transform.localPosition;
            Quaternion rot = clone.transform.localRotation;
            clone.transform.parent        = otherPortal.transform;
            clone.transform.localPosition = pos;
            clone.transform.localRotation = rot;
            clone.transform.parent        = null;
            clone.transform.localScale    = scale;
            clone.transform.RotateAround(otherPortal.transform.position, otherPortal.transform.forward, 180);

            MRclone.material = clipPlaneMaterial;
            MRclone.material.CopyPropertiesFromMaterial(oldMaterial);
            MRobj.material.CopyPropertiesFromMaterial(oldMaterial);


            Rigidbody RBobj = obj.GetComponent <Rigidbody>();
            if (RBobj != null)
            {
                Rigidbody RBclone = clone.AddComponent <Rigidbody>();
                clone.AddComponent(coll.GetType()).GetComponent <Collider>().isTrigger = false;

                RBobj.useGravity   = false;
                RBclone.useGravity = false;

                RBobj.mass  /= 2;
                RBclone.mass = RBobj.mass;

                RBclone.angularDrag            = RBobj.angularDrag;
                RBclone.drag                   = RBobj.drag;
                RBclone.constraints            = RBobj.constraints;
                RBclone.interpolation          = RBobj.interpolation;
                RBclone.collisionDetectionMode = RBobj.collisionDetectionMode;

                Vector3 vel_obj    = RBobj.velocity;
                Vector3 angVel_obj = RBobj.angularVelocity;
                vel_obj -= GetComponent <Rigidbody>().velocity;
                //angVel_obj -= GetComponent<Rigidbody>().angularVelocity;
                vel_obj /= transform.localScale.magnitude;
                Vector3 vel_clone    = Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.TransformDirection(transform.InverseTransformDirection(vel_obj));
                Vector3 angVel_clone = Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.TransformDirection(transform.InverseTransformDirection(angVel_obj));
                vel_clone *= otherPortal.transform.localScale.magnitude;
                vel_clone += otherPortal.GetComponent <Rigidbody>().velocity;
                //angVel_clone += otherPortal.GetComponent<Rigidbody>().angularVelocity;
                RBclone.velocity        = vel_clone;
                RBclone.angularVelocity = angVel_clone;
            }

            //GameObject physClone = new GameObject();
            //IgnoreCollisions ICclone = physClone.AddComponent<IgnoreCollisions>();
            //ICclone.obj = obj;
        }
    }
    void OnTriggerEnter(Collider coll)
    {
        GameObject obj = coll.gameObject;

        //Debug.Log(obj.name);
        if (obj.layer != 14 && obj.layer != 21)
        {
            if (!inObjects.Contains(obj))
            {
                Debug.Log("Adding " + obj.name);
                inObjects.Add(obj);
                GameObject clone = new GameObject();
                if (obj.layer != 17)
                {
                    Rigidbody rb1 = obj.GetComponent <Rigidbody>();
                    if (rb1 != null)
                    {
                        masses.Add(rb1.mass);
                        Rigidbody rb2 = clone.AddComponent <Rigidbody>();
                        rb1.useGravity = false;
                        rb2.useGravity = false;

                        Vector3 v1 = rb1.velocity;
                        v1 -= transform.GetComponent <Rigidbody>().velocity;
                        v1 /= transform.lossyScale.magnitude;
                        Vector3 localVel = transform.InverseTransformDirection(v1);
                        Vector3 v2       = Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.TransformDirection(localVel);
                        v2          *= otherPortal.transform.lossyScale.magnitude;
                        v2          += otherPortal.GetComponent <Rigidbody>().velocity;
                        rb2.velocity = v2;
                    }
                    else
                    {
                        masses.Add(0);
                    }
                }
                else
                {
                    masses.Add(0);
                }
                layers.Add(obj.layer);
                obj.layer   = 14;
                clone.layer = 14;

                clone.transform.position = obj.transform.position;
                Vector3 scale = obj.transform.lossyScale;
                scale.x *= otherPortal.transform.lossyScale.x / transform.lossyScale.x;
                scale.y *= otherPortal.transform.lossyScale.y / transform.lossyScale.y;
                scale.z *= otherPortal.transform.lossyScale.z / transform.lossyScale.z;
                clone.transform.rotation = obj.transform.rotation;

                clone.transform.parent = transform;

                Vector3    pos      = clone.transform.localPosition;
                Quaternion rotation = clone.transform.localRotation;

                clone.transform.parent = otherPortal.transform;

                clone.transform.localPosition = pos;
                clone.transform.localRotation = rotation;

                clone.transform.parent     = null;
                clone.transform.localScale = scale;

                clone.transform.RotateAround(otherPortal.transform.position, otherPortal.transform.forward, 180);

                bool direction;
                if (Vector3.Dot(obj.transform.position - transform.position, transform.up) > 0)
                {
                    direction = true;
                    directions.Add(true);
                    clone.name = obj.name + " Clone (Front)" + gameObject.name;
                }
                else
                {
                    direction = false;
                    directions.Add(false);
                    clone.name = obj.name + " Clone (Back)" + gameObject.name;
                }
                moved.Add(false);
                clones.Add(clone);
                Material oldMaterial = obj.GetComponent <MeshRenderer>().material;
                originalMaterials.Add(oldMaterial);

                clone.AddComponent <MeshFilter>().mesh = obj.GetComponent <MeshFilter>().mesh;
                MeshRenderer MR = clone.AddComponent <MeshRenderer>();
                clone.GetComponent <Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.TwoSided;
                MR.material = clipPlaneMaterial;

                ClippableObject CO = clone.AddComponent <ClippableObject>();
                CO.clipPlanes = 1;
                if (direction)
                {
                    CO.plane1Position = otherPortal.transform.position - otherPortal.transform.up * offset;
                    CO.plane1Rotation = otherPortal.transform.eulerAngles;
                }
                else
                {
                    CO.plane1Position = otherPortal.transform.position + otherPortal.transform.up * offset;
                    CO.plane1Rotation = (Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.rotation).eulerAngles;
                }
                MR.material.CopyPropertiesFromMaterial(oldMaterial);

                CO            = obj.AddComponent <ClippableObject>();
                CO.clipPlanes = 1;
                if (direction)
                {
                    CO.plane1Position = transform.position - transform.up * offset;
                    CO.plane1Rotation = transform.eulerAngles;
                }
                else
                {
                    CO.plane1Position = transform.position + transform.up * offset;
                    CO.plane1Rotation = (Quaternion.AngleAxis(180, transform.forward) * transform.rotation).eulerAngles;
                }

                obj.GetComponent <MeshRenderer>().material.CopyPropertiesFromMaterial(oldMaterial);

                clone.AddComponent(coll.GetType()).GetComponent <Collider>().isTrigger = false;
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        for (int i = 0; i < clones.Count; ++i)
        {
            if (moved[i] == false)
            {
                GameObject clone = clones[i];
                GameObject obj   = inObjects[i];

                Vector3    oldPos = clone.transform.position;
                Quaternion oldRot = clone.transform.rotation;

                clone.transform.position = obj.transform.position;
                //clone.transform.position = obj.transform.position;
                Vector3 scale = obj.transform.lossyScale;
                scale.x *= otherPortal.transform.lossyScale.x / transform.lossyScale.x;
                scale.y *= otherPortal.transform.lossyScale.y / transform.lossyScale.y;
                scale.z *= otherPortal.transform.lossyScale.z / transform.lossyScale.z;
                clone.transform.rotation = obj.transform.rotation;

                clone.transform.parent = transform;

                Vector3    pos      = clone.transform.localPosition;
                Quaternion rotation = clone.transform.localRotation;

                clone.transform.parent = otherPortal.transform;

                clone.transform.localPosition = pos;
                clone.transform.localRotation = rotation;

                clone.transform.parent     = null;
                clone.transform.localScale = scale;

                clone.transform.RotateAround(otherPortal.transform.position, otherPortal.transform.forward, 180);

                if (layers[i] != 17)                 //not player
                {
                    Vector3 normal = transform.up;
                    if (directions[i] == false)
                    {
                        normal = -normal;
                    }
                    float d = Vector3.Dot(normal, obj.transform.position - transform.position);
                    float x = Mathf.Clamp(d / obj.GetComponent <Collider>().bounds.extents.magnitude, 0f, 1f) * 0.5f + 0.5f;
                    //Debug.Log("Fraction: "+x);
                    Rigidbody rb1 = obj.GetComponent <Rigidbody>();
                    Rigidbody rb2 = clone.GetComponent <Rigidbody>();
                    if (rb1 != null)
                    {
                        rb1.mass = masses[i] / 2;
                        rb2.mass = masses[i] / 2;
                        //rb1.AddForce(-Vector3.up * masses[i] * gravity);
                        //rb2.AddForce(-Vector3.up * masses[i] * (1f-x) * gravity);
                        Vector3 COM1 = obj.transform.InverseTransformDirection(normal) * (1f - x) / 2;
                        Vector3 COM2 = obj.transform.InverseTransformDirection(normal) * (x) / 2;



                        Vector3 v1 = rb1.velocity;
                        v1 -= transform.GetComponent <Rigidbody>().velocity;
                        v1 /= transform.lossyScale.magnitude;
                        Vector3 localVel = transform.InverseTransformDirection(v1);
                        Vector3 v2       = Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.TransformDirection(localVel);
                        v2          *= otherPortal.transform.lossyScale.magnitude;
                        v2          += otherPortal.GetComponent <Rigidbody>().velocity;
                        rb2.velocity = v2;
                        //Debug.DrawRay(obj.transform.position, rb1.velocity,Color.white, Time.deltaTime);
                        //Debug.DrawRay(clone.transform.position, rb2.velocity, Color.white, Time.deltaTime);


                        rb1.AddForceAtPosition(
                            -Vector3.up * masses[i] * gravity * x,
                            obj.transform.TransformPoint(COM1));
                        rb1.AddForceAtPosition(
                            Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.TransformDirection(transform.InverseTransformDirection(-Vector3.up)) * masses[i] * gravity * (1f - x),
                            obj.transform.TransformPoint(-COM2));


                        Debug.DrawRay(obj.transform.TransformPoint(COM1), Vector3.up * masses[i] * gravity * x, Color.green, Time.deltaTime);
                        Debug.DrawRay(obj.transform.TransformPoint(-COM2), Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.TransformDirection(transform.InverseTransformDirection(-Vector3.up)) * masses[i] * gravity * (1 - x), Color.red, Time.deltaTime);
                        //Vector3 COM2 = otherPortal.transform.up * clone.GetComponent<Collider>().bounds.extents.magnitude * (x);

                        //rb1.AddForceAtPosition(-Vector3.up * rb1.mass * gravity, obj.transform.position + COM1);
                        //rb2.AddForceAtPosition(-Vector3.up * rb2.mass * gravity, clone.transform.position + COM2);
                        //Debug.Log("Dist: " + (0.5f - x/2));
                        //Debug.DrawRay(obj.transform.position,transform.up * obj.GetComponent<Collider>().bounds.extents.magnitude,new Color(1,1,1,0.5f),Time.deltaTime);
                        //Debug.DrawRay(obj.transform.position,COM1,Color.black,Time.deltaTime);
                    }
                    //Debug.DrawRay(transform.position,transform.up*d,Color.red,Time.deltaTime);
                    //Vector3 posDiff = clone.transform.position - oldPos;
                    //Quaternion rotDiff = Quaternion.Inverse(oldRot)*clone.transform.rotation;
                    //Debug.DrawRay(oldPos,posDiff,Color.red,1);

                    //clone.transform.position = Vector3.Lerp(oldPos,clone.transform.position,0.5f);
                    //clone.transform.rotation = Quaternion.Lerp(oldRot,clone.transform.rotation,0.5f);
                    //Vector3 posDiff2 = otherPortal.transform.TransformDirection(transform.InverseTransformDirection(posDiff));
                    //Debug.DrawRay(obj.transform.position,posDiff2,Color.red,1);
                    //obj.transform.position += posDiff2/2;

                    //Vector3 objPos = obj.transform.position;
                    //Quaternion objRot = obj.transform.rotation;

                    scale = obj.transform.localScale;
                    obj.transform.position   = Vector3.Lerp(oldPos, clone.transform.position, 0.5f);
                    obj.transform.rotation   = Quaternion.Lerp(oldRot, clone.transform.rotation, 0.5f);
                    clone.transform.position = Vector3.Lerp(oldPos, clone.transform.position, 0.5f);
                    clone.transform.rotation = Quaternion.Lerp(oldRot, clone.transform.rotation, 0.5f);
                    //obj.transform.position = clone.transform.position;
                    //obj.transform.rotation = clone.transform.rotation;
                    obj.transform.localScale = clone.transform.localScale;

                    obj.transform.parent = otherPortal.transform;

                    pos      = obj.transform.localPosition;
                    rotation = obj.transform.localRotation;

                    obj.transform.parent = transform;

                    obj.transform.localPosition = pos;
                    obj.transform.localRotation = rotation;

                    obj.transform.parent     = null;
                    obj.transform.localScale = scale;

                    obj.transform.RotateAround(transform.position, transform.forward, 180);
                }

                bool direction = true;
                if (Vector3.Dot(obj.transform.position - transform.position, transform.up) < 0)
                {
                    direction = false;
                }

                ClippableObject CO = clone.GetComponent <ClippableObject>();
                CO.clipPlanes = 1;
                if (direction)
                {
                    CO.plane1Position = otherPortal.transform.position - otherPortal.transform.up * offset;
                    CO.plane1Rotation = otherPortal.transform.eulerAngles;
                }
                else
                {
                    CO.plane1Position = otherPortal.transform.position + otherPortal.transform.up * offset;
                    CO.plane1Rotation = (Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.rotation).eulerAngles;
                }

                ClippableObject CO2 = obj.GetComponent <ClippableObject>();
                CO2.clipPlanes = 1;
                if (direction)
                {
                    CO2.plane1Position = transform.position - transform.up * offset;
                    CO2.plane1Rotation = transform.eulerAngles;
                }
                else
                {
                    CO2.plane1Position = transform.position + transform.up * offset;
                    CO2.plane1Rotation = (Quaternion.AngleAxis(180, transform.forward) * transform.rotation).eulerAngles;
                }

                if (!(direction == directions[i]) && obj.transform.parent == null && moved[i] == false)
                {
                    moved[i] = true;

                    //Debug.Log(obj.name + " " +Vector3.Dot(obj.transform.position - transform.position,transform.up) + " " + gameObject.name);

                    /*
                     * scale = obj.transform.lossyScale;
                     * scale.x *= otherPortal.transform.lossyScale.x/transform.lossyScale.x;
                     * scale.y *= otherPortal.transform.lossyScale.y/transform.lossyScale.y;
                     * scale.z *= otherPortal.transform.lossyScale.z/transform.lossyScale.z;
                     *
                     * obj.transform.parent = transform;
                     *
                     * pos = obj.transform.localPosition;
                     * rotation = obj.transform.localRotation;
                     *
                     * obj.transform.parent = otherPortal.transform;
                     *
                     * obj.transform.localPosition = pos;
                     * obj.transform.localRotation = rotation;
                     *
                     * obj.transform.parent = null;
                     * obj.transform.localScale = scale;
                     *
                     * obj.transform.RotateAround(otherPortal.transform.position, otherPortal.transform.forward, 180);
                     */
                    pos      = obj.transform.position;
                    rotation = obj.transform.rotation;
                    scale    = obj.transform.localScale;

                    obj.transform.position   = clone.transform.position;
                    obj.transform.rotation   = clone.transform.rotation;
                    obj.transform.localScale = clone.transform.localScale;

                    clone.transform.position   = pos;
                    clone.transform.rotation   = rotation;
                    clone.transform.localScale = scale;

                    Rigidbody rb = obj.GetComponent <Rigidbody>();
                    if (rb != null)
                    {
                        rb.mass       = masses[i];
                        rb.useGravity = true;
                        Vector3 v1 = rb.velocity;
                        v1 -= transform.GetComponent <Rigidbody>().velocity;
                        v1 /= transform.lossyScale.magnitude;
                        Vector3 localVel = transform.InverseTransformDirection(v1);
                        Vector3 v2       = Quaternion.AngleAxis(180, otherPortal.transform.forward) * otherPortal.transform.TransformDirection(localVel);
                        v2         *= otherPortal.transform.lossyScale.magnitude;
                        v2         += otherPortal.GetComponent <Rigidbody>().velocity;
                        rb.velocity = v2;
                    }

                    CO  = obj.GetComponent <ClippableObject>();
                    CO2 = clone.GetComponent <ClippableObject>();

                    CO.plane1Position = otherPortal.transform.position;
                    CO.plane1Rotation = otherPortal.transform.eulerAngles;

                    CO2.plane1Position = transform.position;
                    CO2.plane2Rotation = transform.eulerAngles;

                    /*
                     * Vector3 clipPos = CO2.plane1Position;
                     * Vector3 clipRot = CO2.plane2Rotation;
                     *
                     * CO2.plane1Position = CO.plane1Position;
                     * CO2.plane1Rotation = CO.plane1Rotation;
                     * CO.plane1Position = clipPos;
                     * CO.plane1Rotation = clipRot;
                     */
                    /*
                     * obj.GetComponent<MeshRenderer>().material = originalMaterials[i];
                     *
                     * originalMaterials.RemoveAt(i);
                     * Destroy(obj.GetComponent<ClippableObject>());
                     * obj.layer = layers[i];
                     *
                     * Destroy(clones[i]);
                     * clones.RemoveAt(i);
                     * masses.RemoveAt(i);
                     * inObjects.RemoveAt(i);
                     * directions.RemoveAt(i);
                     * moved.RemoveAt(i);
                     * layers.RemoveAt(i);
                     * i=0;
                     */
                }
            }
        }
    }