c++unreal-engine4unreal-engine5

SetControlRotation doesn't work for some reason with Unreal engine


My code:

APlayerController* controller = UGameplayStatics::GetPlayerController(this->GetWorld(), 0);
FRotator Rotation = Controller->GetControlRotation();
Controller->SetControlRotation(Rotation.Pitch + amount*5, Rotation.Yaw, Rotation.Roll);

The error I get:

error C2660, function doesnt accept 3 arguments

What is the reason for that ?


Solution

  • The problem:

    SetControlRotation API requires 1 argument of type FRotator, not 3 separate ones for the rotation components (pitch, yaw, roll).

    This is the meaning of the error you get (error C2660, function doesnt accept 3 arguments).

    You can fix it in one of the following ways:

    1. Add a FRotator variable created from the 3 components and use it with the API:
      FRotator rot = FRotator(Rotation.Pitch + amount*5, 
                              Rotation.Yaw, 
                              Rotation.Roll);
      Controller->SetControlRotation(rot);
      

    or:

    1. Construct a FRotator object from the 3 components directly in the API call, like this:
      Controller->SetControlRotation(
      //--------------------vvvvvvvv--------------------------
                            FRotator(Rotation.Pitch + amount*5, 
                                     Rotation.Yaw, 
                                     Rotation.Roll));
      

    Note that although FRotator is composed of the 3 components, it is not the same (from the C++ point of view) as the 3 components given separatly.