home

Tutorial index

Unreal Engine 4: Tutorial 3 - Adding a Particle Effect and Physics

We've seen how we create a mesh for our class, and adding most other stuff is just as easy - though to get some more complex stuff to behave the way you want them may require more code, like animating a skeletal mesh for example.

Though it's advisable to place stuff in the header, we'll just keep the code all together and add this to to the constructor - after all I don't have to show you how to move a declaration, do I?


UParticleSystemComponent* ParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("Particles"));

ParticleSystem->AttachTo(SphereMesh);
ParticleSystem->bAutoActivate = true;
ParticleSystem->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));

static ConstructorHelpers::FObjectFinder<UParticleSystem> ParticleAsset(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));
if(ParticleAsset.Succeeded())
{
ParticleSystem->SetTemplate(ParticleAsset.Object) ;
}

// And now, the moment we've all been waiting for.. Physics!

SphereComponent->SetSimulatePhysics(true);

Now to take a look at the code:

ParticleSystem->AttachTo(SphereMesh);. This time we attach it to the mesh. This way, if the mesh changes position relative to the object, the particle effect follows it, so a burning satelite's particle effect follows the satelite as it moves around the planet (to which the satelite is attached to maintain a relative position) for example.

ParticleSystem->bAutoActivate = true; We want the particles to start right away. We can set it to false to trigger them later on during gameplay, which is more likely for you to use later on, but for learning's sake we'll see it this way.

ParticleSystem->SetTemplate(ParticleAsset.Object);. With particles, we use a template, as they have randomized behavior (so that explosions don't all look the same, and anything else doesn't exactly play in a loop).

SphereComponent->SetSimulatePhysics(true); Remember how we set a physics property of the collider? It handles physics on it's own, we just have to tell it to!

I know it looks like a relatively small and pointless tutorial, but trust me, if you tried to do these things on your own, you would waste quite a while searching - everyone does, and I did too - so it's best to see as much as possible before you go looking for more functionality.

Note that often you have to reopen the editor, or preferably just delete and place back in there the instance of the object you mdofied so that the changes can take effect.

In the next tutorial we will add gravity and physics to our class.