How can I set Control Rotation in C++?

How can I set Control Rotation in C++? I already tried to set control rotation in c++ but I can find a function for it, I only can find one of get control rotation. Anyone can help me?
I want to this, but in c++:
image
Is it possible to make like this next one?
image

If you take a look at the Blueprint Node, it says “Target is Controller”, meaning to find the C++ part of that node you have to look in the AController class.

In there, you find the SetControlRotation function

//In Controller.h , class AController
	UFUNCTION(BlueprintCallable, Category=Pawn, meta=(Tooltip="Set the control rotation."))
	virtual void SetControlRotation(const FRotator& NewRotation);

which means in C++, you need an AController object (APlayerController is a child class of AController, so it also has the function) and can then call the function on it with the desired rotation. Translated to C++ your Blueprint code example would look like this

//BeginPlay in AMyActor class
void AMyActor::BeginPlay() 
{
  Super::BeginPlay(); // don't forget to call parent BeginPlay in C++, or else Unreal will complain

  //get player controller at index 0
  APlayerController* controller = UGameplayStatics::GetPlayerController(this->GetWorld(), 0)

  //Get this actors rotation
  FRotator rot = this->GetActorRotation();

  //Set control rotation of controller
  controller->SetControlRotation(rot);
}
2 Likes

If you want to see C++ implementation of some node, you can right click and select “Go to Definition”. It should open your IDE with .h file where function is defined. If IDE is not opened, look at the log, and you should find something like this:

LogSelectionDetails: Warning: NavigateToFunctionSource: Unable to find symbols for ‘AController::SetControlRotation’ [The specified module could not be found.]

2 Likes

If you just want to convert that BP to C++ you can do, make sure you include “Kismet/GameplayStatics.h”

UGameplayStatics::GetPlayerController(GetWorld(), 0)->SetControlRotation(GetActorRotation());

You can also do assuming you are in the character cpp
GetController()->SetControlRotation(GetActorRotation());

3 Likes