虚幻四C++入坑指南10:C++实现FPS游戏(4)设置人物模型

虚幻四C++入坑指南10:C++实现FPS游戏(4)设置人物模型
虚幻引擎4文档 > 编程指南 > C++ 编程教程 > 第一人称射击游戏教程 > 2 - 实现角色 > 2.6 - 为角色添加模型
虚幻引擎4文档 > 编程指南 > C++ 编程教程 > 第一人称射击游戏教程 > 2 - 实现角色 > 2.7 - 更改摄像机视图
虚幻引擎4文档 > 编程指南 > C++ 编程教程 > 第一人称射击游戏教程 > 2 - 实现角色 > 2.8 - 为角色添加第一人称模型

01 导入模型

01 01 下载模型

样板模型
在这里插入图片描述

01 02 解压模型

在这里插入图片描述

01 03 导入模型

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

02 角色添加模型

02 01 打开角色

在这里插入图片描述

02 02 添加模型

在这里插入图片描述

02 03 调整位置(胶囊体包住模型)

在这里插入图片描述

02 04 演示

在这里插入图片描述

03 加Camera调整视角

03 01 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 propertiesAFPSCharacter();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:	// Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;// 处理前进和后退的输入。UFUNCTION()void MoveForward(float Value);// 处理左右移动的输入。UFUNCTION()void MoveRight(float Value);// 按下按键时设置跳跃标记。UFUNCTION()void StartJump();// 松开按键时清除跳跃标记。UFUNCTION()void StopJump();// FPS 摄像机。UPROPERTY(VisibleAnywhere)UCameraComponent* FPSCameraComponent;
};

03 02 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;// 创建一个第一人称摄像机组件。FPSCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));// 将摄像机组件附加到胶囊体组件。FPSCameraComponent->SetupAttachment(GetCapsuleComponent());// 将摄像机放置在眼睛上方不远处。FPSCameraComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f + BaseEyeHeight));// 用 pawn 控制摄像机旋转。FPSCameraComponent->bUsePawnControlRotation = 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模板结构体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);// 上下左右旋转,具体函数实现官方写好了//z轴旋转,左右//y轴旋转,上下PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);// 设置"动作"绑定。//上面都是BingActionPlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);}
//********************************************************************
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);
}void AFPSCharacter::StartJump()
{/*源代码UPROPERTY(BlueprintReadOnly, Category = Character)uint32 bPressedJump : 1;*/bPressedJump = true;
}void AFPSCharacter::StopJump()
{bPressedJump = false;
}

03 03 演示(有了相机,视角跟02 04 演示的不一样,起码看不到白色)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

04 胳膊

04 01 下载胳膊

胳膊
在这里插入图片描述

04 02 导入

在这里插入图片描述

04 03 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"//在.h没引入相关的头文件,没报错,是因为下面声明了一下
class UCameraComponent;
class USkeletalMeshComponent;UCLASS()
class QUICKSTART_API AFPSCharacter : public ACharacter
{GENERATED_BODY()public:// Sets default values for this character's propertiesAFPSCharacter();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:	// Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;// 处理前进和后退的输入。UFUNCTION()void MoveForward(float Value);// 处理左右移动的输入。UFUNCTION()void MoveRight(float Value);// 按下按键时设置跳跃标记。UFUNCTION()void StartJump();// 松开按键时清除跳跃标记。UFUNCTION()void StopJump();// FPS 摄像机。UPROPERTY(VisibleAnywhere)UCameraComponent* FPSCameraComponent;// 第一人称模型(手臂),仅对拥有玩家可见。UPROPERTY(VisibleDefaultsOnly, Category = Mesh)//骨骼类型的组件//#include "Components/SkeletalMeshComponent.h"(.cpp)USkeletalMeshComponent* FPSMesh;
};

04 04 FPSCharacter.cpp

// Fill out your copyright notice in the Description page of Project Settings.#include "FPSCharacter.h"
#include "Engine/Engine.h"
#include "Camera/CameraComponent.h"
#include "Components/SkeletalMeshComponent.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;//*************************************************************************// 创建一个第一人称摄像机组件。FPSCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));/*void USceneComponent::SetupAttachment(class USceneComponent* InParent, FName InSocketName)class UCapsuleComponent* GetCapsuleComponent() const { return CapsuleComponent; }一个是场景类型,一个是胶囊体类型,类型不匹配,需要强转类型官网:FPSCameraComponent->SetupAttachment(GetCapsuleComponent());*/// 将摄像机组件附加到胶囊体组件。FPSCameraComponent->SetupAttachment((USceneComponent *)GetCapsuleComponent());// 将摄像机放置在眼睛上方不远处。FPSCameraComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f + BaseEyeHeight));// 用 pawn 控制摄像机旋转。FPSCameraComponent->bUsePawnControlRotation = true;//*************************************************************************// 为拥有玩家创建一个第一人称模型组件。FPSMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("FirstPersonMesh"));// 该模型仅对拥有玩家可见。//其他玩家看到的你的胳膊断的,像阴阳师的鬼切FPSMesh->SetOnlyOwnerSee(true);// 将 FPS 模型添加到 FPS 摄像机。FPSMesh->SetupAttachment(FPSCameraComponent);// 禁用部分环境阴影,保留单一模型存在的假象。//断臂投影也不行FPSMesh->bCastDynamicShadow = false;FPSMesh->CastShadow = false;// 拥有玩家无法看到普通(第三人称)身体模型。GetMesh()->SetOwnerNoSee(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);// 上下左右旋转,具体函数实现官方写好了//z轴旋转,左右//y轴旋转,上下PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);// 设置"动作"绑定。//上面都是BingActionPlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);}
//********************************************************************
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);
}void AFPSCharacter::StartJump()
{/*源代码UPROPERTY(BlueprintReadOnly, Category = Character)uint32 bPressedJump : 1;*/bPressedJump = true;
}void AFPSCharacter::StopJump()
{bPressedJump = false;
}

04 05 添加模型

在这里插入图片描述

04 06 调参数

在这里插入图片描述

04 07 演示(穿模了)

在这里插入图片描述

bug C2664 类型不匹配,需要强转类型

试试注释大法,但问题很严重,摄像机会不跟着角色,不动。

	/*void USceneComponent::SetupAttachment(class USceneComponent* InParent, FName InSocketName)class UCapsuleComponent* GetCapsuleComponent() const { return CapsuleComponent; }一个是场景类型,一个是胶囊体类型,类型不匹配,需要强转类型官网:FPSCameraComponent->SetupAttachment(GetCapsuleComponent());*/// 将摄像机组件附加到胶囊体组件。FPSCameraComponent->SetupAttachment((USceneComponent *)GetCapsuleComponent());

在这里插入图片描述


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部