Пример #1
0
    private void Update()
    {
        if (controller && !controller.IsMe)
        {
            return;
        }

        if (limb && addLimbForce > 1f)
        {
            limb.addedForce       *= addLimbForce;
            controller.addedForce *= addLimbForce;
            Debug.Log(addLimbForce);
        }

        if (Input.GetKeyDown(key))
        {
            if (joint.enabled)
            {
                if (joint.connectedBody)
                {
                    addLimbForce = 1;
                    ig.EnableColliders(currentId);
                    joint.connectedBody.mass = mass;
                    joint.connectedBody      = null;
                }
                joint.enabled = false;
            }
            else
            {
                var colliders = Physics2D.OverlapCircleAll(transform.position, range);
                foreach (var coll in colliders)
                {
                    if (coll.transform.IsChildOf(parent))
                    {
                        continue;
                    }
                    var rigidbody = coll.GetComponent <Rigidbody2D>();
                    if (rigidbody)
                    {
                        currentId = rigidbody.GetInstanceID();

                        var cd  = coll.Distance(this.coll);
                        var pos = cd.pointA - cd.pointB;
                        rb.position += pos;

                        joint.connectedBody = rigidbody;
                        mass           = rigidbody.mass;
                        rigidbody.mass = 1f;


                        joint.enabled = true;
                        if (!rigidbody.isKinematic)
                        {
                            var parent = rigidbody.transform;
                            while (parent.parent)
                            {
                                parent = parent.parent;
                            }
                            var col = parent.GetComponentsInChildren <Collider2D>();
                            ig.AddIgnoreList(currentId, col);
                            addLimbForce = Mathf.Log(col.Length, 2) * 1.1f;

                            break;
                        }
                    }
                    else
                    {
                        var cd  = coll.Distance(this.coll);
                        var pos = cd.pointA - cd.pointB;
                        rb.position  += pos;
                        joint.enabled = true;
                    }
                }
            }
        }
    }