可移动物体不会在虚幻中旋转

Movable object wont rotate in Unreal

我正在使用以下代码旋转对象并将其设置为在编辑器中可移动。

void URotateMe::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
    Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

    FRotator rot = Owner->GetTransform().Rotator();

    UE_LOG(LogTemp, Warning, TEXT("rotation before : %s"), *rot.ToString());

    Owner->GetTransform().SetRotation(FQuat(rot.Add(0, 20, 0)));

    rot = Owner->GetTransform().Rotator();

    UE_LOG(LogTemp, Warning, TEXT("rotation after : %s"), *rot.ToString());
}

谁能帮我看看我做错了什么,因为我是 Unreal Engine 的新手。

首先,检查您的 BeginPlay() 方法中是否有

AActor* Owner = GetOwner();

否则UE无法识别Owner中的指针 FRotator rot = Owner->GetTransform().Rotator();

此外,对于 Owner->GetTransform().SetRotation(FQuat(rot.Add(0, 20, 0)));,我建议初学者远离 FQuat,因为四元数引用可能有点奇怪。尝试使用 SetActorRotation() 方法(顺便说一下,它有多个签名,包括 FQuat() 和 Frotator()),这样你就可以做这样的事情:

Owner->GetTransform().SetActorRotation(FRotator(0.0f, 20.0f, 0.0f));   // notice I am using SetActorRotation(), NOT SetRotation()

或者更好的是,这就是您的代码在 BeginPlay():

中的样子
AActor* Owner = GetOwner();
FRotator CurrentRotation = FRotator(0.0f, 0.0f, 0.0f)
Owner->SetActorRotation(CurrentRotation);

然后是您的 TickComponent():

CurrentRotation += FRotator(0.0, 20.0f, 0.0f)  
Owner->SetActorRotation(CurrentRotation);      // rotate the Actor by 20 degrees on its y-axis every single frame