Figuring out projectile trajectory should be pretty easy right? Well, I’m not really a math guy. This is not a statement of character, just of condition. I’m good at math — I’ve always been able to grasp concepts when I put my mind to them. But, for years, I’ve told myself that I’m not a math guy.

I recently completed Math 12 (as a 29 year old) in order to get back into university, and I did well (really well). But I still don’t know very much.

I’ll do a bigger post on maths in game development soon — the basics seem to be hella useful and I am going to get deeper into it when I get back in school — but I’m just going to touch in on my struggles with projectiles here.

Projectiles are not very important in my game. Though many spells and attacks USE projectiles, they aren’t physics objects with hit boxes. There’s no collision (per-se). I’m also not using any physics from the Unity engine. All the projectiles do is follow a path and check if they have reached the “x” point of an target in order to “resolve.”

Making projectiles go straight is easy. However, I wanted to have the option to arc them nicely.

## Using Actual Ballistics Calculations

Might as well do it right the first time… right?

I started with this Wikipedia article on calculating trajectories of projectiles.

Inputting this created projectiles that flew everywhere, very fast. I realized that I am definitely not using the pre-approved Unity world-units (since I thought I could make this game with no physics), and would have to tweak gravity and my scene structure to get any sort of sensible result. I tried this for a while. Then I gave up.

Next I had a look at this one…

…but it seemed to be much more robust (accurate, complicated, and possibly processor-intensive) than I would need.

If I were starting this project over, I could do a little bit more early work to make sure that the physics would make sense. But I’m not going to do that.

## Faking Projectile Trajectories

In reality, I didn’t need projectiles to do much more than look reasonable and give me some sort of sense of attack timing. So I decided to fake it.

My current WIP ballistics code uses calculations from this answer on Stack Exchange.

**Here’s what I have.**

First, my co-routine checks if the projectile is at its target. If not, it moves it along based on the calculated velocity:

private IEnumerator RunPath() { while (!at_target) { if (PositionWithinPointOhFiveOfTarget(target)) { at_target = true; AtTarget(); } else { MoveToward(target); } yield return new WaitForSeconds(.025f); yield return null; } yield break; } private void MoveToward(Vector3 t) { Vector3 velocity = GetVelocity(origin, target, 0.55f); transform.localPosition = new Vector3(transform.localPosition.x + velocity.x, transform.localPosition.y + velocity.y); } private Vector3 GetVelocity(Vector3 from, Vector3 to, float power) { float distance = to.x - from.x; //Ultimately, we want the y-velocity to start higher and become more negative. //I have cheated that by having it multiply the y-offset by a percentage of completion. float distance_now = to.x - transform.position.x; float percent_done = distance_now / distance; float y_offset = percent_done/2 - 0.29f; //This makes it end up a bit further down (on the Y axis) than it starts. Use - 0.25f for a level shot. float angle_to_point = Mathf.Atan2(to.y, to.x - from.x); float distance_factor = 1f / 5f; float angle_correction = (3.14159f * 0.18f) * (distance * distance_factor); float velocity_x = Mathf.Cos(angle_to_point + angle_correction) * power; float velocity_y = Mathf.Sin(angle_to_point + angle_correction) * power * y_offset; return new Vector3(velocity_x, velocity_y); }

When it hits the target, I can trigger whatever I like in my “AtTarget()” method.

Does this look absurd and bad? Probably. Unscaleable? Maybe. But I hope it’s more light-weight in terms of performance (compared to doing a lot of square roots). If you have any feedback, I’d love to hear it below!

## Disclaimer

This is almost certainly a BAD solution if you care about physics. If you are creating an Angry Birds style ballistics puzzler, don’t use it. I also don’t totally understand the code snippet I included from Stack Overflow. But I needed something that was quick to implement and looked okay, and it seems to work mostly. Since this isn’t a major part of my game, I feel like a hacky approach (at least for now) is fine.

## TL;DR:

I should learn math better, but it doesn’t matter much for this feature. If I have to refactor and improve my code, I’ll probably revisit this topic. However, this works okay for now! Use at your own risk.

## Update June 26, 2017: PEOPLE ARE PROJECTILES TOO

So I needed a visual jumping mechanic for a skill and thought… well… maybe I could just use that projectile script:

Obviously I’ll need a better animation for the hero, but this looks hilarious.