Reputation: 33
I'm making a 2d shooter in Ue4. I'm trying to rotate a sprite to face the mouse. The sprite will later be a hidden arrow that determines the directions the bullets go when they spawn. The sprite was added to help me visually debug. Since the game is 2d, the sprite should only need to rotate around the Y axis.
The sprite is not rotating correctly. As I move the mouse pointer around the edge of the game's window: rather than spin 360 in a circle, it seesaws back and forth (see second image).
I figured this was a radians or worldspace issue, but I've done the basic troubleshooting I know how. The first image is the cleanest version of the blueprint that produces the error. I've tried ATAN, ATAN2, and GetMousePosition solutions, all of which look a bit different than this.
In the sprite images, the red circle shows where my cursor is. The red arrow is just to help determine the angle the sprite is tilted at (they're all wrong). The top of the round bit (90 degrees CCW of the red arrow) is what should be facing the mouse. The sprite's default, unrotated position, has the round bit pointing up towards the top of the screen. Of note, the actual rotation and the desired rotation all seem to be off by a multiple of 90 degrees, but this might be a coincidence.
Any ideas on what I'm doing wrong?
Upvotes: 0
Views: 1654
Reputation: 33
I figured it out.
I had (incorrectly) assumed that the mouse's location on the screen was strictly XY (with a Z of 0). After popping open the location from the hit event, the mouse is calculated in XZ (with an almost unmoving Y).
I switched back to an ATAN2 solution (seen in the BP), but found that my sprite was facing away from the mouse instead of towards it. Adding a -1 multiplier fixed it, and it's now working as intended.
See BP for full solution. Cheers, all.
Upvotes: 0