Esempio n. 1
0
        //join with other shape by FixedJoint2D
        public bool JoinTo(MeshBase otherMesh)
        {
            FixedJoint2D C_FJ2D = gameObject.AddComponent <FixedJoint2D>();

            C_FJ2D.connectedBody = otherMesh.GetComponent <Rigidbody2D>();
            if (C_FJ2D.connectedBody == null)
            {
                return(false);
            }
            C_FJ2D.anchor = transform.InverseTransformPoint(transform.position);
            C_FJ2D.anchor = otherMesh.transform.InverseTransformPoint(otherMesh.transform.position);
            return(true);
        }
Esempio n. 2
0
        //joins two shapes by FixedJoint2D
        public static bool Join(MeshBase meshA, MeshBase meshB)
        {
            FixedJoint2D C_FJ2D = meshA.gameObject.AddComponent <FixedJoint2D>();

            C_FJ2D.connectedBody = meshB.GetComponent <Rigidbody2D>();
            if (C_FJ2D.connectedBody)
            {
                return(false);
            }
            C_FJ2D.anchor = meshA.transform.InverseTransformPoint(meshA.transform.position);
            C_FJ2D.anchor = meshB.transform.InverseTransformPoint(meshB.transform.position);
            return(true);
        }
        private void ConnectBody(RaycastHit2D hit)
        {
            //assign hit object's Rigidbody to {connectedBody}
            connectedBody = hit.collider.GetComponent <Rigidbody2D>();

            //save its drag
            oldDrag            = connectedBody.drag;
            connectedBody.drag = 10f;

            //attach SpringJoint to hit collider
            C_SP2D = hit.collider.gameObject.AddComponent <SpringJoint2D>();
            C_SP2D.autoConfigureDistance = false;
            C_SP2D.distance     = 0;
            C_SP2D.dampingRatio = 1f;
            C_SP2D.frequency    = 5f;

            //choose the place to anchor spring joint
            if (attachToCenterOfMass)
            {
                MeshBase connectedMeshBase = hit.collider.GetComponent <MeshBase>();
                //check if selected Rigidbody has MeshBase derived component
                if (connectedMeshBase != null)
                {
                    //GetCenter overrides object origin
                    C_SP2D.anchor = connectedBody.transform.InverseTransformPoint(connectedMeshBase.transform.position);
                }
                else
                {
                    //set to generated center of mass
                    C_SP2D.anchor = connectedBody.transform.InverseTransformPoint(connectedBody.transform.position);
                }
            }
            else
            {
                //set anchor to point of hit
                C_SP2D.anchor = connectedBody.transform.InverseTransformPoint(hit.point);
            }
        }
Esempio n. 4
0
        void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                //raycast from mouse pointer
                RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
                if (hit.collider != null)
                {
                    //if we hit something...
                    if (hit.collider.GetComponent <Rigidbody2D>())
                    {
                        //...and it has Rigidbody2D

                        //assign hit object's Rigidbody to {connectedBody}
                        connectedBody = hit.collider.GetComponent <Rigidbody2D>();

                        //save its drag
                        oldDrag            = connectedBody.drag;
                        connectedBody.drag = 10f;

                        //attach SpringJoint to hit collider
                        C_SP2D = hit.collider.gameObject.AddComponent <SpringJoint2D>();
                        C_SP2D.autoConfigureDistance = false;
                        C_SP2D.distance     = 0;
                        C_SP2D.dampingRatio = 1f;
                        C_SP2D.frequency    = 5f;

                        //choose the place to anchor spring joint
                        if (attachToCenterOfMass)
                        {
                            MeshBase connectedMeshBase = hit.collider.GetComponent <MeshBase>();
                            //check if selected Rigidbody has MeshBase derived component
                            if (connectedMeshBase != null)
                            {
                                //GetCenter overrides object origin
                                C_SP2D.anchor = connectedBody.transform.InverseTransformPoint(connectedMeshBase.transform.position);
                            }
                            else
                            {
                                //set to generated center of mass
                                C_SP2D.anchor = connectedBody.transform.InverseTransformPoint(connectedBody.transform.position);
                            }
                        }
                        else
                        {
                            //set anchor to point of hit
                            C_SP2D.anchor = connectedBody.transform.InverseTransformPoint(hit.point);
                        }
                    }
                }
            }
            if (Input.GetMouseButtonUp(0))
            {
                if (connectedBody != null)
                {
                    //remove joint from body
                    Destroy(C_SP2D);
                    connectedBody.gravityScale = 1;
                    connectedBody.drag         = oldDrag;
                    C_SP2D        = null;
                    connectedBody = null;
                    SetLineRendererPositions(Vector3.zero, Vector3.zero);
                }
            }
        }