Why does my actor lag when I'm setting it's world rotation?

Hello, I am in the process of making a scene component that when attached to an object will apply a force (gravity) in a specified direction and, if enabled, reorient the object so the bottom will rotate towards the direction of the appling force.

The gravity force part is done and works great however whenever I start setting the world rotation it starts to lag during simulation in the editor. If I set EnableGravityOrientation to false it works fine

PhysicsBody->AddForce(GravityDirection * 980.0f, NAME_None, true);

	if (EnableGravityOrientation == true && PhysicsBody != nullptr) {
		const FVector objectDown = -PhysicsBody->GetUpVector();

		const FQuat deltaQuat = FQuat::FindBetweenVectors(objectDown, GravityDirection);
		const FQuat targetQuat = deltaQuat * PhysicsBody->GetComponentQuat();

		PhysicsBody->SetWorldRotation(FQuat::Slerp(PhysicsBody->GetComponentQuat(), targetQuat, DeltaTime* 1.0f));

		GEngine->AddOnScreenDebugMessage(0, 0.5f, FColor::Yellow, deltaQuat.ToString());
	}

Here is a video showing the problem
https://streamable.com/7px0p

When you SetWorldRotation, also try setting the TeleportType to TeleportPhysics. You might be resetting your physics right now.

You could also try moving the rotation code to before the AddForce.

Also, what tick group is this in? Make sure its not DuringPhysics, PrePhysics would probably be best.

I wrote a custom gravity component that I use in BP, here’s a link.

Yea my money is on setting the tick group to PrePhysics.

So I updated it, I added PrimaryComponentTick.TickGroup = TG_PrePhysics; under the bCanEverTick and I made sure the SetWorldRotation was a TeleportPhysics but still it’s causing the jittering effect

if (PhysicsBody == nullptr) { return; }

	if (EnableGravityOrientation == true) {
		const FVector objectDown = -PhysicsBody->GetUpVector();

		const FQuat deltaQuat = FQuat::FindBetweenVectors(objectDown, GravityDirection);
		const FQuat targetQuat = deltaQuat * PhysicsBody->GetComponentQuat();

		PhysicsBody->SetWorldRotation(FQuat::Slerp(PhysicsBody->GetComponentQuat(), targetQuat, DeltaTime* 1.0f), false, nullptr, ETeleportType::TeleportPhysics);

		GEngine->AddOnScreenDebugMessage(0, 0.5f, FColor::Yellow, deltaQuat.ToString());
	}

	PhysicsBody->AddForce(GravityDirection * 980.0f, NAME_None, true);

you’re calling this on tick, right? not a timer function?

It seems like something about setting rotation is overriding the physics “sometimes”. but then its catching up. You could also try setting tick to post physics.

I’ve had odd problems like this before too, usually comes down to the tick group.

Instead of using set rotation, you could add torque proportional to the current angular velocity.