【问题标题】:Movable object wont rotate in Unreal可移动对象不会在虚幻中旋转
【发布时间】:2018-05-04 00:42:55
【问题描述】:

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

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

谁能帮我看看我做错了什么,因为我是虚幻引擎的新手。

【问题讨论】:

  • 请提供更多细节和上下文。包括你尝试过的。

标签: c++ unreal-engine4


【解决方案1】:

首先,检查您的 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多