Download

Actor Rotation via Line Trace

So, I have a vehicle pawn that is driving on a road, and when the side of the road change in elevation or angle I want the actors roll to match the angle of the ground. So I created a line trace pointing straight to the ground that gets the impact normal and translates its angle to the actors. It seems to work for the most part, however sometimes after the game has been driving for awhile the actors rotation begins to lock and then stops moving altogether. Here is the code I am using to make the rotation. When I comment it out my game runs fine with no issues but when I re-add it the player will rotate correctly for the majority of the time but then start to freeze up towards the end. This is all happening in the component tick.



if(Hit)
	{	
		FVector ImpactNormal = HitResult.ImpactNormal;
		//FRotator RotZ = UKismetMathLibrary::MakeRotFromZ(ImpactNormal);
		FRotator RotZ = FRotationMatrix::MakeFromZ(ImpactNormal).Rotator();

		FRotator NewRotation = FRotator(ActorRotation.Pitch, ActorRotation.Yaw, -RotZ.Roll);
		auto Interp = FMath::RInterpTo(ActorRotation, NewRotation, DeltaTime, 0.0000000001);
		GetOwner()->SetActorRotation(Interp);
		
		HasLanded = true;
		LastLanded = Time;
	}


EDIT:
I believe I am getting a Gimbal Lock here, I am not sure how to go about fixing this? I have a SpringArm component, do I need pass it into this function and adjust it or use Quaternion rotations instead.