C++ 可移动的物体在虚幻中不会旋转
我使用以下代码在编辑器中旋转一个对象并将其设置为可移动C++ 可移动的物体在虚幻中不会旋转,c++,unreal-engine4,C++,Unreal Engine4,我使用以下代码在编辑器中旋转一个对象并将其设置为可移动 void URotateMe::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); FRotator rot = Owner->GetTransfo
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());
}
有人能帮我看看我做错了什么吗,因为我不熟悉虚幻引擎。首先,在BeginPlay()方法中检查
AActor* Owner = GetOwner();
否则,UE将无法识别中的所有者
指针
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);
CurrentRotation += FRotator(0.0, 20.0f, 0.0f)
Owner->SetActorRotation(CurrentRotation); // rotate the Actor by 20 degrees on its y-axis every single frame
然后是您的TickComponent():
AActor* Owner = GetOwner();
FRotator CurrentRotation = FRotator(0.0f, 0.0f, 0.0f)
Owner->SetActorRotation(CurrentRotation);
CurrentRotation += FRotator(0.0, 20.0f, 0.0f)
Owner->SetActorRotation(CurrentRotation); // rotate the Actor by 20 degrees on its y-axis every single frame
请提供更多详细信息和上下文。包括你尝试过的。