Hello, I am a university student living in Korea.
Since I’m a beginner in Unreal, I still don’t know a lot, so I asked.
I’ve finished making the x, y, and z data visible by logging the current location of the camera actor.
But after running the game, I want to get x, y, and z angle data in the cmd window and change the camera angle, but I don’t know Help me…
Certainly! To achieve what you want, you’ll need to use Unreal Engine’s C++ programming capabilities. You can create a custom actor class that represents your camera and then use C++ code to access and manipulate its rotation.
Here’s a step-by-step guide:
- Create a New C++ Class: Open your Unreal project in the Unreal Editor and go to the “File” menu. Choose “New C++ Class…” and select a class based on the actor (e.g.,
AActor
orACameraActor
). Follow the instructions to create the new class. - Modify the Header (.h) File: Open the generated header file (e.g.,
MyCameraActor.h
) in a text editor or your preferred IDE. Add a public function to get the rotation angles and update the rotation:
cppCopy code
// MyCameraActor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyCameraActor.generated.h"
UCLASS()
class YOURPROJECT_API AMyCameraActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyCameraActor();
// Function to get rotation angles
UFUNCTION(BlueprintPure, Category = "Camera")
FRotator GetCameraRotation() const;
// Function to set rotation angles
UFUNCTION(BlueprintCallable, Category = "Camera")
void SetCameraRotation(const FRotator& NewRotation);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
- Implement the Functions in the Source (.cpp) File: Open the corresponding source file (e.g.,
MyCameraActor.cpp
) and implement the functions:
cppCopy code
// MyCameraActor.cpp
#include "MyCameraActor.h"
// Sets default values
AMyCameraActor::AMyCameraActor()
{
// Set this actor to call Tick() every frame
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AMyCameraActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyCameraActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// Your update logic here
}
// Function to get rotation angles
FRotator AMyCameraActor::GetCameraRotation() const
{
return GetActorRotation();
}
// Function to set rotation angles
void AMyCameraActor::SetCameraRotation(const FRotator& NewRotation)
{
SetActorRotation(NewRotation);
}
- Compile the Code: Save the files and then recompile your project. If you encounter any errors, follow the instructions provided by the compiler to fix them.
- Place Your Custom Camera Actor in the Level: Open the level where you want to use your custom camera actor. Drag and drop an instance of your custom camera actor into the level.
- Access Camera Rotation in Blueprint: In your Blueprint, you can now use the functions you created. To access the rotation, you can call the
GetCameraRotation
function and use theSetCameraRotation
function to change the rotation. - Print Rotation in CMD Window (Console): If you want to print the rotation to the console during runtime, you can use the
UE_LOG
macro. For example, add the following line inside theGetCameraRotation
function:
cppCopy code
UE_LOG(LogTemp, Warning, TEXT("Camera Rotation: %s"), *GetActorRotation().ToString());
This will print the camera rotation in the console.
Now, when you play the game, you should be able to see the camera rotation in the console window. Make sure to replace “YOURPROJECT” with the actual name of your project.
Thank you for your response!
I rewrote it the way I wanted to refer to the code.
// MyCameraActor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Camera/CameraComponent.h"
#include "MyCameraActor.generated.h"
UCLASS()
class FSIM_API AMyCameraActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyCameraActor();
// Called every frame
virtual void Tick(float DeltaTime) override;
// Function to get rotation angles
UFUNCTION(BlueprintPure, Category = "Camera")
FRotator GetCameraRotation() const;
// Function to apply rotation input from the user
UFUNCTION(BlueprintCallable, Category = "Camera")
void ApplyCameraRotation(float DeltaRoll, float DeltaPitch, float DeltaYaw, float DeltaPan, float DeltaTilt);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
// Camera component
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera", meta = (AllowPrivateAccess = "true"))
UCameraComponent* CameraComponent; // CameraComponent을 클래스의 멤버로 선언
};
// MyCameraActor.cpp
#include "MyCameraActor.h"
#include "Camera/CameraComponent.h"
// Sets default values
AMyCameraActor::AMyCameraActor()
{
// Set this actor to call Tick() every frame
PrimaryActorTick.bCanEverTick = true;
// Create camera component
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
RootComponent = CameraComponent;
}
// Called when the game starts or when spawned
void AMyCameraActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyCameraActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Function to get rotation angles
FRotator AMyCameraActor::GetCameraRotation() const
{
return CameraComponent->GetComponentRotation();
}
// Function to apply rotation input from the user
void AMyCameraActor::ApplyCameraRotation(float DeltaRoll, float DeltaPitch, float DeltaYaw, float DeltaPan, float DeltaTilt)
{
FRotator CurrentRotation = CameraComponent->GetComponentRotation();
// Calculate new rotation based on input values
FRotator NewRotation = FRotator(
CurrentRotation.Roll + DeltaRoll,
CurrentRotation.Pitch + DeltaPitch,
CurrentRotation.Yaw + DeltaYaw
);
// Apply additional pan and tilt rotation
NewRotation.Add(0.0f, DeltaPan, DeltaTilt);
// Set the new rotation for the camera
CameraComponent->SetWorldRotation(NewRotation);
}
But what I want is to angle the camera by taking the roll, pitch, and yaw values from the user in the Unreal Engine console window.
Do I have to make a separate UI and deliver it to the user? Can’t I get a variable value input through the cmd window right away?