Why does my rotation always glitch out?

So I’m trying to rotate an object by taking it’s current rotation and adding a rotational constant. So it would look something like this.

GetActorRotation().Pitch + rotConstant

I tried to set this using FRotator but that causes gimbal lock to occur. So I tried using FQuat. In my set actor rotation I have this.

SetActorRotation(FQuat(0.0f, GetActorRotation().Pitch + rotConstant, 0.0f, 1.0f));

This does not work at all. Not only does my pitch go crazy when I do this but I even tried just setting the Yaw value to 1.0 and it set it to 90 in game. Why is it doing that and how can I make a stable rotation?

A quaternion rotation is defined differently from Pitch, Roll, Yaw. The real part of the quaternion is the rotation and the imaginary (i, j, k) part is a vector representing the axis of rotation.

To apply an arbitrary rotation Q (about a different axis) to an existing quaternion P (or a 3-vector represented as a quaternion with rotation zero, ie (0, x, y, z)), an operation called conjugation is used. Note that quaternion multiplication is not commutative, so usually PQ =/= QP. With Q^-1 representing the inverse of Q and P’ being the desired result:

P’ = QPQ^-1

I believe that it is best to make a function to apply this rotation; there may be one already in the math quaternion library.

Have a look at how the Blueprints handle adding 2 Rotators:

FRotator UKismetMathLibrary::ComposeRotators(FRotator A, FRotator B)
{
	FQuat AQuat = FQuat(A);
	FQuat BQuat = FQuat(B);

	return FRotator(BQuat*AQuat);
}

Applying it to your case:

FRotator DeltaRotation = FRotator(rotConstant, 0, 0);

FQuat A = GetActorRotation().Quaternion();
FQuat B = DeltaRotation.Quaternion();

SetActorRotation(B * A);

Or to make it shorter:

SetActorRotation(FRotator(rotConstant, 0, 0).Quaternion() *GetActorRotation().Quaternion());