Thank You, Trigonometry

Of all the gamedev things I’ve figured out so far, most have been specific to learning the tools or architecting All The Things™️ in an understandable way. But for the first time I actually had to use math. Like, good ol’ high school radians and angles type math.

The idea seemed simple, I was attempting to make some indicators for abilities. When you hold down the ability button, a targeting crosshair replaces your mouse cursor to let you know where the ability is headed. Then when you release the key, it reverts back to the normal cursor and activates the ability.

First I tackled the AOE ability which seemed simple enough. When triggered, the ability logs the location of the cursor and flings a meteor at it. I simply added a new animation clip to my cursor, set up a bool for turning it on and off, and badda-bing-badda-boom.

Next up, I wanted to have a directional arrow for my “dodge roll”. Because horses aren’t exactly known for gymnastic dexterity, I figured I’d take a pointer from Ziggs and explode our protagonist in the desired direction.

The flow of things is to hold the button down, see the arrow which follows the position of the cursor, release the button to activate which spawns an explosion object at the activation point and a shield object as a child of the player - both of which destroy enemy bullets, render you immune to damage for a brief moment, and deal damage to enemies on contact.

Knowing what to Google is 90% of the battle but everything I tried just wasn’t fruitful. I needed this arrow object to anchor itself on the player and follow the cursor position but every combination of “make an object point toward another object” returned examples of transform.LookAt which only works in 3d or Vector3.RotateTowards which was equally not helpful.

So I started thinking, what am I actually trying to do here? Essentially I’m placing this arrow as a child of the player so that it follows it around. Check ✅. Then I’m trying to update the Z axis rotation of that object in proportion to the cursor’s position. Seems easy enough right?

Well if this isn’t the most underpants gnomes thing I’ve ever written...

How do I take the direction (essentially the slope of a line between the two objects) and convert it into something useful? Again I took to Googling but apparently my word-smithing abilities were off that day. The results were a bunch of graphing and math related content that I couldn’t easily relate to my problem. I hopped back into Bolt and attempted, in futility, to make LootAt and RotateTowards work with this setup before giving up and looking into the Unity docs for some kind of inspiration.

I poured through all of the Mathf functions specifically searching for the keyword angle because I remembered just enough from trigonometry classes to believe this might be the correct route. Atan2 appeared to be the most applicable because the only information I had to work with were the x and y of the slope. I kind of hit the jackpot with this one.

It worked! … kind of. The arrow was hidden behind the horse and while it did rotate with the cursor, the direction still seemed off. I tried (for entirely too long) to work some magic in the script which would manually move the arrow’s position before remembering that you can set pivot points when importing images! Changing the arrow’s Pivot to “Bottom” from “Center” was all I needed to do. ✅

PIVOT!

Next up was fixing the angle offset. I wish I had a better explanation for this one, but honestly I just kind of eye-balled it as being about 90 degrees off and viola, it works! Here’s the final macro and corresponding functionality.

I almost busted out a yearbook to look up my high school trigonometry teacher’s name and give them a shout out. Thanks to Kenney for the crosshairs and always to Bolt for making it possible for a scrub like me to do cool stuff 🥳

Evan Lemmons

Husband, father, musician, gamer.