Blasters are such a staple to video games. Think back to space invaders, that game is almost completely just a simple blaster that moves side to side. That works great if you only want to shoot in a single direction, but what happens when you would like to aim in a full 360 degrees?
Adding the capability to aim in all directions seems simple in concept, but in reality it is remarkably complex, especially for something so common in today’s games. To get this blaster working, I found myself having to add two important parts to my code. One, I need to somehow track the mouses location, and two, I need to make the blaster aim in that direction. It turns out that the first is much more simple than the second.
Turns out that the “ViewportMouseEvent” Function keeps tabs on the location of the mouse at all times, simple, but effective for my needs. With step one complete, I dove in to part two, rotating my blaster to the direction of my mouse. So, in I bring everyone’s favorite math, Trig!
So, in short I had to calculate the angle difference, in radians, caused by the mouse and then rotate the blaster object by that many radians. Of course in words that seems simple, but in code it involves multiple levels of inter-connectivity in order to keep the code constantly updating the angles, and therefor creating a smooth blasting experience.