Hey, I have a Skeletal Mesh Component as the Root Component with physics disabled.
But when I enable it after a certain event, physics works just fine. But the rotation doesn’t updates. But the AddWorldRotation is getting called cause if I remove the TeleportType flag, then editor gives me warning. Its a one time function btw!
Here is the code that does work! It works when I add rotation before adding rotation
void AWeapon::ThrowWeapon()
{
GetItemMesh()->AddLocalRotation(FRotator(0, 90, 0), false, nullptr, ETeleportType::TeleportPhysics);
GetItemMesh()->SetSimulatePhysics(true);
FVector ImpulseDirection = GetActorForwardVector();
float RandomRotationZ{ FMath::RandRange(-45.f, 45.f) };
ImpulseDirection = ImpulseDirection.RotateAngleAxis(RandomRotationZ, FVector(0, 0, 1));
GetItemMesh()->AddImpulse(4'000 * ImpulseDirection);
}
Whereas the code below doesn’t works. Everything works same as above except rotation. Mesh doesn’t rotates even a single degree.
void AWeapon::ThrowWeapon()
{
GetItemMesh()->SetSimulatePhysics(true);
GetItemMesh()->AddLocalRotation(FRotator(0, 90, 0), false, nullptr, ETeleportType::TeleportPhysics);
FVector ImpulseDirection = GetActorForwardVector();
float RandomRotationZ{ FMath::RandRange(-45.f, 45.f) };
ImpulseDirection = ImpulseDirection.RotateAngleAxis(RandomRotationZ, FVector(0, 0, 1));
GetItemMesh()->AddImpulse(4'000 * ImpulseDirection);
Can someone help me please?
But if I set a Timer for the same line of code of adding rotation after setting Simulate Physics to on even of 0.0001 second (that is it will be called in the same frame), then it works again.
I mean this >>>
void AWeapon::RotateMesh()
{
GetItemMesh()->AddLocalRotation(FRotator(0, 90, 0), false, nullptr, ETeleportType::TeleportPhysics);
}
void AWeapon::ThrowWeapon()
{
GetItemMesh() ->SetSimulatePhysics(true);
FTimerHandle Handle;
GetWorldTimerManager().SetTimer(Handle, this, &AWeapon::RotateMesh, 0.0001f);
FVector ImpulseDirection = GetActorForwardVector();
float RandomRotationZ{ FMath::RandRange(-45.f, 45.f) };
ImpulseDirection = ImpulseDirection.RotateAngleAxis(RandomRotationZ, FVector(0, 0, 1));
GetItemMesh()->AddImpulse(4'000 * ImpulseDirection);
}
I don’t understand this behaviour and I’m stuck. Any help would greatly be appreciated.
And yes Add/Set Location/Rotation all work for Simulated Skeletal Mesh in BeginPlay/Tick.
Thanks,
Dean