Math in Unity : Cross Product (Part III)
Before proceeding, let’s try to understand why we created a bool and why it is not enough to simply calculate the Cross and use it.
The “problem” is basically this:
there is no way to change the formula for rotation to make it spin in the other direction using the same angle.
So we should use a little trick.
Instead of rotating the sprite clockwise, we will rotate it counterclockwise as if it were a circle.
And to do this calculation, just subtract “angle” from 360 °.
Now, we are working on RADIANS.
The formula for calculating 360° in radians is
2 * PI
So our calculation will not be 360 - angle,
but
2 * PI - angle
angle = 2 * PI — angle
And we will have to apply this formula in the Rotate() method
We check if the rotation is clockwise
And then we calculate the angle
If we test now
Now the Y axis, that is our FORWARD, is correctly rotated towards the enemy!
And it works perfectly even if we invert the positions
I hope it was interesting.
Again, I don’t think you need to have super in-depth knowledge of math to be able to program, personally I like it a lot, but the purpose of these articles is simply:
found the formula, understand how to apply it in your game(s).
In the next article we will see another very interesting thing, the LookAt()!