UE4 C++ ActionRoguelike开发记录

斯坦福UE4 + C++课程学习记录 19:黑洞技能 阅读详情

目录

角色类:SCharacter

设置摄像机

弹簧臂和摄像机设置

设置人物移动和动作绑定

移动与朝向:MoveForward/MoveRight、Turn/LoopUp

为人物绑定设置子弹类:SpawnProjectile适配器类

设置攻击动画以及攻击特效

普通攻击

黑洞攻击

闪现攻击

❗为角色添加属性组件:USAttributeComponent

为角色添加交互组件接口:USInteractionComponent* InteractionComp;

子弹类:SProejctileBase

胶囊碰撞体:USphereComponent* SphereComp;

子弹移动组件:UProjectileMovementComponent* MoveComp;

粒子系统组件(平A效果):UParticleSystemComponent* EffectComp;

粒子系统组件(爆炸效果):UParticleSystem* ImpactVFX;

子弹爆炸音效组件:USoundCue* ImpactSound;

?子弹播放声音组件:UAudioComponent* AudioComp;

子弹击中画面震动效果:TSubclassOf ImpactShake;

⭐碰撞事件&爆炸(虚函数):void Explode();(BlueprintNativeEvent)

普通攻击:SMagicProjectile

设置并重写OnActorOverlap逻辑:

设置子弹伤害:DamageAmount

❗闪现:SDashProjectile

重写BeginPlay:设置子弹存活时间

重写爆炸逻辑Explode_Implementation

实现闪现传送效果:TeleportInstigator

设置定时器:FTimerHandle

黑洞攻击(蓝图):Proj_BlackHole

径向力Force Strength

可影响物体Object Types to Affect

吸进黑洞后要杀掉物体:

❗接口类:USGameplayInterface

USInteractionComponent

世界中的物体

爆炸油桶:ASExplosiveBarrel

设置StaticMesh,径向力

初始化Actor组件(添加碰撞事件)

宝箱(实现交互接口):ASItemChest

血包(实现交互接口):ASPowerupActor

血包有很多种,因此接口实现逻辑由子类实现:

吃掉后隐藏血包(计时器):

血包子类:ASPowerup_HealthPotion

UI:Widget

十字准星

创建Widget并设置:

游戏开始时添加到屏幕上

人物血条、Credits、游戏进行时间:Main_HUD

血条

Credits:

游戏进行时间:

AI

UE中的AI系统

行为树:

添加导航网格体:NavMeshBoundsVolume​编辑

游戏AI角色:SAICharacter

游戏AI控制器:SAIController

设置行为树

为SAICharacter设置控制器

游戏AI逻辑-范围内攻击:USBTService_CheckAttackRange(UBTService)

范围查询逻辑

在行为树添加此Service

游戏AI范围内攻击任务结点:USBTTask_RangedAttack

AI朝向玩家:Set default focus

环境查询系统:

创建EQS

在玩家周围半径内设置EQS生成移动目的地

AI死亡


角色类:SCharacter

设置摄像机

弹簧臂和摄像机设置

	UPROPERTY(VisibleAnywhere)
	USpringArmComponent* SpringArmComp;

	UPROPERTY(VisibleAnywhere)
	UCameraComponent* CameraComp;

在构造函数中创建这两个类,并设置依赖关系:

弹簧臂附着在RootComponent,摄像机附着在弹簧臂上

	SpringArmComp = CreateDefaultSubobject<USpringArmComponent>("SpringArmComp");
	SpringArmComp->bUsePawnControlRotation = true;
	SpringArmComp->SetupAttachment(RootComponent);

	CameraComp = CreateDefaultSubobject<UCameraComponent>("CameraComp");
	CameraComp->SetupAttachment(SpringArmComp);

    GetCharacterMovement()->bOrientRotationToMovement = true;
	bUseControllerRotationYaw = false;

注意 bUseControllerRotationYaw =false;其效果为:

bUseControllerRotationYaw=true;

bUseControllerRotationYaw=false; 

应该设置为false, 

同时注意GetCharacterMovement()->bOrientRotationToMovement = true;

GetCharacterMovement()->bOrientRotationToMovement = false;如下

 GetCharacterMovement()->bOrientRotationToMovement = true;如下

类似黑暗之魂这种第三人称的移动朝向方式。

设置人物移动和动作绑定

void ASCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAxis("MoveForward", this, &ASCharacter::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &ASCharacter::MoveRight);

	PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
	PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);

	PlayerInputComponent->BindAction("PrimaryAttack", IE_Pressed, this, &ASCharacter::PrimaryAttack);
	// Used generic name 'SecondaryAttack' for binding
	PlayerInputComponent->BindAction("SecondaryAttack", IE_Pressed, this, &ASCharacter::BlackHoleAttack);
	PlayerInputComponent->BindAction("Dash", IE_Pressed, this, &ASCharacter::Dash);
	PlayerInputComponent->BindAction("PrimaryInteract", IE_Pressed, this, &ASCharacter::PrimaryInteract);

	PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
}

移动与朝向:MoveForward/MoveRight、Turn/LoopUp

void ASCharacter::MoveForward(float Value)
{
	FRotator ControlRot = GetControlRotation();
	ControlRot.Pitch = 0.0f;
	ControlRot.Roll = 0.0f;

	AddMovementInput(ControlRot.Vector(), Value);
}


void ASCharacter::MoveRight(float Value)
{
	FRotator ControlRot = GetControlRotation();
	ControlRot.Pitch = 0.0f;
	ControlRot.Roll = 0.0f;

	// X = Forward (Red)
	// Y = Right (Green)
	// Z = Up (Blue)

	FVector RightVector = FRotationMatrix(ControlRot).GetScaledAxis(EAxis::Y);

	AddMovementInput(RightVector, Value);
}

 其中Turn和LookUp绑定的函数是由APawn中已经写好的内容。

&APawn::AddControllerYawInput
&APawn::AddControllerPitchInput

这里附上Yaw、Roll、Pitch的内容,结合代码理解:

UE是左手坐标系:拇指X,食指Y,中指Z(中指向上摆放)
X:Roll翻滚角
Y:Pitch俯仰角
Z:Yaw偏航角

动图:

1.俯仰:(Pitch)

2.翻滚:(Roll)

3.偏航:(Yaw)

所以,MoveForward和MoveRight只与Yaw有关,控制偏航角度;Turn也和Yaw有关,控制偏航角度;而LoopUp与Pitch有关,控制上下移动视角。

为人物绑定设置子弹类:SpawnProjectile适配器类

  • 生成位置:手部Socket(HandLocation)
  • Actor生成:设置碰撞,instigator为角色自身
  • 碰撞预设:
  • 子弹轨迹:起点为手部,终点为十字准星方向
void ASCharacter::SpawnProjectile(TSubclassOf<AActor> ClassToSpawn)
{
	if (ensureAlways(ClassToSpawn))
	{
		FVector HandLocation = GetMesh()->GetSocketLocation(HandSocketName);

		FActorSpawnParameters SpawnParams;
		SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
		SpawnParams.Instigator = this;

		FCollisionShape Shape;
		Shape.SetSphere(20.0f);

		// Ignore Player
		FCollisionQueryParams Params;
		Params.AddIgnoredActor(this);

		FCollisionObjectQueryParams ObjParams;
		ObjParams.AddObjectTypesToQuery(ECC_WorldDynamic);
		ObjParams.AddObjectTypesToQuery(ECC_WorldStatic);
		ObjParams.AddObjectTypesToQuery(ECC_Pawn);

		FVector TraceStart = CameraComp->GetComponentLocation();

		// endpoint far into the look-at distance (not too far, still adjust somewhat towards crosshair on a miss)
		FVector TraceEnd = CameraComp->GetComponentLocation() + (GetControlRotation().Vector() * 5000);

		FHitResult Hit;
		// returns true if we got to a blocking hit
		if (GetWorld()->SweepSingleByObjectType(Hit, TraceStart, TraceEnd, FQuat::Identity, ObjParams, Shape, Params))
		{
			// Overwrite trace end with impact point in world
			TraceEnd = Hit.ImpactPoint;
		}

		// find new direction/rotation from Hand pointing to impact point in world.
		FRotator ProjRotation = FRotationMatrix::MakeFromX(TraceEnd - HandLocation).Rotator();

		FTransform SpawnTM = FTransform(ProjRotation, HandLocation);
		GetWorld()->SpawnActor<AActor>(ClassToSpawn, SpawnTM, SpawnParams);
	}
}

设置攻击动画以及攻击特效

void ASCharacter::StartAttackEffects()
{
	PlayAnimMontage(AttackAnim);

	UGameplayStatics::SpawnEmitterAttached(CastingEffect, GetMesh(), HandSocketName, FVector::ZeroVector, FRotator::ZeroRotator, EAttachLocation::SnapToTarget);
}

普通攻击

void ASCharacter::PrimaryAttack()
{
	StartAttackEffects();

	GetWorldTimerManager().SetTimer(TimerHandle_PrimaryAttack, this, &ASCharacter::PrimaryAttack_TimeElapsed, AttackAnimDelay);
}


void ASCharacter::PrimaryAttack_TimeElapsed()
{
	SpawnProjectile(ProjectileClass);
}

黑洞攻击

void ASCharacter::BlackHoleAttack()
{
	StartAttackEffects();

	GetWorldTimerManager().SetTimer(TimerHandle_BlackholeAttack, this, &ASCharacter::BlackholeAttack_TimeElapsed, AttackAnimDelay);
}


void ASCharacter::BlackholeAttack_TimeElapsed()
{
	SpawnProjectile(BlackHoleProjectileClass);
}

闪现攻击

void ASCharacter::Dash()
{
	StartAttackEffects();

	GetWorldTimerManager().SetTimer(TimerHandle_Dash, this, &ASCharacter::Dash_TimeElapsed, AttackAnimDelay);
}


void ASCharacter::Dash_TimeElapsed()
{
	SpawnProjectile(DashProjectileClass);
}

❗为角色添加属性组件:USAttributeComponent

为角色添加交互组件接口:USInteractionComponent* InteractionComp;

子弹类:SProejctileBase

ASProjectileBase::ASProjectileBase()
{
	SphereComp = CreateDefaultSubobject<USphereComponent>("SphereComp");
	SphereComp->SetCollisionProfileName("Projectile");
	SphereComp->OnComponentHit.AddDynamic(this, &ASProjectileBase::OnActorHit);
	RootComponent = SphereComp;

	EffectComp = CreateDefaultSubobject<UParticleSystemComponent>("EffectComp");
	EffectComp->SetupAttachment(RootComponent);

	AudioComp = CreateDefaultSubobject<UAudioComponent>("AudioComp");
	AudioComp->SetupAttachment(RootComponent);

	MoveComp = CreateDefaultSubobject<UProjectileMovementComponent>("ProjectileMoveComp");
	MoveComp->bRotationFollowsVelocity = true;
	MoveComp->bInitialVelocityInLocalSpace = true;
	MoveComp->ProjectileGravityScale = 0.0f;
	MoveComp->InitialSpeed = 8000;

	ImpactShakeInnerRadius = 0.0f;
	ImpactShakeOuterRadius = 1500.0f;

}

胶囊碰撞体:USphereComponent* SphereComp;

  • 设置名称
  • 设置碰撞预设
  • 设置碰撞触发事件
  • 将RootComponent设为碰撞体

子弹移动组件:UProjectileMovementComponent* MoveComp;

  • 初始化相关移动属性,比如速度

粒子系统组件(平A效果):UParticleSystemComponent* EffectComp;

在cpp中构造函数进行初始化,只需要设置名称与挂载RootComponent。

因为子弹效果必然要跟随着子弹,故需要附着在RootComponent上。

粒子系统组件(爆炸效果):UParticleSystem* ImpactVFX;

UE中不同摄像机震动的区别Camera Shake 中同步全局事件(如BOSS释放技能时,所有玩家根据自身位置感受到不同强度的震动)。都用于触发摄像机震动效果,但它们的适用场景和实现方式有所不同。一颗手雷在远处爆炸,近处玩家震动强烈,远处玩家震动微弱。与玩家直接相关的 UI 反馈(如菜单操作、技能释放)。玩家自身受到的伤害或互动(如角色被击中、跳跃落地)。:震动的影响范围(内圈完全强度,外圈衰减到零)。(如手雷爆炸时,只有附近的玩家感受到震动)。施加震动,无需指定位置,震动效果是。:震动强度缩放(1.0为默认强度)。:震动中心位置(世界坐标)。 阅读详情

相关推荐

手把手教你用示波器实测LVDS/CML信号:从波形解读到故障排查(附实测图)

本文详细介绍了如何使用示波器实测LVDS和CML信号,从波形解读到故障排查的全过程。涵盖差分探头选择、示波器配置、标准波形特征解读、眼图分析技巧以及高速信号捕获要点,帮助硬件工程师快速诊断和解决信号完整性问题。附实测图和案例,提升高速数字电路设计效率。

weixin_27215337的博客 212

n8n 快速入门1:构建一个简单的工作流

n8n 快速入门指南

engchina的专栏 1万+

从零到一:手把手部署用友T3标准版11.2及SQL Server 2008R2环境

本文详细介绍了从零开始部署用友T3标准版11.2及SQL Server 2008R2环境的完整流程,包括环境准备、SQL Server安装、环境检测与修复、T3标准版安装实战以及初始化配置与测试。通过手把手的步骤指导和常见问题解决方案,帮助用户顺利完成部署,确保系统稳定运行。

weixin_28339967的博客 472

使用Power Apps实现SharePoint Online列表的下拉菜单的级联选择

在SharePoint列表中添加数据的时候,下拉菜单的级联选择一直是一个非常实用的功能,尤其是有很多数据的情况,级联选择可以根据上一层的选择,自动过滤下一层的选择,方便用户查找和选择准确的信息。一个最常用的情景是选择供应商的地区和国家,例如有三个列表,一个是供应商所在的大洲,一个是国家,一个是供应商。当用户选择州的时候,会自动列出属于这个州的国家。避免用户迷失在大量的数据中。 这里展示一下如何在...

shrenk的专栏 7818

告别标注地狱!YOLO+CLIP多模态零样本检测全实战:图文联合推理,想检测什么直接输文字,不用训模型

这套方案我给朋友的超市用了快3个月,运行极其稳定,临期商品的检测准确率能到96%,后续加了“货架空货检测”“通道杂物堆积检测”,都是改一行文本就搞定,完全不用重新开发,朋友说省了十几万的定制开发费用。很多做计算机视觉的朋友,总觉得目标检测必须标数据、训模型,其实不是的。现在多模态大模型的能力已经足够强,把YOLO的定位能力和CLIP的图文泛化能力结合起来,就能用极低的成本,搞定绝大多数定制化检测需求。对于我们开发者来说,技术的价值从来不是炫技,而是用最简单的方案,解决最实际的问题。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 523

搭建AutoSAR开发环境

任何开发都需要一套开发环境,包括软硬件环境,笔者后续将介绍的是 NXP 的 S32K144 配套的开发板。接下来详细介绍怎么获取这些软硬件工具。仅供个人学习使用!

cfl927096306的博客 6119

【PyTorch项目实战】之ResNet系列:resnet18、resnet34、resnet50、resnet101、resnet152

ResNet (Residual Network) :是一种深度卷积神经网络(CNN)架构,其核心思想是引入残差学习(Residual Learning)机制,通过跳跃连接(skip connections)来解决深层网络训练中的退化问题。

📷 图像处理践行者 | 传统算法 · 深度学习 · 多模态 | 分享实战经验与技术洞见,探索从像素到智能的无限可能。 6388

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

上一篇: 构造函数为什么不能为虚函数?构造函数为什么不能调用虚函数?
下一篇: PaddleFL 1.2.0版本 使用docker镜像编译流程(Ubuntu 20.04)
BBBourne
博客等级 码龄9年 288粉丝 255原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值