Tank gun stabilization

I recently calculated hip tilt angle for standing on uneven terrain as part of my IK setup, I believe its a bit similar, so I will try to explain the simple trigonometry which I used, it works perfectly, but maybe there is a simpler way which I don’t know of.
Here it is in BP, (also sync required variable between character BP and animation BP if not doing trace in animation BP.)

EDIT: You don’t need to convert anything into vector for anim graph as in screenshot, only to rotator.