Skip to content

Instantly share code, notes, and snippets.

@pr00thmatic
Last active April 28, 2021 15:20
Show Gist options
  • Save pr00thmatic/d4cb8fc0b431a6807a92a23404f648a3 to your computer and use it in GitHub Desktop.
Save pr00thmatic/d4cb8fc0b431a6807a92a23404f648a3 to your computer and use it in GitHub Desktop.
public Vector3 ThrowBall (Vector3 origin, Vector3 destination,
float overHeight = 0.5f, bool hProportionalToDistance = true,
float maximumDistanceToCapHeight = 1) {
Vector3 force = Vector3.zero;
float distance = (Utils.SetY(0, origin) - Utils.SetY(0, destination)).magnitude;
float height = Mathf.Abs(origin.y - destination.y) + overHeight *
(hProportionalToDistance? Mathf.Max(maximumDistanceToCapHeight, distance): 1);
float gravity = Physics.gravity.magnitude;
force.y = Mathf.Sqrt(2 * gravity * height);
float t1 = force.y / gravity;
float t2 = Mathf.Sqrt(2 * gravity * (height - (destination.y - origin.y))) / gravity;
force += (distance / (t1 + t2)) * (Utils.SetY(0, destination) - Utils.SetY(0, origin)).normalized;
// StartCoroutine(_Break(t1 + t2));
return force;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment