Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've gone over this again and again, and my result is obviously wrong when viewed in-action. Here's the initial formula I converted (first one):

and here's my C# code:

float CalculateMaximumRange() {
    float g = Physics.gravity.y;
    float y = origin.position.y;
    float v = projectileSpeed;
    float a = 45;

    float vSin = v * Mathf.Sin(a);
    float vCos = v * Mathf.Cos(a);

    float sqrt = Mathf.Sqrt(vSin * vSin + 2 * g * y);

    return Mathf.Abs((vCos / g) * (vSin + sqrt));

Does anyone see what I did wrong?

share|improve this question
Two things, this is in 2d? And does your Trig functions require radians instead of degrees? – Chewy Gumball Mar 30 '11 at 2:14
Not only that but your variable names could be better. vSin should probably be v_x to represent that it it the X component of v for example. – Robert Massaioli Mar 30 '11 at 3:28
What is the MathF class anyway, I' used to seeing Math and MathHelper (float math), not MathF. – Roy T. Mar 30 '11 at 6:41
@Roy T, it's Unity's floating point math helper class. – Olhovsky Mar 30 '11 at 20:22
Do you have unit tests where you verify the range given a bunch of different inputs? – ashes999 Mar 31 '11 at 11:07
up vote 7 down vote accepted

Use degrees instead of radians.

45 degrees is Pi/4 radians ~= 0.785398.

The y in your formula should be the object's distance above the ground when it is released. Is the ground 0? If the ground is not at y=0, then y should be:

y = origin.Position.y + groundHeight

Finally, are you sure that this formula is giving you what you want? This formula gives the horizontal distance of the object's travel only -- not the distance of the object along the path (which is longer or equal to the horizontal travel).

Based on your response, it sounds like you're actually looking for the maximum possible distance that can be achieved.

To find the angle that gives the maximum possible distance, use this (using your variable names):

 a = arccos(sqrt((2*g*y + v*v)/(2*g*y + 2*v*v)))

Now you can plug a into the equation you already have to find what the distance is at that angle.

Now you have both the angle that produces the maximum distance, and the maximum possible distance itself!

For example: g = 30, y = 3.85, v = 35, gives:

Wolfram Alpha computation.

a = 0.7422637 radians = 42.53 degrees.

Then plugging a into your original formula gives range = 44.517.

share|improve this answer
"This formula gives the horizontal distance of the object's travel only" -- CalculateMaximumRange looks just right. – Kromster Mar 30 '11 at 5:46
Judging by "MathF", Joren is using Unity. In Unity MathF.Sin and MathF.Cos indeed work with degrees, not radians. – Nevermind Mar 30 '11 at 6:55
Joren should definitely check his constants (g) and units (g, v, y, a, result) – Kromster Mar 30 '11 at 8:50
@Nevermind: Mathf.Sin appears to use radians:… – Olhovsky Mar 30 '11 at 9:08
WTF, I could've sworn it uses degrees. OK, I was wrong, sorry. – Nevermind Mar 30 '11 at 13:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.