撞击特效组件
2023年10月12日 2024年1月11日
说明
子弹和榴弹击中时, 生成特效
创建VFX组件
武器的下级组件
- | |
---|---|
基类 | ActorComponent |
STUWeaponFXComponent | |
Weapon/Components |
添加到头文件搜索路径
ShootThemUp: ShootThemUp.Build.cs
1PublicIncludePaths.AddRange(new string[] 2{ 3 "ShootThemUp/Public/Player", 4 "ShootThemUp/Public/Components", 5 "ShootThemUp/Public/Dev", 6 "ShootThemUp/Public/Weapon", 7 "ShootThemUp/Public/UI", 8 "ShootThemUp/Public/Animations", 9 "ShootThemUp/Public/Pickups", 10 "ShootThemUp/Public/Weapon/Components" 11});
添加依赖模块
ShootThemUp: ShootThemUp.Build.cs
1PublicDependencyModuleNames.AddRange(new string[] 2{ "Core", 3 "CoreUObject", 4 "Engine", 5 "InputCore", 6 "Niagara" 7});
编译项目
实现撞击特效组件
ShootThemUp: Weapon/Components/STUWeaponFXComponent.h
1class UNiagaraSystem; 2 3// public 4void PlayImpactFX(const FHitResult &Hit); 5 6// protected 7UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) 8UNiagaraSystem *Effect;
碰撞信息结构体包含生成特效的所有信息: 位置, 法向量等
ShootThemUp: Weapon/Components/STUWeaponFXComponent.cpp
1#include "NiagaraFunctionLibrary.h" 2 3void USTUWeaponFXComponent::PlayImpactFX(const FHitResult &Hit) 4{ 5 UNiagaraFunctionLibrary::SpawnSystemAtLocation(GetWorld(), Effect, Hit.ImpactPoint, Hit.ImpactNormal.Rotation()); 6}
UNiagaraFunctionLibrary::SpawnSystemAtLocation
Engine/Plugins
1static UNiagaraComponent* SpawnSystemAtLocation(const UObject* WorldContextObject, class UNiagaraSystem* SystemTemplate, FVector Location, FRotator Rotation = FRotator::ZeroRotator, FVector Scale = FVector(1.f), bool bAutoDestroy = true, bool bAutoActivate = true, ENCPoolMethod PoolingMethod = ENCPoolMethod::None, bool bPreCullCheck = true);
- 在指定位置生成特效
- 会检查粒子系统指针和上下文指针
- | |
---|---|
WorldContentObject | 上下文, 传入GetWorld()或this |
SystemTemplate | 指向Niagara System的指针 |
SpawnLocation | 生成地点 |
Rotation | 朝向 |
bAutoDestroy | 为true时, 粒子系统特效结束即从世界中移除 |
为步枪添加碰撞特效组件
ShootThemUp: Weapon/STURifleWeapon.h
1class USTUWeaponFXComponent; 2 3// protected 4UPROPERTY(VisibleAnywhere) 5USTUWeaponFXComponent *WeaponFXComponent; 6 7// public 8ASTURifleWeapon(); 9 10// protected 11virtual void BeginPlay() override;
ShootThemUp: Weapon/STURifleWeapon.cpp
屏蔽绘制子弹轨迹和击中点
1#include "Weapon/Components/STUWeaponFXComponent.h" 2 3ASTURifleWeapon::ASTURifleWeapon() 4{ 5 WeaponFXComponent = CreateDefaultSubobject<USTUWeaponFXComponent>("WeaponFXComponent"); 6} 7 8void ASTURifleWeapon::BeginPlay() 9{ 10 Super::BeginPlay(); 11 check(WeaponFXComponent); 12} 13 14// MakeShot 15if (HitResult.bBlockingHit) 16{ 17 // ... 18 WeaponFXComponent->PlayImpactFX(HitResult); 19}
为榴弹添加碰撞特效组件
ShootThemUp: Weapon/STUProjectile.h
1class USTUWeaponFXComponent; 2 3// protected 4UPROPERTY(VisibleAnywhere) 5USTUWeaponFXComponent *WeaponFXComponent;
ShootThemUp: Weapon/STUProjectile.cpp
1#include "Weapon/Components/STUWeaponFXComponent.h" 2 3// ASTUProjectile 4WeaponFXComponent = CreateDefaultSubobject<USTUWeaponFXComponent>("WeaponFXComponent"); 5 6// BeginPlay 7check(WeaponFXComponent); 8 9// OnProjectileHit 10WeaponFXComponent->PlayImpactFX(Hit);
查看
设置步枪和榴弹的粒子系统指针类型
-
步枪
-
榴弹
设置粒子系统
NS_BaseImpact
-
粒子个数
-
颜色
-
方向: 修改速度方向