‫So our objective is to be able to shoot projectiles and get an explosion effect when the projectile
‫hits something, including when we hit the enemy tower and when the enemy tower projectiles hit us.
‫Hello and welcome back.
‫Now let's take a look at our section plan and see how far we've come.
‫Now we know we've created a tank.
‫It can move throughout the world.
‫We made an enemy turret class.
‫We added fire functionality and projectiles along with health damage and destruction in our game.
‫Now we have winning and losing conditions and we're displaying those with widgets to our HUD.
‫Now the last item is to add our special effects, which means we need some smoke trailing behind our
‫projectile as well as some explosions and sounds.
‫And there are going to be a couple other things that we add to spice up our game and make it look a
‫bit more polished.
‫Now, in this video, we're going to handle some hit particles.
‫These are special effects for when the projectile hits something.
‫This is going to involve something called a particle system.
‫So let's go back into the editor and take a look at the particle systems we have in our project.
‫So I'm here in the editor.
‫I'm going to go into the assets folder and effects and here we have three effects.
‫These are called particle systems.
‫Now we have a hit effect, a death effect and a projectile trail.
‫Now let's go ahead and double click on p hit effect and you'll see that here we have a particle system
‫editor.
‫And in this little viewport, we'll see this explosion happening over and over again.
‫Now, luckily, this particle system has already been created for us and it looks like a nice little
‫explosion.
‫And we would like to see this explosion happen as soon as the projectile hits something.
‫So that is our goal for this video.
‫So here in V's code, I'd like to go back into our projectile class.
‫So let's go into projectile H and I'm going to add a variable to this class and this is going to be
‫a particle system.
‫Now, this can be a private variable and the type is going to be U.
‫Particle system and it's going to be a pointer and we can call this hit particles.
‫Now I'm going to forward declare this and I'm also going to give this a new property.
‫So let's say you property and I want to make this at it anywhere so we can set this from the blueprint
‫for our projectile.
‫And while I'm at it, I'm going to give this a category and I'll place this in the category combat,
‫since it's sort of related to our other combat variables.
‫Now this is edit anywhere because we plan on setting this from within the blueprint so we can go ahead
‫and compile this.
‫Now let's hop back in the editor and I'm going to close out of my other blueprints and go into our actors
‫folder here in blueprints.
‫Actors, here's our B.P. projectile.
‫Let's open that up and open the full blueprint editor.
‫Now, let's take a look in the details.
‫Panel.
‫Remember, BP projectile self must be selected to see these overall variables.
‫And here's our combat section and we see hit particles.
‫Now here we have a dropdown and we can select particle systems.
‫Since this variable is of type you particle system, we're going to use the hit effect for our hit particles.
‫So now we have a variable and its value is set here in the projectile blueprint.
‫So let's compile and save this.
‫Now, the next step is to actually spawn this particle system as soon as we've hit something, which
‫means on hit is the perfect function to do this in now we need to know how to spawn particles and use
‫gameplay.
‫Statics has a handy function for that.
‫It's called spawn emitter at location and it says plays the specified effect at the given location and
‫rotation.
‫Fire and forget.
‫This is nice because we don't have to worry about removing it or deleting it.
‫We just fire off this function.
‫It spawns the particle system and takes care of cleaning it up for us, which is great.
‫So let's take a look at the syntax now.
‫There are a number of input parameters, but this function has a number of overloads.
‫Now, since there are a number of versions of this function, let's go into V's code and take a look
‫at those different versions.
‫Now I'm going to open up projectile CP and take a look at the on hit function.
‫Now just down here at the bottom I'm going to type you gameplay statics spawn emitter at location and
‫as soon as I type the opening parentheses, it's pop up is showing me the input parameters I need to
‫pass in.
‫But we see one of three, which means there are three different versions of this function.
‫Three Overloads.
‫I'm going to click the down arrow twice to take a look at this particular overload.
‫Now it takes a world context object, a particle system and a location, and the remaining input parameters
‫have.
‫Default values, so they're optional.
‫Notice the rotation parameter is set to zero rotator.
‫Now if we like, we could set the emitters rotation to the rotation of the actor by passing in get active
‫rotation.
‫It's not too important, but I would like to spawn the emitter with the same rotation as the actor.
‫So I'd like to use get active rotation and pass that in.
‫But the rest of these parameters are optional.
‫So since we know we need to call spawn emitter at location to play a particle system, we can do this
‫in the on hit function.
‫We know the first parameter is a world context object and we already know how to handle that.
‫We know that it takes a particle system and we have a particle system variable that we can pass in.
‫We also know that we need a location and a rotation and we know how to get those for a given actor using
‫get actor location and get actor rotation.
‫So since you have all of this information, I'd like you to use the U gameplay statics function spawn
‫emitter at location in the on hit function for the projectile class.
‫So passing this for the world context object, passing the particle system itself, that new hit particles
‫variable we created and use the projectiles location using get active location and use the projectiles
‫rotation using get actor rotation.
‫Pause the video and spawn the emitter in the on hit function now.
‫Okay.
‫So I'm going to remove this line that we used just to take a look at the function overloads for this
‫function.
‫And I'm going to place this just before we destroy the actor.
‫So if you placed it, say, at the end of the function, that's okay.
‫The challenge was left open ended for you.
‫But we're going to place this just before we destroy the actor, because if we destroy the actor, then
‫it doesn't make sense to spawn particles after that.
‫So we're going to do this before we destroy the actor.
‫So we're going to say you gameplay statics spawn emitter at location and first we need our world context
‫object, which is going to be this.
‫Next we need our hit particles followed by our actors location for which we're going to use get actor
‫location.
‫Next we're going to pass in the rotation for which we'll use get actor rotation and now we're spawning
‫the emitter at the location.
‫Now, one thing I'd like to point out is that if my owner is a null pointer and we return out of this
‫function early, well, it might be a good idea to just destroy the projectile there.
‫So I'd like to remove this return statement and give this if statement its own body will still return.
‫But just before we return, we should be destroying the actor.
‫So I'm going to call destroy here in this if check.
‫And in fact, even if we fail all of the other checks here, checking the other actor, we don't want
‫a projectile just lingering around in the world.
‫So we should move this second call to destroy outside the if check.
‫That way, we're destroying the projectile when it hits something, no matter what.
‫That way, we don't have lingering projectiles for any reason.
‫Okay?
‫And while we're being safe, we should notice that we're using hit particles here, which means we should
‫check to see if the hit particles are set before we attempt to use them.
‫Otherwise, we might be using a hit particles variable that has not been set and we should not use null
‫pointers.
‫So we're going to place an if check here to see if hit particles is valid and if it is valid, then
‫it's safe to call spawn emitter at location here.
‫All right.
‫Let's go ahead and compile this now.
‫And with a successful compile, we'll hop back into the editor and here's BP projectile.
‫We have our hit particles set, so make sure that that is set before we test this out.
‫Otherwise, we won't see anything.
‫And if we hit play, we can wait for our go widget and then boom, check that out.
‫We have clouds of smoke.
‫We have hit particles being spawned.
‫I'm going to go ahead and shoot at our enemies and we'll see that they get spawned when we're hitting
‫the enemies as well.
‫Perfect.
‫So in summary, we now have spawned some hit particles.
‫We have explosions when our projectiles hit something in the world.
‫And the next step is to have a smoke trail that follows behind the projectile as it's flying through
‫the air.
‫And we'll take care of that in the next video.
‫I'll see you soon.