可移动对象不会在虚幻中旋转

时间:2017-11-20 17:44:46

标签: c++ unreal-engine4

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

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());
}

有人可以帮我看看我做错了什么,因为我不熟悉虚幻引擎。

1 个答案:

答案 0 :(得分:4)

首先,检查你的BeginPlay()方法,你有

AActor* Owner = GetOwner();

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

此外,对于Owner->GetTransform().SetRotation(FQuat(rot.Add(0, 20, 0)));我建议远离FQuat作为初学者,因为Quaternion引用可能有点奇怪。尝试使用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