Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Calculates the necessary initial velocity to loft an object from the start position (with the given inclination angle) to end position
- /// </summary>
- /// <param name="startPos">the start of the loft</param>
- /// <param name="inclinationAngle">the angle (up from flat) to loft at</param>
- /// <param name="targetPos">the target point to hit with the loft</param>
- /// <param name="minThrowDistance">the minimum throw distance to allow (fixes issues with throwing infinitely hard straight up)</param>
- /// <returns>the velocity to assign to an object to make it loft up and land perfectly at the target position</returns>
- static public Vector3 ThrowVelocityToPoint(Vector3 startPos, float inclinationAngle, Vector3 targetPos, float minThrowDistance)
- {
- // Get the flat-direction from thrower to target position (and flat-distance to target while we're at it)
- Vector3 flatDir = targetPos - startPos;
- float vertDis = flatDir.y;
- flatDir.y = 0.0f;
- float horizDis = flatDir.magnitude;
- flatDir.Normalize();
- if (horizDis < minThrowDistance) { horizDis = minThrowDistance; }
- // Now calculate the throw dir based on throw angle and flat-dir
- Vector3 rightDir = Vector3.Cross(Vector3.up, flatDir);
- Vector3 throwDir = Quaternion.AngleAxis(-inclinationAngle, rightDir) * flatDir;
- // Equation: v^2 = (g * (horiz_distance)^2) / ( 2.0 * ( (horiz_distance * tan(angle)) - vert_distance ) * cos^2(angle) )
- float gravity = Mathf.Abs(Physics.gravity.y);
- float initialSpeed = Mathf.Sqrt((gravity * horizDis * horizDis) / (2.0f * ((horizDis * Mathf.Tan(Mathf.Deg2Rad * inclinationAngle)) - vertDis) * Mathf.Cos(Mathf.Deg2Rad * inclinationAngle) * Mathf.Cos(Mathf.Deg2Rad * inclinationAngle)));
- return throwDir * initialSpeed;
- }
Advertisement
Add Comment
Please, Sign In to add comment