Ejemplo n.º 1
0
        //Applys the alignment
        private void ApplyAlignment(CGF cgf, Rigidbody rigid, Collider coll)
        {
            if (_filterProperties == null)
            {
                return;
            }

            if (Enable)
            {
                if (_filterProperties.ValidateFilters(rigid, coll))
                {
                    var transPos = this.transform.position;

                    switch (cgf._forcePositionProperties.ForcePosition)
                    {
                    case CGF.ForcePosition.ThisTransform:
                        break;

                    case CGF.ForcePosition.ClosestCollider:
                        if (cgf._forcePositionProperties.ClosestColliders != null)
                        {
                            if (cgf._forcePositionProperties.ClosestColliders.Count > 0)
                            {
                                float heightOffset = 0f;

                                if (UseClosestColliderHeightOffset)
                                {
                                    heightOffset = cgf._forcePositionProperties.HeightOffset;
                                }

                                if (!cgf._forcePositionProperties.UseEffectedClosestPoint)
                                {
                                    var point = cgf.FindClosestPoints(rigid.position, cgf._forcePositionProperties.ClosestColliders);

                                    transPos = cgf.GetVectorHeightOffset(point, rigid.position, heightOffset);
                                }
                                else
                                {
                                    Vector3 pointA = cgf.FindClosestPoints(coll.transform.position, cgf._forcePositionProperties.ClosestColliders);
                                    Vector3 pointB = cgf.FindClosestPoints(pointA, coll);

                                    float distanceThisA = Vector3.Distance(coll.transform.position, pointA);
                                    float distanceAB    = Vector3.Distance(pointA, pointB);

                                    transPos = cgf.GetVectorHeightOffset(pointA, coll.transform.position, Mathf.Abs(distanceThisA - distanceAB) + heightOffset);
                                }
                            }
                        }
                        break;
                    }

                    Vector3 newLocal = Vector3.zero;

                    switch (_alignDirection)
                    {
                    case AlignDirection.Up:
                        newLocal = -rigid.transform.up;
                        break;

                    case AlignDirection.Down:
                        newLocal = rigid.transform.up;
                        break;

                    case AlignDirection.Left:
                        newLocal = rigid.transform.right;
                        break;

                    case AlignDirection.Right:
                        newLocal = -rigid.transform.right;
                        break;

                    case AlignDirection.Forward:
                        newLocal = -rigid.transform.forward;
                        break;

                    case AlignDirection.Backward:
                        newLocal = rigid.transform.forward;
                        break;
                    }

                    Quaternion targetRotation = Quaternion.FromToRotation(newLocal, rigid.position - transPos) * rigid.rotation;

                    Quaternion deltaRotation    = Quaternion.Inverse(rigid.rotation) * targetRotation;
                    Vector3    deltaAngles      = GetRelativeAngles(deltaRotation.eulerAngles);
                    Vector3    worldDeltaAngles = rigid.transform.TransformDirection(deltaAngles);

                    rigid.maxAngularVelocity = MaxAngularVelocity;
                    rigid.AddTorque((RotateSpeed * worldDeltaAngles) - ((AngularVelocityDamping * rigid.angularVelocity)));

                    if (OnApplyAlignToForceEvent != null)
                    {
                        OnApplyAlignToForceEvent.Invoke(cgf, rigid, coll, transPos);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        //Applys the alignment
        private void ApplyAlignment(CGF2D cgf, Rigidbody2D rigid, Collider2D coll)
        {
            if (_filterProperties == null)
            {
                return;
            }

            if (Enable)
            {
                if (_filterProperties.ValidateFilters(rigid, coll))
                {
                    var transPos = this.transform.position;

                    switch (cgf._forcePositionProperties.ForcePosition)
                    {
                    case CGF.ForcePosition.ThisTransform:
                        break;

                    case CGF.ForcePosition.ClosestCollider:
                        if (cgf._forcePositionProperties.ClosestColliders != null)
                        {
                            if (cgf._forcePositionProperties.ClosestColliders.Count > 0)
                            {
                                float heightOffset = 0f;

                                if (UseClosestColliderHeightOffset)
                                {
                                    heightOffset = cgf._forcePositionProperties.HeightOffset;
                                }

                                if (!cgf._forcePositionProperties.UseEffectedClosestPoint)
                                {
                                    var point = cgf.FindClosestPoints(coll, cgf._forcePositionProperties.ClosestColliders, false);
                                    transPos = cgf.GetVectorHeightOffset(point, coll.transform.position, heightOffset);
                                }
                                else
                                {
                                    Vector3 pointA = cgf.FindClosestPoints(coll, cgf._forcePositionProperties.ClosestColliders, false);
                                    Vector3 pointB = cgf.FindClosestPoints(coll, cgf._forcePositionProperties.ClosestColliders, true);

                                    float distanceThisA = Vector3.Distance(coll.transform.position, pointA);
                                    float distanceAB    = Vector3.Distance(pointA, pointB);

                                    transPos = cgf.GetVectorHeightOffset(pointA, coll.transform.position, Mathf.Abs(distanceThisA - distanceAB) + heightOffset);
                                }
                            }
                        }
                        break;
                    }

                    Vector3 newLocal = Vector3.zero;

                    switch (_alignDirection)
                    {
                    case AlignDirection.Up:
                        newLocal = rigid.transform.up;
                        break;

                    case AlignDirection.Down:
                        newLocal = -rigid.transform.up;
                        break;

                    case AlignDirection.Left:
                        newLocal = -rigid.transform.right;
                        break;

                    case AlignDirection.Right:
                        newLocal = rigid.transform.right;
                        break;
                    }

                    float angle = LookAtAngle(rigid, newLocal, transPos);

                    rigid.AddTorque((RotateSpeed * angle) - ((rigid.angularVelocity * AngularVelocityDamping) * Time.deltaTime));

                    if (OnApplyAlignToForceEvent != null)
                    {
                        OnApplyAlignToForceEvent.Invoke(cgf, rigid, coll, transPos);
                    }
                }
            }
        }