射击游戏案例(一)

2024-05-13 19:20
文章标签 游戏 案例 射击

本文主要是介绍射击游戏案例(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一、使用到的资源:

1.小白人动画包(AnimStarterPack)

2.基础武器包(MilitaryWeapSilver)

 二、角色创建

2.1 添加摄像机臂和摄像机组件

 2.2 基于创建的角色C++类创建对应的蓝图类

2.3 基础项目设置 

2.4 角色按键输入​

一、使用到的资源:

以下使用到的资源都可以在虚幻商城免费资源处获取,也可以使用自己的资源。

1.小白人动画包(AnimStarterPack)

AnimStarterPack

2.基础武器包(MilitaryWeapSilver)

 二、角色创建

以Character为基类创建出需要的角色,我这里创建了一个BaseCharacter作为角色类的基类,并以这个角色类为基类创建出子类ManCharacter,将部分功能放到子类中去处理。

2.1 添加摄像机臂和摄像机组件

BaseCharacter.h中:

private:
/*
*摄像机臂
*/
UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category=Components,meta=(AllowPrivateAccess="true"))
USpringArmComponent*CameraBoom;
/*
*摄像机
*/
UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category=Components,meta=(AllowPrivateAccess="true"))
UCameraComponent*FollowCamera;

BaseCharacter.cpp中:

ABaseCharacter::ABaseCharacter()
{PrimaryActorTick.bCanEverTick = true;CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));CameraBoom->SetupAttachment(RootComponent);CameraBoom->bUsePawnControlRotation = true;FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));FollowCamera->SetupAttachment(CameraBoom);
}

在cpp中需要添加的头文件:

#include"Camera/CameraComponent.h"
#include"GameFramework/SpringArmComponent.h"

 2.2 基于创建的角色C++类创建对应的蓝图类

创建完两个组件之后,在编辑器中可以选中BaseCharacter或者ManCharacter右键创建下属的蓝图类,这个是为了方便进行可视化操作。创建出来之后,在代码里面创建的摄像机臂和摄像机组件就会显示在创建的蓝图类中。

 选中网格体的组件(骨骼体组件),为骨骼体组件赋予模型,模型对应的动画后续再进行赋予。调整模型的位置,这里的下移-88是根据胶囊体组件的高度进行调整的,旋转是因为要将模型角色的正面面向箭头组件,这样会跟移动的方向一致。

2.3 基础项目设置 

打开项目设置,找到地图和模式,将默认的GameMode设置为自己的,并将默认Pawn类,HUD,以及玩家控制器设置为自己创建的;编辑器开始地图和游戏默认地图也设置为自己创建的。

 运行,调整自己的角色在界面中的位置。

2.4 角色按键输入​

编辑器按键设置,在项目设置里面的输入模块,在这里并没有使用UE5特有的增强输入系统,还是按照UE4原有的轴绑定和按键绑定,可以先加一个角色前后左右走和上下左右看,以及跳跃的输入绑定。 

 BaseCharacter.h中:

/*
*角色移动和视角查看
*/
voidMoveForward(floatValue);
voidMoveRight(floatValue);
voidTurn(floatValue);
voidLookUp(floatValue);

BaseCharacter.cpp中:

void ABaseCharacter::MoveForward(float Value)
{if (Controller && Value != 0.0f){const FRotator ControlRotation = Controller->GetControlRotation();const FRotator NewRotation = FRotator(0.0f,ControlRotation.Yaw,0.0f);const FVector Dir = FRotationMatrix(NewRotation).GetUnitAxis(EAxis::X);AddMovementInput(Dir,Value);}
}void ABaseCharacter::MoveRight(float Value)
{if (Controller && Value != 0.0f){const FRotator ControlRotation = Controller->GetControlRotation();const FRotator NewRotation = FRotator(0.0f,ControlRotation.Yaw,0.0f);const FVector Dir = FRotationMatrix(NewRotation).GetUnitAxis(EAxis::Y);AddMovementInput(Dir,Value);}
}void ABaseCharacter::Turn(float Value)
{AddControllerYawInput(Value*0.25f);
}void ABaseCharacter::LookUp(float Value)
{const FRotator ControlRotation = Controller->GetControlRotation();const float PitchValue = ControlRotation.Pitch;//限制上下视角if (PitchValue >= 30.0f && PitchValue < 180.0f && Value < 0.0f){return;}if (PitchValue > 180.0f && PitchValue <= 280.0f && Value > 0.0f){return;}AddControllerPitchInput(Value*0.25f);
}

注:视角查看的*0.25f是控制了鼠标滑动的速度,可要可不要。

在LookUp方法中,为了不让玩家在上下视角看的时候穿裆,进行了视角限制,可以在LookUp中添加屏幕打印,查看PitchValue和Value的具体值,以便去进行范围限制。

    if (GEngine){FString Msg = FString::Printf(TEXT("PitchValue:%f,Value:%f"),PitchValue,Value);GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Blue,Msg);}

全部代码:

 BaseCharacter.h

#pragma once#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "BaseCharacter.generated.h"class USpringArmComponent;
class UCameraComponent;UCLASS()
class SHOOTINGGAME_API ABaseCharacter : public ACharacter
{GENERATED_BODY()public:ABaseCharacter();protected:virtual void BeginPlay() override;/** 角色移动和视角查看*/void MoveForward(float Value);void MoveRight(float Value);void Turn(float Value);void LookUp(float Value);public:	virtual void Tick(float DeltaTime) override;virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;private:/** 摄像机臂*/UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category=Components,meta=(AllowPrivateAccess="true"))USpringArmComponent* CameraBoom;/** 摄像机*/UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category=Components,meta=(AllowPrivateAccess="true"))UCameraComponent* FollowCamera;};

BaseCharacter.cpp

#include "ShootingGame/Public/Gameplay/BaseCharacter.h"
#include"Camera/CameraComponent.h"
#include"GameFramework/SpringArmComponent.h"ABaseCharacter::ABaseCharacter()
{PrimaryActorTick.bCanEverTick = true;CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));CameraBoom->SetupAttachment(RootComponent);CameraBoom->bUsePawnControlRotation = true;FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));FollowCamera->SetupAttachment(CameraBoom);
}void ABaseCharacter::BeginPlay()
{Super::BeginPlay();}void ABaseCharacter::MoveForward(float Value)
{if (Controller && Value != 0.0f){const FRotator ControlRotation = Controller->GetControlRotation();const FRotator NewRotation = FRotator(0.0f,ControlRotation.Yaw,0.0f);const FVector Dir = FRotationMatrix(NewRotation).GetUnitAxis(EAxis::X);AddMovementInput(Dir,Value);}
}void ABaseCharacter::MoveRight(float Value)
{if (Controller && Value != 0.0f){const FRotator ControlRotation = Controller->GetControlRotation();const FRotator NewRotation = FRotator(0.0f,ControlRotation.Yaw,0.0f);const FVector Dir = FRotationMatrix(NewRotation).GetUnitAxis(EAxis::Y);AddMovementInput(Dir,Value);}
}void ABaseCharacter::Turn(float Value)
{AddControllerYawInput(Value*0.25f);
}void ABaseCharacter::LookUp(float Value)
{const FRotator ControlRotation = Controller->GetControlRotation();const float PitchValue = ControlRotation.Pitch;//限制上下视角if (PitchValue >= 30.0f && PitchValue < 180.0f && Value < 0.0f){return;}if (PitchValue > 180.0f && PitchValue <= 280.0f && Value > 0.0f){return;}AddControllerPitchInput(Value*0.25f);
}void ABaseCharacter::Tick(float DeltaTime)
{Super::Tick(DeltaTime);}void ABaseCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{Super::SetupPlayerInputComponent(PlayerInputComponent);PlayerInputComponent->BindAxis("MoveForward",this,&ABaseCharacter::MoveForward);PlayerInputComponent->BindAxis("MoveRight",this,&ABaseCharacter::MoveRight);PlayerInputComponent->BindAxis("Turn",this,&ABaseCharacter::Turn);PlayerInputComponent->BindAxis("LookUp",this,&ABaseCharacter::LookUp);PlayerInputComponent->BindAction("Jump",IE_Pressed,this,&ABaseCharacter::Jump);PlayerInputComponent->BindAction("Jump",IE_Released,this,&ABaseCharacter::StopJumping);
}

这篇关于射击游戏案例(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/986601

相关文章

MyBatis分页查询实战案例完整流程

《MyBatis分页查询实战案例完整流程》MyBatis是一个强大的Java持久层框架,支持自定义SQL和高级映射,本案例以员工工资信息管理为例,详细讲解如何在IDEA中使用MyBatis结合Page... 目录1. MyBATis框架简介2. 分页查询原理与应用场景2.1 分页查询的基本原理2.1.1 分

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

Java 正则表达式的使用实战案例

《Java正则表达式的使用实战案例》本文详细介绍了Java正则表达式的使用方法,涵盖语法细节、核心类方法、高级特性及实战案例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、正则表达式语法详解1. 基础字符匹配2. 字符类([]定义)3. 量词(控制匹配次数)4. 边

Python Counter 函数使用案例

《PythonCounter函数使用案例》Counter是collections模块中的一个类,专门用于对可迭代对象中的元素进行计数,接下来通过本文给大家介绍PythonCounter函数使用案例... 目录一、Counter函数概述二、基本使用案例(一)列表元素计数(二)字符串字符计数(三)元组计数三、C

Spring Boot 整合 SSE(Server-Sent Events)实战案例(全网最全)

《SpringBoot整合SSE(Server-SentEvents)实战案例(全网最全)》本文通过实战案例讲解SpringBoot整合SSE技术,涵盖实现原理、代码配置、异常处理及前端交互,... 目录Spring Boot 整合 SSE(Server-Sent Events)1、简述SSE与其他技术的对

MySQL 临时表与复制表操作全流程案例

《MySQL临时表与复制表操作全流程案例》本文介绍MySQL临时表与复制表的区别与使用,涵盖生命周期、存储机制、操作限制、创建方法及常见问题,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随小... 目录一、mysql 临时表(一)核心特性拓展(二)操作全流程案例1. 复杂查询中的临时表应用2. 临时

MySQL 数据库表与查询操作实战案例

《MySQL数据库表与查询操作实战案例》本文将通过实际案例,详细介绍MySQL中数据库表的设计、数据插入以及常用的查询操作,帮助初学者快速上手,感兴趣的朋友跟随小编一起看看吧... 目录mysql 数据库表操作与查询实战案例项目一:产品相关数据库设计与创建一、数据库及表结构设计二、数据库与表的创建项目二:员

C#中的Drawing 类案例详解

《C#中的Drawing类案例详解》文章解析WPF与WinForms的Drawing类差异,涵盖命名空间、继承链、常用类及应用场景,通过案例展示如何创建带阴影圆角矩形按钮,强调WPF的轻量、可动画特... 目录一、Drawing 是什么?二、典型用法三、案例:画一个“带阴影的圆角矩形按钮”四、WinForm

setsid 命令工作原理和使用案例介绍

《setsid命令工作原理和使用案例介绍》setsid命令在Linux中创建独立会话,使进程脱离终端运行,适用于守护进程和后台任务,通过重定向输出和确保权限,可有效管理长时间运行的进程,本文给大家介... 目录setsid 命令介绍和使用案例基本介绍基本语法主要特点命令参数使用案例1. 在后台运行命令2.

Python38个游戏开发库整理汇总

《Python38个游戏开发库整理汇总》文章介绍了多种Python游戏开发库,涵盖2D/3D游戏开发、多人游戏框架及视觉小说引擎,适合不同需求的开发者入门,强调跨平台支持与易用性,并鼓励读者交流反馈以... 目录PyGameCocos2dPySoyPyOgrepygletPanda3DBlenderFife