UE学习记录part4

此为udemy课程

Unreal Engine 5 C++ The Ultimate Game Developer Course的第七节 the pawn class的学习笔记


66、Capsule Component

---------Birds.h-----------
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Components/CapsuleComponent.h"
#include "Bird.generated.h"

// Bird.generated.h得是最后一个头文件
UCLASS()
class MYPROJECT_API ABird : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	ABird();

	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

private:
	UPROPERTY(VisibleAnywhere)
	UCapsuleComponent* Capsule;
};
--------------Birds.cpp-------------
#include "Pawns/Bird.h"

// Sets default values
ABird::ABird()
{
 	// 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;

	Capsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule"));
	Capsule->SetCapsuleHalfHeight(20.f);
	Capsule->SetCapsuleRadius(15.f);
	SetRootComponent(Capsule);
}

// Called when the game starts or when spawned
void ABird::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ABird::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void ABird::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

}

67、forward declaration

----------Birds.h----------
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Bird.generated.h"
class UCapsuleComponent;
UCLASS()
class MYPROJECT_API ABird : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	ABird();

	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

private:
	//从这里开始就可以不用添加forward declaration而直接声明了
	UPROPERTY(VisibleAnywhere)
	UCapsuleComponent* Capsule;

----------------Birds.cpp--------------
#include "Pawns/Bird.h"
#include "Components/CapsuleComponent.h"

// Sets default values
ABird::ABird()
{
 	// 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;

	Capsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule"));
	Capsule->SetCapsuleHalfHeight(20.f);
	Capsule->SetCapsuleRadius(15.f);
	SetRootComponent(Capsule);
}

68、skeletal mesh component

-----------Birds.h------
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Bird.generated.h"
class UCapsuleComponent;
class USkeletalMeshComponent;

UCLASS()
class MYPROJECT_API ABird : public APawn
{
	GENERATED_BODY()
...
private:
	//从这里开始就可以不用添加forward declaration而直接声明了
	UPROPERTY(VisibleAnywhere)
	UCapsuleComponent* Capsule;

	UPROPERTY(VisibleAnywhere)
	USkeletalMeshComponent* BirdMesh;
};
----------Birds.cpp-------
#include "Pawns/Bird.h"
#include "Components/CapsuleComponent.h"
#include "Components/SkeletalMeshComponent.h"

// Sets default values
ABird::ABird()
{
 	// 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;

	Capsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule"));
	Capsule->SetCapsuleHalfHeight(20.f);
	Capsule->SetCapsuleRadius(15.f);
	SetRootComponent(Capsule);

	BirdMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("BirdMesh"));
	BirdMesh->SetupAttachment(GetRootComponent());
}

70、binding input(旧版)

shift + f1 + detach 按钮,可以看到默认拥有的pawn

 通过auto possess player设置,可以设置默认pawn

--------Birds.h----------
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	void MoveForward(float Value);

--------Birds.cpp---------
void ABird::MoveForward(float Value)
{
	UE_LOG(LogTemp, Warning, TEXT("Value: %f"), Value);
}


// Called to bind functionality to input
void ABird::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	PlayerInputComponent->BindAxis(FName("MoveForward"), this, &ABird::MoveForward);
}


72、Adding Movement Input

给pawn添加movement组件,可以控制速度等

设置w\s相反方向

-----------Birds.cpp----------
void ABird::MoveForward(float Value)
{
	if (Controller && (Value != 0.f))
	{
		FVector Forward = GetActorForwardVector();
		AddMovementInput(Forward, Value);
	}
}


73、camera  and springarm

在蓝图中添加相机与弹簧臂。之所以需要通过一个弹簧臂来进行相机的调节而不是将相机直接attach在根物体上,是因为弹簧臂碰到阻碍物时会自动调节距离,这样使得相机不会穿墙显示。

通过c++添加相机和弹簧臂(先在蓝图中删除相机和弹簧臂) 

-----------Birds.h------------

class UCapsuleComponent;
class USkeletalMeshComponent;
class USpringArmComponent;
class UCameraComponent;

UCLASS()
class MYPROJECT_API ABird : public APawn
{
	
private:
	//从这里开始就可以不用添加forward declaration而直接声明了
	UPROPERTY(VisibleAnywhere)
	UCapsuleComponent* Capsule;

	UPROPERTY(VisibleAnywhere)
	USkeletalMeshComponent* BirdMesh;

	UPROPERTY(VisibleAnywhere)
	USpringArmComponent* SpringArm;

	UPROPERTY(VisibleAnywhere)
	UCameraComponent* ViewCamera;
};

--------------Birds.cpp----------
ABird::ABird()
{
 	// 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;

	Capsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule"));
	Capsule->SetCapsuleHalfHeight(20.f);
	Capsule->SetCapsuleRadius(15.f);
	SetRootComponent(Capsule);

	BirdMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("BirdMesh"));
	BirdMesh->SetupAttachment(GetRootComponent());

	SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
	SpringArm->SetupAttachment(GetRootComponent());
	SpringArm->TargetArmLength = 300.f;

	ViewCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ViewCamera"));
	ViewCamera->SetupAttachment(SpringArm);

	AutoPossessPlayer = EAutoReceiveInput::Player0;

如果遇到detail面板为空的情况,如下图

解决方法:重命名该变量名 。


75、Adding Controller Input

控制鸟的左右旋转与视角的上下调节

在蓝图中:

c++:

设置 碰撞

 

--Birds.h----------
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	void MoveForward(float Value);
	void Turn(float Value);
	void LookUp(float Value);
-------------Birds.cpp-------------
void ABird::Turn(float Value)
{
	AddControllerYawInput(Value);
}

void ABird::LookUp(float Value)
{
	AddControllerPitchInput(Value);
}
void ABird::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	PlayerInputComponent->BindAxis(FName("MoveForward"), this, &ABird::MoveForward);
	PlayerInputComponent->BindAxis(FName("Turn"), this, &ABird::Turn);
	PlayerInputComponent->BindAxis(FName("LookUp"), this, &ABird::LookUp);
}

当我们在远离Bird的时候play,我们实际上并不会possess bird

而当我们自己创建一个pawn时,游戏中还出现了默认的pawn

这是因为使用了项目默认设置的gamemode,而默认设置的gamemode中就设置了自动生成的pawn



76、setting the default pawn

创建一个gamemode蓝图类

设置default pawn

 复写game mode设置

但这样还是没法将pawn的生成位置固定,这时候就可以用到player start。player start可以固定游戏开始时pawn生成的位置

 

有一个现象是,如果我们不设置gamemode overide,只是生成了一个BP_Bird并且设置为player0。在游戏开始时,我们可以操控bird,但当我们飞远了,游戏开始load and unload section时,我们会失去我们的bird并且视角卡住。因为bird随旧关卡一起被卸载了 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值