Ninja Character - Dynamic gravity for characters & objects

Hey all, thought I’d share my Camera Smoothing function for free:

Slope:

Sphere World:

Code:

void ANinjaCharacter::AdjustControlRotationToActor(float DeltaTime)
{
	if(Controller)
	{
		// Get the current control rotation
		FRotator CurrentControlRotator = Controller->GetControlRotation();

		// Convert world-space control rotation to local-space control rotation relative to characters rotation
		FQuat WorldToCharacterRotation = GetActorRotation().Quaternion().Inverse();
		FQuat LocalControlRotation = WorldToCharacterRotation * CurrentControlRotator.Quaternion();

		// Perform the pitch and yaw adjustments in this local space
		// We create a desired local control rotation with the same pitch and yaw but zero roll
		FRotator DesiredLocalControlRotator(LocalControlRotation.Rotator().Pitch, LocalControlRotation.Rotator().Yaw, 0);
		FQuat DesiredLocalControlRotation = DesiredLocalControlRotator.Quaternion();

		// Interpolate from the current to the desired local control rotation
		FQuat NewLocalControlRotation = FQuat::Slerp(LocalControlRotation, DesiredLocalControlRotation, DeltaTime * CameraInterpSpeed);

		// Convert this local-space control rotation back to world-space control rotation
		FQuat NewWorldControlRotation = GetActorRotation().Quaternion() * NewLocalControlRotation;

		// And finally apply the new world-space rotation to the controller
		Controller->SetControlRotation(NewWorldControlRotation.Rotator());
	}	
}

There are a few caveats:

This requires you turn Off “Capsule Rotates Control Rotation”.

This function also runs on tick, but it’s pretty minimal. There may be a cleaner method with less instructions but this feels good, smooth, and responsive imo.

The camera roll is pretty much the only thing adjusted with direction change, the pitch and yaw remain the same. This is ideal for me when developing a shooter, so that my aim is affected as little as possible.

(The project here uses Halo assets as a placeholder)

No credit or payment required to use this

1 Like