public Vector3 GetPlanePosition()
 {
     return(MathExt.ProjectPointOnPlane(Plane.up, Plane.position, ToProject.position) + HeightOffset * Plane.up);
 }
示例#2
0
        public static Vector3 GetPushForceNeeded(float mass, Vector3 velocity, Vector3 position, Vector3 target,
                                                 float maxVelocity             = Mathf.Infinity, float maxDownVelocityDifference = Mathf.Infinity,
                                                 float maxUpVelocityDifference = Mathf.Infinity, float maxForce                  = Mathf.Infinity,
                                                 Vector3 forceAxisNullifier    = new Vector3(), bool logConstrains               = false)
        {
            Vector3 outVelocity = target - position;

#if UNITY_EDITOR
            //Logging the velocity difference change
            if (logConstrains)
            {
                if (outVelocity.magnitude < velocity.magnitude - maxDownVelocityDifference)
                {
                    UnityEngine.Debug.Log("Velocity Down Difference Clamped, Expected: " + outVelocity.magnitude
                                          + " Clamped: " +
                                          (velocity.magnitude - maxDownVelocityDifference
                                          ));
                }

                if (outVelocity.magnitude > velocity.magnitude + maxUpVelocityDifference)
                {
                    UnityEngine.Debug.Log("Velocity Up Difference Clamped, Expected: " + outVelocity.magnitude
                                          + " Clamped: " +
                                          (velocity.magnitude + maxUpVelocityDifference));
                }
            }
#endif
            //clamping the velocity difference
            if (maxUpVelocityDifference > 0)
            {
                outVelocity = outVelocity.GetClampedMagnitude(
                    velocity.magnitude - maxDownVelocityDifference,
                    velocity.magnitude + maxUpVelocityDifference);
            }

            //clamping the max velocity
            outVelocity = Vector3.ClampMagnitude(outVelocity, maxVelocity * UnityEngine.Time.deltaTime);

#if UNITY_EDITOR
            if (logConstrains)
            {
                if ((target - position).magnitude > maxVelocity)
                {
                    UnityEngine.Debug.Log("Max Velocity Clamped, Expected: "
                                          + (target - position).magnitude + "Clamped: " + maxVelocity);
                }
            }
#endif
            //Calculating the force required
            outVelocity = (outVelocity / TimeUtils.safeDeltaTime - velocity) / TimeUtils.safeDeltaTime
                          * mass;


#if UNITY_EDITOR
            //Logging the max force clamping
            if (logConstrains)
            {
                if (!(outVelocity.magnitude <= maxForce))
                {
                    UnityEngine.Debug.Log("Max Force Clamped, Expected: "
                                          + outVelocity.magnitude + " Clamped: " + maxForce);
                }
            }
#endif
            //Clamping the max force
            outVelocity = Vector3.ClampMagnitude(outVelocity, maxForce);

            //removing undesired axis
            if (forceAxisNullifier != Vector3.zero)
            {
                outVelocity = MathExt.ProjectVectorOnPlane(outVelocity, forceAxisNullifier);
            }

            return(outVelocity);
        }