射击游戏案例(一)

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

相关文章

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

SpringBoot实现动态插拔的AOP的完整案例

《SpringBoot实现动态插拔的AOP的完整案例》在现代软件开发中,面向切面编程(AOP)是一种非常重要的技术,能够有效实现日志记录、安全控制、性能监控等横切关注点的分离,在传统的AOP实现中,切... 目录引言一、AOP 概述1.1 什么是 AOP1.2 AOP 的典型应用场景1.3 为什么需要动态插

Golang操作DuckDB实战案例分享

《Golang操作DuckDB实战案例分享》DuckDB是一个嵌入式SQL数据库引擎,它与众所周知的SQLite非常相似,但它是为olap风格的工作负载设计的,DuckDB支持各种数据类型和SQL特性... 目录DuckDB的主要优点环境准备初始化表和数据查询单行或多行错误处理和事务完整代码最后总结Duck

MySQL不使用子查询的原因及优化案例

《MySQL不使用子查询的原因及优化案例》对于mysql,不推荐使用子查询,效率太差,执行子查询时,MYSQL需要创建临时表,查询完毕后再删除这些临时表,所以,子查询的速度会受到一定的影响,本文给大家... 目录不推荐使用子查询和JOIN的原因解决方案优化案例案例1:查询所有有库存的商品信息案例2:使用EX

Python开发围棋游戏的实例代码(实现全部功能)

《Python开发围棋游戏的实例代码(实现全部功能)》围棋是一种古老而复杂的策略棋类游戏,起源于中国,已有超过2500年的历史,本文介绍了如何用Python开发一个简单的围棋游戏,实例代码涵盖了游戏的... 目录1. 围棋游戏概述1.1 游戏规则1.2 游戏设计思路2. 环境准备3. 创建棋盘3.1 棋盘类

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

性能分析之MySQL索引实战案例

文章目录 一、前言二、准备三、MySQL索引优化四、MySQL 索引知识回顾五、总结 一、前言 在上一讲性能工具之 JProfiler 简单登录案例分析实战中已经发现SQL没有建立索引问题,本文将一起从代码层去分析为什么没有建立索引? 开源ERP项目地址:https://gitee.com/jishenghua/JSH_ERP 二、准备 打开IDEA找到登录请求资源路径位置

深入探索协同过滤:从原理到推荐模块案例

文章目录 前言一、协同过滤1. 基于用户的协同过滤(UserCF)2. 基于物品的协同过滤(ItemCF)3. 相似度计算方法 二、相似度计算方法1. 欧氏距离2. 皮尔逊相关系数3. 杰卡德相似系数4. 余弦相似度 三、推荐模块案例1.基于文章的协同过滤推荐功能2.基于用户的协同过滤推荐功能 前言     在信息过载的时代,推荐系统成为连接用户与内容的桥梁。本文聚焦于

【区块链 + 人才服务】可信教育区块链治理系统 | FISCO BCOS应用案例

伴随着区块链技术的不断完善,其在教育信息化中的应用也在持续发展。利用区块链数据共识、不可篡改的特性, 将与教育相关的数据要素在区块链上进行存证确权,在确保数据可信的前提下,促进教育的公平、透明、开放,为教育教学质量提升赋能,实现教育数据的安全共享、高等教育体系的智慧治理。 可信教育区块链治理系统的顶层治理架构由教育部、高校、企业、学生等多方角色共同参与建设、维护,支撑教育资源共享、教学质量评估、

客户案例:安全海外中继助力知名家电企业化解海外通邮困境

1、客户背景 广东格兰仕集团有限公司(以下简称“格兰仕”),成立于1978年,是中国家电行业的领军企业之一。作为全球最大的微波炉生产基地,格兰仕拥有多项国际领先的家电制造技术,连续多年位列中国家电出口前列。格兰仕不仅注重业务的全球拓展,更重视业务流程的高效与顺畅,以确保在国际舞台上的竞争力。 2、需求痛点 随着格兰仕全球化战略的深入实施,其海外业务快速增长,电子邮件成为了关键的沟通工具。