This site uses strictly necessary cookies. More Information

X- Home /

# Firing A Projectile Along A Parabola

Hey,

My mathematical ability is horrible, at best. I am genuinely trying to improve it when I can. I'm making a boat game (3D), and am trying to fire my projectile along a parabola without the physics system at the moment. Forgive my naivety but In all honesty I thought i could just use the Wiki page formulae, but I was horribly wrong.

How do I fire and also take into consideration how high my boat turret is facing?

Current code:

```
float t = 0;
for (int i = 0; i < resolution; i++)
{
float ang = transform.rotation.x * Mathf.Deg2Rad;
float v = 20f;
float x = v * t * Mathf.Cos(ang);
float y = v * t * Mathf.Sin(ang) - 0.5f * 9.18f * Mathf.Pow(t, 2);
Vector3 porabPoint = transform.position + new Vector3(x, y, 0);
Gizmos.DrawSphere(porabPoint, 0.2f);
t += 0.01f;
}
```

I presume something is wrong with my angle calculation? Should I be using atan2?

Thanks!

**Answer** by unity_ek98vnTRplGj8Q
·
Feb 20, 2020 at 10:22 PM

I should start by mentioning that this is a needlessly complicated way to calculate a trajectory, even without using the physics system. However since you already have it typed out, I can tell you that you have 2 issues. Firstly, you want to be using the euler angles of the rotation; these are the actual values that indicate how far around each axis you are rotated. The w, x, y, and z variables of the the rotation are something *completely* different and you should almost never touch them.

Second, you have an axis mismatch. You are checking the rotation around the x axis, and then plotting the result on the x/y plane. If you are making a 3D game, then you should plot the results on the *local* Y/Z plane. If you are making a 2D game its a bit simpler, but you should be checking the rotation about the Z axis.

For 2D:

```
float t = 0;
for (int i = 0; i < resolution; i++)
{
float ang = transform.rotation.eulerAngles.z * Mathf.Deg2Rad;
float v = 20f;
float x = v * t * Mathf.Cos(ang);
float y = v * t * Mathf.Sin(ang) - 0.5f * 9.18f * Mathf.Pow(t, 2);
Vector3 porabPoint = transform.position + new Vector3(x, y, 0);
Gizmos.DrawSphere(porabPoint, 0.2f);
t += 0.01f;
}
```

And for 3D:

```
float t = 0;
for (int i = 0; i < resolution; i++)
{
float ang = -1 * transform.rotation.eulerAngles.x * Mathf.Deg2Rad;
float v = 20f;
float x = v * t * Mathf.Cos(ang);
float y = v * t * Mathf.Sin(ang) - 0.5f * 9.18f * Mathf.Pow(t, 2);
//I'm rotating the position to match the heading of the game object
Vector3 porabPoint = transform.position + (Quaternion.Euler(0,transform.rotation.eulerAngles.y, 0) * new Vector3(0, y, x));
Gizmos.DrawSphere(porabPoint, 0.2f);
t += 0.01f;
}
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to find an intercept on a moving target 2 Answers

Perlin Noise applied to plane, except endpoints 1 Answer

I want to create an interactive right analog MouseLook? 0 Answers