Posted December 09, 2023 by NotArcane
Author: Jonah McConnell
While working on making the player's third-person camera, I encountered a series of bugs that, while being largely funny to look at, ate away at my time devoted to working more than I would have liked. While trying to change the direction the player model was facing, I repeatedly ended up with the model simply rapidly spinning, often to the point of blurring due to Unreal's renderer in the editor. Aside from the distracting visuals, this meant that the player would spin in place rather than walk forward. No combinations of inputs allowed the player to move in any meaningful way.
The fix was ultimately to continue digging into the best way to rotate the player character to face the given direction correctly. In the end, the solution proved to account for the difference between the camera's angle, which was being used to set the player-facing direction at first, and the player's own orientation. Without this, the player would turn towards the direction of the camera, only for that direction to rotate with it relatively, causing it to rotate further. Adding in some simple vector math in a couple of locations helped to solve this issue.
The trickier issue came in the form of easing this rotation. While snapping to the new rotation technically functioned, it did not function very well, as it looked incredibly unnatural. While not necessarily an issue outright, it brought up concerns in my mind for any effects dependent on player direction, especially at further distances, suddenly teleporting about the player because their rotation snapped to a new value.
To fix this, I linearly interpolated between the current angle and the desired angle for the player's Yaw by a fixed value which I exposed to Blueprint to be changed in the editor. This was a great fix for the majority of the time, though some fiddling to find the correct value was necessary. However, the edge case scenario was once again greatly distracting from the events in-game. Any time the player rotated across the 360-degree mark in either direction with their Yaw rotation, they would suddenly spin in the opposite direction, taking the longer way to rotate to their new point. This however quickly was solved by manipulating the degree values to ensure that they would always interpolate in the direction the player was already spinning to avoid any sudden rotations in the other direction.