Having problems understanding FVector::Rotation()

So I have this code that makes my object “look” at the player.

FVector selfLocation = VisibleComponent->GetComponentLocation();
FVector playerLocation = (UGameplayStatics::GetPlayerCharacter(GetWorld(),0))->GetActorLocation();

FRotator deltaRotate = (selfLocation - playerLocation).Rotation();
VisibleComponent->SetWorldRotation(deltaRotate);

What I don’t understand is this line FRotator deltaRotate = (selfLocation - playerLocation).Rotation();. How is rotation calculated ? isnt FVector just a collection of 3 variables (X, Y, Z) ?

And why does this work ? I dont understand how can you get the rotation just by substracting the locations…

Hey leryss,

FVector::Rotation() converts a direction Vector into a Rotator.
selfLocation - playerLocation calculates the direction Vector from the player to self. After this the direction Vector will be converted to a Rotator.
That means the VisibleComponent always looks to the Player if it gets this Rotation.

If you need further Information go to

Greetings Cloudy :slight_smile:

thank you for the explanation :slight_smile: I just ran into the same question in my head and you solved it :wink: