Reputation: 926
I am using following code to rotate an object and its set to movable in editor.
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());
}
Can someone help me see what I am doing wrong, since I am new to Unreal Engine.
Upvotes: 1
Views: 1589
Reputation: 905
First of all, check that in your BeginPlay() method, you have
AActor* Owner = GetOwner();
Otherwise UE won't recognize the Owner
pointer in
FRotator rot = Owner->GetTransform().Rotator();
Also, for Owner->GetTransform().SetRotation(FQuat(rot.Add(0, 20, 0)));
I suggest staying away from FQuat
as a beginner since Quaternion references can be a little weird. Try using the SetActorRotation()
method (which by the way has multiple signatures, including FQuat() and Frotator()) so you can do something like this instead:
Owner->GetTransform().SetActorRotation(FRotator(0.0f, 20.0f, 0.0f)); // notice I am using SetActorRotation(), NOT SetRotation()
Or even better, this is what your code should look like in your BeginPlay():
AActor* Owner = GetOwner();
FRotator CurrentRotation = FRotator(0.0f, 0.0f, 0.0f)
Owner->SetActorRotation(CurrentRotation);
and then your 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
Upvotes: 3