射击游戏案例(一)

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

相关文章

MySQL 表的内外连接案例详解

《MySQL表的内外连接案例详解》本文给大家介绍MySQL表的内外连接,结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录表的内外连接(重点)内连接外连接表的内外连接(重点)内连接内连接实际上就是利用where子句对两种表形成的笛卡儿积进行筛选,我

Java Stream.reduce()方法操作实际案例讲解

《JavaStream.reduce()方法操作实际案例讲解》reduce是JavaStreamAPI中的一个核心操作,用于将流中的元素组合起来产生单个结果,:本文主要介绍JavaStream.... 目录一、reduce的基本概念1. 什么是reduce操作2. reduce方法的三种形式二、reduce

Spring Boot 整合 Redis 实现数据缓存案例详解

《SpringBoot整合Redis实现数据缓存案例详解》Springboot缓存,默认使用的是ConcurrentMap的方式来实现的,然而我们在项目中并不会这么使用,本文介绍SpringB... 目录1.添加 Maven 依赖2.配置Redis属性3.创建 redisCacheManager4.使用Sp

springboot项目redis缓存异常实战案例详解(提供解决方案)

《springboot项目redis缓存异常实战案例详解(提供解决方案)》redis基本上是高并发场景上会用到的一个高性能的key-value数据库,属于nosql类型,一般用作于缓存,一般是结合数据... 目录缓存异常实践案例缓存穿透问题缓存击穿问题(其中也解决了穿透问题)完整代码缓存异常实践案例Red

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

MySQL 复合查询案例详解

《MySQL复合查询案例详解》:本文主要介绍MySQL复合查询案例详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录基本查询回顾多表笛卡尔积子查询与where子查询多行子查询多列子查询子查询与from总结合并查询(不太重要)union基本查询回顾查询

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

MySQL 中的 JSON 查询案例详解

《MySQL中的JSON查询案例详解》:本文主要介绍MySQL的JSON查询的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 的 jsON 路径格式基本结构路径组件详解特殊语法元素实际示例简单路径复杂路径简写操作符注意MySQL 的 J

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模