Example #1
0
        private void SetupJoint()
        {
            _joint = gameObject.AddComponent <ConfigurableJoint>();
            _joint.connectedBody = ConnectedBody;
            //_joint.anchor = StartPosition;
            _joint.autoConfigureConnectedAnchor = false;

            var worldStartPosition = StartPosition;

            if (transform.parent)
            {
                worldStartPosition = transform.parent.TransformPoint(StartPosition);
            }

            var worldEndPosition = EndPosition;

            if (transform.parent)
            {
                worldEndPosition = transform.parent.TransformPoint(EndPosition);
            }


            if (ConnectedBody)
            {
                _joint.connectedAnchor = ConnectedBody.transform.InverseTransformPoint(worldStartPosition);
            }
            else
            {
                _joint.connectedAnchor = worldStartPosition;
            }

            _joint.SetXDrive(Spring, Damper, Spring);

            _joint.LimitXMotion();
            _joint.LockYMotion();
            _joint.LockZMotion();
            _joint.LockAllAngularMotion();
            _joint.axis          = _axis;
            _joint.secondaryAxis = _joint.axis.OrthogonalVector();
            _joint.SetLinearLimit(Vector3.Distance(StartPosition, EndPosition));

            _limitJoint = gameObject.AddComponent <ConfigurableJoint>();
            _limitJoint.connectedBody = ConnectedBody;
            //_limitJoint.anchor = EndPosition;
            _limitJoint.autoConfigureConnectedAnchor = false;

            if (ConnectedBody)
            {
                _limitJoint.connectedAnchor = ConnectedBody.transform.InverseTransformPoint(worldEndPosition);
            }
            else
            {
                _limitJoint.connectedAnchor = worldEndPosition;
            }

            _limitJoint.LockYMotion();
            _limitJoint.LockZMotion();
            _limitJoint.LockAllAngularMotion();
            _limitJoint.axis          = _axis;
            _limitJoint.secondaryAxis = _joint.axis.OrthogonalVector();
            _limitJoint.LimitXMotion();
            _limitJoint.SetLinearLimit(Vector3.Distance(StartPosition, EndPosition));
        }