目录
原视频地址:
【虚幻5】UE5C++零基础全网全流程开发从入门到进阶教程合集(持续跟新中)_哔哩哔哩_bilibili
摄像机
在pawn中加入头文
#include "GameFrameword/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
并在pawn的头文件中声明组件属性
#include "Components/SceneComponent.h"
#include "GameFramework/SpringArmComponent.h"
添加组件
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
USceneComponent* MyRoot;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
USpringArmComponent* MyArm;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
UCameraComponent* MyCamera;
在pawn的构造函数中初始化组件
//在构造函数中初始化组件
MyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("MyRoot"));
MyArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MyArm"));
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
设置组件绑定关系,和空间位置
//组件之间的绑定关系
RootComponent = MyRoot;
MyArm->SetupAttachment(MyRoot);
MyCamera->SetupAttachment(MyArm);
//设置组件之间空间位置关系
FVector locatoin = FVector(0, 0, 0);
FRotator MyRotation = FRotator(-50,0,0);
FVector MyScale = FVector(1, 1, 1);
SetActorLocation(locatoin);
SetActorRotation(MyRotation);
SetActorScale3D(MyScale);
利用按键映射实现摄像机的缩放
重载MyPlayerController中SetupInputComponent方法
//定义
virtual void SetupInputComponent()override;
//实现
void AMyPlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
InputComponent->BindAction("WheelUp",IE_Pressed,this, &AMyPlayerController::WheelUpFunction);
InputComponent->BindAction("WheelDown", IE_Pressed, this, &AMyPlayerController::WheelDownFunction);
}
在unreal引擎中绑定按键到WheelUP 和 WheelDown上
实现我们绑定的WheelUpFunction和WheelDownFunction方法,用来当输入响应后所发生的事件
//MyPlayerController中的定义和实现
void WheelUpFunction();
void WheelDownFunction();
void AMyPlayerController::WheelUpFunction()
{
if (GetPawn()) {
AMyPawn* Mypawn = Cast<AMyPawn>(GetPawn());
if (Mypawn) {
Mypawn->Zoom(-1,20);
}
}
}
void AMyPlayerController::WheelDownFunction()
{
if (GetPawn()) {
AMyPawn* Mypawn = Cast<AMyPawn>(GetPawn());
if (Mypawn) {
Mypawn->Zoom(1, 20);
}
}
}
在我们pawn中实现Zoom方法,用来操控我们pawn的相机臂
//定义
UFUNCTION()
void Zoom(int direction,float speed);
//实现
void AMyPawn::Zoom(int direction, float speed)
{
MyArm->TargetArmLength += (float)direction * speed;
if (MyArm->TargetArmLength < 200)MyArm->TargetArmLength = 200;
if (MyArm->TargetArmLength > 1000)MyArm->TargetArmLength = 1000;
}
完整代码
//AMyPawn.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/SceneComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"
UCLASS()
class PROJECT001_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
USceneComponent* MyRoot;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
USpringArmComponent* MyArm;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
UCameraComponent* MyCamera;
UFUNCTION()
void Zoom(int direction,float speed);
};
//MyPawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
// Sets default values
AMyPawn::AMyPawn()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//在构造函数中初始化组件
MyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("MyRoot"));
MyArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MyArm"));
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
MyArm->bDoCollisionTest = false;//关闭相机臂的物理碰撞
//组件之间的绑定关系
RootComponent = MyRoot;
MyArm->SetupAttachment(MyRoot);
MyCamera->SetupAttachment(MyArm);
}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
//设置组件之间空间位置关系
MyArm->TargetArmLength = 200;//初始化摄像机臂的长度
FVector locatoin = FVector(0, 0, 0);
FRotator MyRotation = FRotator(-50, 0, 0);
FVector MyScale = FVector(1, 1, 1);
SetActorLocation(locatoin);
SetActorRotation(MyRotation);
SetActorScale3D(MyScale);
}
// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
void AMyPawn::Zoom(int direction, float speed)
{
MyArm->TargetArmLength += (float)direction * speed;
if (MyArm->TargetArmLength < 200)MyArm->TargetArmLength = 200;
if (MyArm->TargetArmLength > 1000)MyArm->TargetArmLength = 1000;
}
//MyPlayerController.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "MyPawn.h"
#include "Blueprint/UserWidget.h"
#include "Components/StaticMeshComponent.h"
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "MyPlayerController.generated.h"
/**
*
*/
UCLASS()
class PROJECT001_API AMyPlayerController : public APlayerController
{
GENERATED_BODY()
public:
virtual void BeginPlay() override;
virtual void SetupInputComponent()override;
void WheelUpFunction();
void WheelDownFunction();
};
//MyPlayerController.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPlayerController.h"
void AMyPlayerController::BeginPlay()
{
//Super::BeginPlay();
//GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Red, TEXT("MyPlayerController BeginPlay!"));
//UClass *MyUserWidgetClass = LoadClass<UUserWidget>(NULL,TEXT("/Script/UMGEditor.WidgetBlueprint'/Game/BP_UserWidget.BP_UserWidget_C'"));
//UUserWidget* MyUserWidget = nullptr;
//MyUserWidget = CreateWidget<UUserWidget>(GetWorld(), MyUserWidgetClass);
//MyUserWidget->AddToViewport();
}
void AMyPlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
InputComponent->BindAction("WheelUp",IE_Pressed,this, &AMyPlayerController::WheelUpFunction);
InputComponent->BindAction("WheelDown", IE_Pressed, this, &AMyPlayerController::WheelDownFunction);
}
void AMyPlayerController::WheelUpFunction()
{
if (GetPawn()) {
AMyPawn* Mypawn = Cast<AMyPawn>(GetPawn());
if (Mypawn) {
Mypawn->Zoom(-1,20);
}
}
}
void AMyPlayerController::WheelDownFunction()
{
if (GetPawn()) {
AMyPawn* Mypawn = Cast<AMyPawn>(GetPawn());
if (Mypawn) {
Mypawn->Zoom(1, 20);
}
}
}