Lock rotation in C++ not reflecting in game

I noticed that if I make changes to the rotation constraints in the c++ code and then try and test those changes by playing the game, the constraints on the Details tab for the affected object look correct but the object is still rotating.

Please see the following video to better understand the issue: https://youtu.be/2mfLelTaOfY

Here is the header section for the affected variables:

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Liftables")
bool bIsHoldingObject;

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Liftables")
class AActor* HeldObjectRef;

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Liftables")
class UStaticMeshComponent* HeldObjectStaticMesh;

Here is the code in the cpp:

void AFP_MainPlayer::PickUpObject()
{
	if (!bIsHoldingObject)
	{
		if (TracedObject)
		{
			bIsHoldingObject = true;
			HeldObjectRef = TracedObject;
			HeldObjectRef->SetActorRotation(FRotator::ZeroRotator);
			HeldObjectStaticMesh = Cast<UStaticMeshComponent>(HeldObjectRef->GetComponentByClass(UStaticMeshComponent::StaticClass()));
			HeldObjectStaticMesh->SetEnableGravity(false);

			HeldObjectStaticMesh->BodyInstance.bLockXRotation = true;
			HeldObjectStaticMesh->BodyInstance.bLockYRotation = true;
			HeldObjectStaticMesh->BodyInstance.bLockZRotation = true;
		}
	}
	else
	{
		bIsHoldingObject = false;
		HeldObjectStaticMesh->SetEnableGravity(true);

		HeldObjectStaticMesh->BodyInstance.bLockXRotation = false;
		HeldObjectStaticMesh->BodyInstance.bLockYRotation = false;
		HeldObjectStaticMesh->BodyInstance.bLockZRotation = false;

		HeldObjectRef = nullptr;
	}
}

Did you ever solve this? I am getting the same result.

Based on the answer at: https://answers.unrealengine.com/questions/379860/how-to-set-lock-position-and-lock-location-on-comp.html

HeldObjectStaticMesh->BodyInstance.bLockXRotation = true;
HeldObjectStaticMesh->BodyInstance.bLockYRotation = true;
HeldObjectStaticMesh->BodyInstance.bLockZRotation = true;
HeldObjectStaticMesh->BodyInstance.SetDOFLock(EDOFMode::SixDOF);

Adding that last line should do the trick.

2 Likes