This week I created a system that moves a targeting reticle across a 2D plane that marks where a projectile will land. To do this I had to a few things. Firstly I needed to create objects for a cannon and a reticle, the cannon can move and the reticle moves based on the cannons position. Then it needed a script, I created the reticle movement script to in such a way that it could be placed on the reticle rather than the cannon. In the update this script it calculates the range of the shot(and so the distance the reticle should be from the cannon). This is done with a projectile motion equation that you can find on this page. Then, by setting the reticles x position to this range I could move the reticle. But this would not make the reticle be where the cannon pointed. So to do this i gave the reticle a parent. This parent would be referenced in the code. With the code I made the parent have the same y rotation as the cannon. This then made the reticle point where the cannon pointed and gave a depiction of the cannons range on the ground.
Angles within unity are extremely frustrating. I spent hours attempting to get the range to be suited to the angle the actually existed. For a very long time the range would change depending on all three angles rather than just its x angle. This resulted in the reticle reducing in range as the cannon rotated on its y axis away from the origin. I discovered after these hours of frustration that I needed euler angles to get the range to work correctly and had an error in my range calculation.
Euler angles are frustrating
But that wasn’t my only issue. I also could not get the reticle parent to rotate the way I wanted it to. I have no idea why. The portion of code that rotated the parent would always lock the parent to 180 degrees on the Y. For some reason, setting the rotation to a quaternion.euler in the one line fixed this issue. Like magic.
But the code works now, and it feels right. Errors have been resolved and much hair has been pulled. Hopefully I will learn from this frustrating experience when attempting to work with rotation in unity next time.