`
aigo
  • 浏览: 2720174 次
  • 性别: Icon_minigender_1
  • 来自: 宜昌
社区版块
存档分类
最新评论

[UE4]C++代码实现播放粒子特效

UE4 
阅读更多

函数参数说明(在GameplayStatics.h中)

将一个粒子放到指定位置上播放: 

/** Plays the specified effect at the given location and rotation, fire and forget. The system will go away when the effect is complete. Does not replicate.
	 * @param WorldContextObject - Object that we can obtain a world context from
	 * @param EmitterTemplate - particle system to create
	 * @param Location - location to place the effect in world space
	 * @param Rotation - rotation to place the effect in world space	
	 * @param bAutoDestroy - Whether the component will automatically be destroyed when the particle system completes playing or whether it can be reactivated
	 */
	UFUNCTION(BlueprintCallable, Category="Effects|Components|ParticleSystem", meta=(Keywords = "particle system", WorldContext="WorldContextObject", UnsafeDuringActorConstruction = "true"))
	static UParticleSystemComponent* SpawnEmitterAtLocation(UObject* WorldContextObject, class UParticleSystem* EmitterTemplate, FVector Location, FRotator Rotation = FRotator::ZeroRotator, bool bAutoDestroy = true);

 另一种重载形式:

static UParticleSystemComponent* SpawnEmitterAtLocation(UWorld* World, class UParticleSystem* EmitterTemplate, const FTransform& SpawnTransform, bool bAutoDestroy = true);

 

 

将一个粒子attach到某个Component上播放:

/** Plays the specified effect attached to and following the specified component. The system will go away when the effect is complete. Does not replicate.
	* @param EmitterTemplate - particle system to create
	 * @param AttachComponent - Component to attach to.
	 * @param AttachPointName - Optional named point within the AttachComponent to spawn the emitter at
	 * @param Location - Depending on the value of Location Type this is either a relative offset from the attach component/point or an absolute world position that will be translated to a relative offset
	 * @param Rotation - Depending on the value of LocationType this is either a relative offset from the attach component/point or an absolute world rotation that will be translated to a realative offset
	 * @param LocationType - Specifies whether Location is a relative offset or an absolute world position
	 * @param bAutoDestroy - Whether the component will automatically be destroyed when the particle system completes playing or whether it can be reactivated
	 */
	UFUNCTION(BlueprintCallable, Category="Effects|Components|ParticleSystem", meta=(Keywords = "particle system", UnsafeDuringActorConstruction = "true"))
	static UParticleSystemComponent* SpawnEmitterAttached(class UParticleSystem* EmitterTemplate, class USceneComponent* AttachToComponent, FName AttachPointName = NAME_None, FVector Location = FVector(ForceInit), FRotator Rotation = FRotator::ZeroRotator, EAttachLocation::Type LocationType = EAttachLocation::KeepRelativeOffset, bool bAutoDestroy = true);

 

 

具体使用实例

头文件,其中MuzzleFX在蓝图中设置为具体哪个粒子资源,Mesh是要AttachTo的Component

UPROPERTY(EditDefaultsOnly)
UParticleSystem* MuzzleFX;

/** weapon mesh: 3rd person view */
UPROPERTY(VisibleDefaultsOnly, Category = Mesh)
USkeletalMeshComponent* Mesh;


cpp中调用:

//此方法只播放一遍即结束
UGameplayStatics::SpawnEmitterAttached(MuzzleFX, Mesh, "MyAttachPoint");

 

 

 

 

 

=====================================================
上面粒子是用蓝图直接创建ParticleSystem(粒子系统),下面是用C++代码创建ParticleSystem:

头文件中定义:

UParticleSystem* TestParticle;
UParticleSystemComponent* LastPSC;

 

 

创建ParticleSystem:

void AHGameMode::LoadParticlesClass()
{
	static ConstructorHelpers::FObjectFinder<UParticleSystem> MyParticleSystem(TEXT("ParticleSystem'/Game/Particles/Oneshot/P_mc01CIrcle05_large_oneshot.P_mc01CIrcle05_large_oneshot'"));
	if (MyParticleSystem.Succeeded()) 
	{
		TestParticle = MyParticleSystem.Object;
	}
}

 

播放粒子:

void AHGameMode::PlayParticle_01()
{
	LastPSC = UGameplayStatics::SpawnEmitterAttached(TestParticle, MyCharacter->GetMesh(), "MyParticle");
}

 

  

如果是循环播放的粒子,手动控制粒子的播放和关闭(如果是一个只播放一次的粒子,可以不用手动停止):

//ActivateSystem的bool参数表示开启特效后是否重置特效(从头播放)
LastPSC->ActivateSystem(true);

 
关闭特效:

LastPSC->DeactivateSystem();

 
如果希望 DeactivateSystem 之后,再次开启 ActivateSystem 的时候能自动激活特效,需要禁用自动销毁:

UParticleSystemComponent->bAutoDestroy = false;

 

 

参考:

How can I get a particle to spawn in a specific location?

https://answers.unrealengine.com/questions/36451/particle-spawns-in-incorrect-location.html

 

How can I spawn an instance of an emitter during runtime?

https://answers.unrealengine.com/questions/42209/spawning-emitter-content-that-exists-in-the-conten.html

分享到:
评论

相关推荐

    UE4学习笔记----使用C++之控制球体运动并使用移动粒子效果(源代码)

    粒子系统的创建和控制可以通过C++代码或者在UE4编辑器中通过蓝图实现。 6. **集成粒子系统**: 要将粒子系统附加到球体上,你需要创建一个`UParticleSystemComponent`实例,并将其添加到`CollidingPawn`或`...

    UE4离线官方中文文档

    6. **粒子系统**:UE4的粒子系统可以创建火焰、烟雾、水波等各种特效。文档会指导如何设计和编辑粒子系统,以实现炫酷的视觉效果。 7. **音频处理**:UE4提供了全面的音频工具,包括音频播放、空间化和触发器。学习...

    fir_UE4火焰文件源码_ue4_火焰_

    《UE4火焰效果实现解析与源码探讨》 ...无论是C++编程还是蓝图设计,都能在UE4中找到实现火焰特效的途径。通过对源码的学习和实践,开发者不仅可以提升自己的技能,还能为游戏带来更加震撼的视觉体验。

    Unreal Engine 4 Scripting with C++ Cookbook willam

    10. **渲染与特效**:通过C++深入理解UE4的渲染管线,包括材质、着色器、光照和粒子系统。学习如何自定义渲染效果和创建复杂视觉特效。 本书《Unreal Engine 4 Scripting with C++ Cookbook》将通过一系列实例和...

    UE4文档渲染 & 图像——Cascade Particle Systems

    8. **脚本控制**:除了可视化编辑器,还可以通过蓝图或C++代码对粒子系统进行更深入的控制,实现特定的游戏逻辑或特定的触发条件。 9. **性能优化**:UE4提供了多种优化手段,如LOD(Level of Detail)系统、批次...

    UE4技能系统全面训练最后的代码.zip

    1. **虚幻蓝图**:UE4主要采用蓝图系统,允许开发者无需编写C++代码即可进行游戏逻辑设计。蓝图系统包括事件图、组件图、类蓝图等,用于创建行为、物体和类。 2. **技能触发器**:学习如何设置技能触发条件,如按键...

    UE4里做暴炸效果的蓝图脚本

    在UE4(Unreal Engine 4)中创建爆炸效果,蓝图脚本是一种强大的工具,它允许开发者无需编写C++代码就能实现复杂的逻辑和特效。在这个过程中,我们将探讨如何使用蓝图来设计一个逼真的爆炸效果。 首先,让我们了解...

    UE4Niagara沙盒

    《UE4 Niagara沙盒:深度探索C++编程在视觉特效中的应用》 UE4 Niagara沙盒是一个基于Epic Games的Unreal Engine 4(UE4)的特效制作工具,它为开发者提供了强大的粒子系统和视觉效果设计能力。在这个环境中,用户...

    FPS_template_UE4:UE4上FPS游戏的基本射击游戏模板

    5. **声音和特效(Sound and Effects)**:射击游戏中的音效和粒子特效能增强沉浸感。模板可能包含了基础的射击声效和击中反馈效果,开发者可以通过调整或添加新的声音和特效来提升游戏体验。 6. **物理系统...

    UE4-Battle-Tank:虚幻引擎4中具有简单AI的开放世界坦克格斗游戏

    UE4提供了内置的音频和粒子效果编辑器,用于创建和编辑游戏中的各种音效和特效。 10. **优化与性能**:为了确保游戏在各种硬件配置上都能流畅运行,开发者需要进行性能优化,如减少多边形数量、优化内存使用、调整...

    parkour-ue4:在虚幻引擎4中制作的WIP Parkour风格原型

    UE4提供了材质编辑器和粒子系统,可以创建出逼真的光照、粒子特效和动态材质。 8. **性能优化**:对于实时游戏,性能优化是必不可少的。开发者可能使用了UE4的各种工具和技术,如LOD(细节层次)、静态和动态光照...

    UE中文版编辑器

    6. **粒子系统**:学习创建火焰、烟雾、水波等特效,提升场景视觉表现。 7. **物理模拟**:了解如何设置刚体、软体物体,模拟现实世界的物理行为。 8. **蓝图类与继承**:理解蓝图类的结构,进行组件复用和代码...

    ue15中文版

    9. **集成开发环境**:UE15集成了代码编辑器,支持C++编程,为专业开发者提供了深入定制游戏逻辑的可能性。 10. **教程与社区**:Epic Games提供了大量的教学资源和活跃的社区支持,帮助用户快速上手和解决问题。 ...

    MyProject.zip

    在UE4(Unreal Engine 4)中,C++是一种常用的语言来实现游戏逻辑和扩展引擎功能。在本项目"MyProject.zip"中,开发者显然在尝试利用C++为UE4添加一个BillboardComponent,并设置其显示引擎默认的图片。下面我们将...

    Unreal Engine Essentials

    1. **C++编程**:虽然UE4提供了蓝prints系统,但深入学习C++能让你更高效地编写复杂逻辑和高性能代码。 2. **插件与扩展**:理解如何使用或开发插件来扩展UE4的功能,以满足特定项目需求。 七、优化与发布 1. **...

    管道:研究UE4的项目

    2. **蓝图系统**:UE4的蓝图系统是其一大特色,它允许非程序员通过图形化界面创建和编辑游戏逻辑,无需编写C++代码。蓝图分为事件图表、组件蓝图和类蓝图,分别用于实现不同层面的游戏逻辑。 3. **材质与着色器**:...

    GGJ2021

    引擎支持高级光照系统、物理模拟、粒子特效以及复杂的材质编辑,使得游戏世界显得更加生动和真实。此外,UE4还提供了蓝图系统,这是一种可视化编程工具,允许非程序员通过连接节点来实现逻辑控制,大大降低了游戏...

    ParticleToy:粒子行为的纯逻辑

    在"ParticleToy"中,C++代码用于定义粒子的生命周期,计算粒子间的相互作用,并处理与引擎的交互。理解C++对于掌握粒子系统的底层逻辑至关重要。 接下来,我们谈谈"ParticleToy"与UE4的集成。UE4是一款强大的实时3D...

Global site tag (gtag.js) - Google Analytics