Pawn
新建一个Pawn的c++类Bird,并且新建一个蓝图
添加一个Capsule(胶囊)
不知道要加啥头文件,去网站找https://docs.unrealengine.com/5.3/en-US/API/Runtime/Engine/Components/UCapsuleComponent/SetCapsuleSize/
包含文件名字的头文件必须放在最后
//文件
UPROPERTY(VisibleAnywhere)
UCapsuleComponent* Capsule;
//构造函数
Capsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule"));
SetRootComponent(Capsule);
不能让两个头文件互相引用,会产生错误,但是可以在对方的CPP里引用头文件。从父类继承时必须包含头文件
当你把头文件移到CPP的时候,你需要在第一个定义的时候加上Class
Skeletal Mesh Component骨骼网格体组件
为之前的Bird蓝图添加一个骨骼网格体,并且选择鸟的骨骼
BindInputs绑定输入
一个玩家的游戏,可以设置Player0来操控。shift+F1可以让鼠标和人物分离
绑定原理,将引擎里边写的前进和代码的绑定,当按下w时会给一个值1.0
添加绑定和代码编写,随后在bird蓝图里添加一个movement组件
添加相机臂和相机
导入头文件,定义相机臂和相机,编写代码
添加俯仰
新增轴映射,新建函数,绑定,编写,打开蓝图里的useYaw和Pitch
添为关卡添加默认pwan
新建一个GameMode的蓝图将里边的默认Pwan修改为BP_bird,并且将世界场景的gamemode覆盖为创建的
详细代码
头文件
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Components/CapsuleComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "Bird.generated.h"
UCLASS()
class ARPG_API ABird : public APawn
{
GENERATED_BODY()
public:
ABird();
virtual void Tick(float DeltaTime) override;
protected:
virtual void BeginPlay() override;
void MoveForward(float Value);
void Turn(float Value);
void LookUp(float Value);
public:
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UPROPERTY(VisibleAnywhere)
UCapsuleComponent* Capsule;
UPROPERTY(VisibleAnywhere)
USkeletalMeshComponent* BirdMesh;
private:
UPROPERTY(VisibleAnywhere)
class USpringArmComponent* CameraArm;
UPROPERTY(VisibleAnywhere)
class UCameraComponent* Camera;
};
CPP
// Fill out your copyright notice in the Description page of Project Settings.
#include "Bird.h"
// Sets default values
ABird::ABird()
{
PrimaryActorTick.bCanEverTick = true;
Capsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule"));
SetRootComponent(Capsule);
//创建骨骼网格体,并且附加在根组件下
BirdMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("BirdMesh"));
BirdMesh->SetupAttachment(GetRootComponent());
// 创建一个名为 "CameraArm" 的默认子对象,将 CameraArm 与根组件(RootComponent)关联
//设置相机臂的目标长度
CameraArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraArm"));
CameraArm->SetupAttachment(RootComponent);
CameraArm->TargetArmLength = 300.f;
//创建一个名为 "Camera" 的默认子对象,关联相机臂使相机位于 CameraArm 的末端
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->SetupAttachment(CameraArm);
//设置自动接收输入玩家0
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
// Called when the game starts or when spawned
void ABird::BeginPlay()
{
Super::BeginPlay();
}
void ABird::MoveForward(float Value)
{
if (Controller && (Value != 0.f))
{
FVector Forward = GetActorForwardVector();
AddMovementInput(Forward,Value);
}
}
void ABird::Turn(float Value)
{
AddControllerYawInput(Value);
}
void ABird::LookUp(float Value)
{
AddControllerPitchInput(Value);
}
// 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);
// 绑定“MoveForward”输入轴到 MoveForward 函数,以响应玩家输入
// 参数 FName("MoveForward") 指定了输入轴的名称
// this 指针将 ABird 类的当前实例与输入组件绑定
// &ABird::MoveForward 是处理输入的函数,它会在输入轴发生变化时被调用
PlayerInputComponent->BindAxis(FName("MoveForward"),this,&ABird::MoveForward);
PlayerInputComponent->BindAxis(FName("Turn"),this,&ABird::Turn);
PlayerInputComponent->BindAxis(FName("LookUp"),this,&ABird::LookUp);
}