虚幻四C++入坑指南08:C++实现FPS游戏(2)
虚幻引擎4文档 > 编程指南 > C++ 编程教程 > 第一人称射击游戏教程 > 2 - 实现角色
01 FPSCharacter
01 01 新建Character类型的C++类“FPSCharacter”
我沿用了QuickStart工程,因为新建工程是GameMode,不符合,也不会调,所以沿用有GameModeBase的QuickStart(之前的工程)
01 02 .cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "FPSCharacter.h"
// Sets default values
AFPSCharacter::AFPSCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AFPSCharacter::BeginPlay()
{
Super::BeginPlay();
if (GEngine)
{
// 显示调试信息五秒。-1"键"值(首个参数)说明我们无需更新或刷新此消息。
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));
}
}
// Called every frame
void AFPSCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
01 03 生成BP
01 04 验证演示
01 05 将 BP_FPSCharacter 用作默认 Pawn,我的Pawn灰色不可选
那就手动Player0
01 06 轴映射
Action是攻击、跳跃类,点按;Axis是移动类,长按
01 07 FPSCharacter.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "FPSCharacter.generated.h"
UCLASS()
class QUICKSTART_API AFPSCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AFPSCharacter();
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;
// 处理前进和后退的输入。
UFUNCTION()
void MoveForward(float Value);
// 处理左右移动的输入。
UFUNCTION()
void MoveRight(float Value);
};
01 08 FPSCharacter.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "FPSCharacter.h"
#include "Engine/Engine.h"
// Sets default values
AFPSCharacter::AFPSCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AFPSCharacter::BeginPlay()
{
Super::BeginPlay();
if (GEngine)
{
// 显示调试信息五秒。-1"键"值(首个参数)说明我们无需更新或刷新此消息。
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));
}
}
// Called every frame
void AFPSCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// 设置"移动"绑定。
//PlayerInputComponent->BindAxis("通知", this, &AFPSCharacter::调用事件);
/*
//使用结构体内部的函数需要绑定函数,指明是呢一个类来调用的
template<class UserClass>模板结构体
FInputAxisBinding& BindAxis(const FName AxisName, UserClass * Object, typename FInputAxisHandlerSignature::TUObjectMethodDelegate< UserClass >::FMethodPtr Func)
{
FInputAxisBinding AB(AxisName);
AB.AxisDelegate.BindDelegate(Object, Func);
AxisBindings.Emplace(MoveTemp(AB));
return AxisBindings.Last();
}
*/
PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
}
void AFPSCharacter::MoveForward(float Value)
{
// 明确哪个方向是"前进",并记录玩家试图向此方向移动。
FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
AddMovementInput(Direction, Value);
}
void AFPSCharacter::MoveRight(float Value)
{
// 明确哪个方向是"向右",并记录玩家试图向此方向移动。
FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
AddMovementInput(Direction, Value);
}