Ejemplo n.º 1
0
        public override sealed void UpdateExtension(TargetConstraint t)
        {
            // Linear limit
            t.joint.xMotion = ConfigurableJointMotion.Locked;
            t.joint.yMotion = ConfigurableJointMotion.Locked;
            t.joint.zMotion = ConfigurableJointMotion.Limited;

            limitSpring.spring        = spring;
            limitSpring.damper        = damper;
            t.joint.linearLimitSpring = limitSpring;

            worldLimitStart = t.connectedAnchor.position - t.GetJointAxisWorldRotation() * Vector3.forward * limit.limit;
            worldLimitEnd   = t.connectedAnchor.position + t.GetJointAxisWorldRotation() * Vector3.forward * limit.limit;

            if (showLimitedAxis)
            {
                Debug.DrawLine(worldLimitStart, worldLimitEnd, Color.black);
            }

            // Threshold detection
            if (detectThreshold)
            {
                if (togglingThresholdDetectionIn <= 0.0f)
                {
                    closestToLine = BasicHelpers.NearestPointOnFiniteLine(worldLimitStart, worldLimitEnd, t.anchor.position);

                    if (inverted)
                    {
                        positionLerp = Vector3.Distance(worldLimitStart, closestToLine) / (limit.limit * 2.0f);
                    }
                    else
                    {
                        positionLerp = Vector3.Distance(worldLimitEnd, closestToLine) / (limit.limit * 2.0f);
                    }

                    if (positionLerp > threshold && thresholdState != ThresholdState.Over)
                    {
                        togglingThresholdDetectionIn = minTimeBetweenDetections;
                        thresholdState = ThresholdState.Over;
                        onOverThreshold.Invoke();
                    }
                    else if (positionLerp < threshold && thresholdState != ThresholdState.Under)
                    {
                        togglingThresholdDetectionIn = minTimeBetweenDetections;
                        thresholdState = ThresholdState.Under;
                        onUnderThreshold.Invoke();
                    }
                }
                else
                {
                    togglingThresholdDetectionIn -= Time.deltaTime;
                }
            }
        }
Ejemplo n.º 2
0
        void FixedUpdateJoint(TargetConstraint t)
        {
            if (editMode && t.joint)
            {
                // Anchor
                t.joint.anchor = t.joint.transform.InverseTransformPoint(t.anchor.position);

                // Axis
                t.axis.position = t.connectedAnchor.position;
                t.axis.rotation = t.GetJointAxisWorldRotation();
            }
        }
Ejemplo n.º 3
0
        void InstantiateAxis(TargetConstraint t)
        {
            if (t.axis)
            {
                return;
            }

            Transform axis = new GameObject(t.name + ".Axis").transform;

            axis.position = t.connectedAnchor.position;
            axis.rotation = t.GetJointAxisWorldRotation();

            axis.parent = t.connectedAnchor.parent;

            t.axis = axis;
        }
Ejemplo n.º 4
0
        void EnableTarget(TargetConstraint t)
        {
            if (!t.pheasy)
            {
                t.pheasy = this;
            }

            if (!t.joint)
            {
                InstantiateJoint(t);
            }

            if (!t.anchor)
            {
                InstantiateAnchor(t);
            }

            if (!t.connectedAnchor)
            {
                InstantiateConnectedAnchor(t);
            }

            if (t.name == null || t.name == "")
            {
                t.name = t.anchor.name + "->" + t.connectedAnchor.name;
            }

            if (t.keepAxisRelativeToObject)
            {
                t.joint.configuredInWorldSpace = false;
            }
            else
            {
                t.joint.configuredInWorldSpace = true;
            }

            if (!t.axis)
            {
                t.setAxisWhenEnabled = false;

                InstantiateAxis(t);
            }
            else
            {
                t.setAxisWhenEnabled = true;

                t.SetAxis(t.axis.rotation);
            }

            if (t.connectedBody)
            {
                t.joint.connectedBody  = t.connectedBody;
                t.initialLocalRotation = t.joint.connectedBody.transform.localRotation;
            }

            t.initialWorldRotation = t.joint.transform.rotation;

            t.jointFrameRotation = t.GetJointAxisWorldRotation();

            if (axis != null)
            {
                if (!t.anchorAxis)
                {
                    t.anchorAxis = Instantiate(axis, rb.transform);
                    UpdateAxis(t.anchorAxis.transform, t.anchor, axisScale);
                }

                if (!t.connAnchorAxis)
                {
                    t.connAnchorAxis = Instantiate(axis, rb.transform);
                    UpdateAxis(t.connAnchorAxis.transform, t.connectedAnchor, axisScale);
                }
            }

            t.gradualLerp = 0.0f;

            t.extensions.ForEach(e => e.InitExtension(t));

            t.enabled = true;
        }
Ejemplo n.º 5
0
        void UpdateJoint(TargetConstraint t, bool editMode)
        {
            if (editMode)
            {
                // Connected body/anchor
                if (t.connectedBody)
                {
                    t.joint.connectedBody = t.connectedBody;
                }
                else
                {
                    t.joint.connectedBody = null;
                }

                // Freedom
                SetPositionLock(t.joint, t.settings.linearMotion);
                SetRotationLock(t.joint, t.settings.angularMotion);

                // Drives
                if (gradualMode)
                {
                    PhysHelpers.UpdateJointMotionDrive(t.joint, PhysHelpers.JointDriveLerp(new JointDrive(), t.settings.motionDrive.toJointDrive(), t.gradualLerp));
                    PhysHelpers.UpdateJointAngularDrive(t.joint, PhysHelpers.JointDriveLerp(new JointDrive(), t.settings.angularDrive.toJointDrive(), t.gradualLerp));
                }
                else
                {
                    PhysHelpers.UpdateJointMotionDrive(t.joint, t.settings.motionDrive.toJointDrive());
                    PhysHelpers.UpdateJointAngularDrive(t.joint, t.settings.angularDrive.toJointDrive());
                }

                // Control
                t.joint.enableCollision = t.settings.collideWithConnectedRb;

                // Prevent modification
                t.joint.configuredInWorldSpace = !t.keepAxisRelativeToObject;
            }

            if (!t.setAxisWhenEnabled)
            {
                t.joint.autoConfigureConnectedAnchor = false;                        // Called every frame
            }
            // Connected body/anchor
            if (t.connectedBody)
            {
                t.tmpConnAnchor = t.joint.connectedBody.transform.InverseTransformPoint(t.connectedAnchor.position);
            }
            else
            {
                t.tmpConnAnchor = t.connectedAnchor.position;
            }

            if (t.joint.connectedAnchor != t.tmpConnAnchor)
            {
                t.joint.connectedAnchor = t.tmpConnAnchor;
            }

            // Target rotation
            t.jointFrameRotation = t.GetJointAxisWorldRotation();

            Quaternion resultRotation = Quaternion.Inverse(t.jointFrameRotation);

            resultRotation *= t.anchor.rotation;
            resultRotation *= Quaternion.Inverse(t.connectedAnchor.rotation);
            resultRotation *= t.jointFrameRotation;

            t.joint.targetRotation = resultRotation;

            // Target position
            if (t.targetPosition)
            {
                t.tmpTargetPos = -1.0f * t.axis.InverseTransformPoint(t.targetPosition.position);
                if (t.connectedBody)
                {
                    t.joint.targetPosition = Vector3.Scale(t.tmpTargetPos, t.connectedBody.transform.localScale);
                }
                else
                {
                    t.joint.targetPosition = t.tmpTargetPos;
                }
            }
            else if (t.joint.targetPosition != Vector3.zero)
            {
                t.joint.targetPosition = Vector3.zero;
            }

            // Update flag
            if (!t.updated)
            {
                t.updated = true;
            }

            // Debug
            if (t.showAxis)
            {
                Debug.DrawLine(t.axis.position, t.axis.position + t.GetJointAxisWorldRotation() * Vector3.forward * 0.5f, Color.blue);
            }
        }