﻿1
00:00:04,020 --> 00:00:10,620
‫So our objective is to be able to shoot projectiles and get an explosion effect when the projectile

2
00:00:10,620 --> 00:00:18,480
‫hits something, including when we hit the enemy tower and when the enemy tower projectiles hit us.

3
00:00:19,390 --> 00:00:21,310
‫Hello and welcome back.

4
00:00:21,340 --> 00:00:25,130
‫Now let's take a look at our section plan and see how far we've come.

5
00:00:25,150 --> 00:00:26,780
‫Now we know we've created a tank.

6
00:00:26,800 --> 00:00:28,250
‫It can move throughout the world.

7
00:00:28,270 --> 00:00:30,250
‫We made an enemy turret class.

8
00:00:30,250 --> 00:00:36,630
‫We added fire functionality and projectiles along with health damage and destruction in our game.

9
00:00:36,640 --> 00:00:42,970
‫Now we have winning and losing conditions and we're displaying those with widgets to our HUD.

10
00:00:43,000 --> 00:00:49,630
‫Now the last item is to add our special effects, which means we need some smoke trailing behind our

11
00:00:49,630 --> 00:00:53,500
‫projectile as well as some explosions and sounds.

12
00:00:53,500 --> 00:00:58,660
‫And there are going to be a couple other things that we add to spice up our game and make it look a

13
00:00:58,660 --> 00:00:59,760
‫bit more polished.

14
00:00:59,770 --> 00:01:03,370
‫Now, in this video, we're going to handle some hit particles.

15
00:01:03,370 --> 00:01:06,880
‫These are special effects for when the projectile hits something.

16
00:01:06,880 --> 00:01:10,000
‫This is going to involve something called a particle system.

17
00:01:10,000 --> 00:01:15,610
‫So let's go back into the editor and take a look at the particle systems we have in our project.

18
00:01:15,610 --> 00:01:17,110
‫So I'm here in the editor.

19
00:01:17,110 --> 00:01:22,840
‫I'm going to go into the assets folder and effects and here we have three effects.

20
00:01:22,840 --> 00:01:25,600
‫These are called particle systems.

21
00:01:25,600 --> 00:01:30,280
‫Now we have a hit effect, a death effect and a projectile trail.

22
00:01:30,280 --> 00:01:36,910
‫Now let's go ahead and double click on p hit effect and you'll see that here we have a particle system

23
00:01:36,910 --> 00:01:37,570
‫editor.

24
00:01:37,570 --> 00:01:43,960
‫And in this little viewport, we'll see this explosion happening over and over again.

25
00:01:43,960 --> 00:01:49,990
‫Now, luckily, this particle system has already been created for us and it looks like a nice little

26
00:01:49,990 --> 00:01:50,830
‫explosion.

27
00:01:50,830 --> 00:01:56,710
‫And we would like to see this explosion happen as soon as the projectile hits something.

28
00:01:56,710 --> 00:01:59,200
‫So that is our goal for this video.

29
00:01:59,200 --> 00:02:04,210
‫So here in V's code, I'd like to go back into our projectile class.

30
00:02:04,210 --> 00:02:11,890
‫So let's go into projectile H and I'm going to add a variable to this class and this is going to be

31
00:02:11,890 --> 00:02:13,480
‫a particle system.

32
00:02:13,480 --> 00:02:18,070
‫Now, this can be a private variable and the type is going to be U.

33
00:02:18,070 --> 00:02:24,280
‫Particle system and it's going to be a pointer and we can call this hit particles.

34
00:02:24,280 --> 00:02:29,140
‫Now I'm going to forward declare this and I'm also going to give this a new property.

35
00:02:29,140 --> 00:02:36,220
‫So let's say you property and I want to make this at it anywhere so we can set this from the blueprint

36
00:02:36,220 --> 00:02:37,720
‫for our projectile.

37
00:02:37,720 --> 00:02:43,750
‫And while I'm at it, I'm going to give this a category and I'll place this in the category combat,

38
00:02:43,750 --> 00:02:48,070
‫since it's sort of related to our other combat variables.

39
00:02:48,070 --> 00:02:54,430
‫Now this is edit anywhere because we plan on setting this from within the blueprint so we can go ahead

40
00:02:54,430 --> 00:02:55,840
‫and compile this.

41
00:02:56,170 --> 00:03:02,710
‫Now let's hop back in the editor and I'm going to close out of my other blueprints and go into our actors

42
00:03:02,710 --> 00:03:04,660
‫folder here in blueprints.

43
00:03:04,780 --> 00:03:07,510
‫Actors, here's our B.P. projectile.

44
00:03:07,510 --> 00:03:11,200
‫Let's open that up and open the full blueprint editor.

45
00:03:11,200 --> 00:03:13,360
‫Now, let's take a look in the details.

46
00:03:13,360 --> 00:03:13,720
‫Panel.

47
00:03:13,720 --> 00:03:19,120
‫Remember, BP projectile self must be selected to see these overall variables.

48
00:03:19,120 --> 00:03:22,420
‫And here's our combat section and we see hit particles.

49
00:03:22,420 --> 00:03:26,500
‫Now here we have a dropdown and we can select particle systems.

50
00:03:26,500 --> 00:03:32,890
‫Since this variable is of type you particle system, we're going to use the hit effect for our hit particles.

51
00:03:32,890 --> 00:03:38,230
‫So now we have a variable and its value is set here in the projectile blueprint.

52
00:03:38,230 --> 00:03:40,750
‫So let's compile and save this.

53
00:03:40,780 --> 00:03:47,770
‫Now, the next step is to actually spawn this particle system as soon as we've hit something, which

54
00:03:47,770 --> 00:03:54,730
‫means on hit is the perfect function to do this in now we need to know how to spawn particles and use

55
00:03:54,760 --> 00:03:55,240
‫gameplay.

56
00:03:55,240 --> 00:03:57,880
‫Statics has a handy function for that.

57
00:03:58,240 --> 00:04:06,130
‫It's called spawn emitter at location and it says plays the specified effect at the given location and

58
00:04:06,130 --> 00:04:07,060
‫rotation.

59
00:04:07,240 --> 00:04:09,460
‫Fire and forget.

60
00:04:09,580 --> 00:04:14,710
‫This is nice because we don't have to worry about removing it or deleting it.

61
00:04:14,710 --> 00:04:17,140
‫We just fire off this function.

62
00:04:17,140 --> 00:04:22,300
‫It spawns the particle system and takes care of cleaning it up for us, which is great.

63
00:04:22,300 --> 00:04:25,210
‫So let's take a look at the syntax now.

64
00:04:25,210 --> 00:04:30,520
‫There are a number of input parameters, but this function has a number of overloads.

65
00:04:30,520 --> 00:04:35,770
‫Now, since there are a number of versions of this function, let's go into V's code and take a look

66
00:04:35,770 --> 00:04:37,240
‫at those different versions.

67
00:04:37,690 --> 00:04:44,050
‫Now I'm going to open up projectile CP and take a look at the on hit function.

68
00:04:44,050 --> 00:04:52,570
‫Now just down here at the bottom I'm going to type you gameplay statics spawn emitter at location and

69
00:04:52,570 --> 00:04:58,210
‫as soon as I type the opening parentheses, it's pop up is showing me the input parameters I need to

70
00:04:58,210 --> 00:04:58,990
‫pass in.

71
00:04:58,990 --> 00:05:03,940
‫But we see one of three, which means there are three different versions of this function.

72
00:05:03,940 --> 00:05:05,260
‫Three Overloads.

73
00:05:05,290 --> 00:05:10,900
‫I'm going to click the down arrow twice to take a look at this particular overload.

74
00:05:10,990 --> 00:05:18,670
‫Now it takes a world context object, a particle system and a location, and the remaining input parameters

75
00:05:18,670 --> 00:05:18,850
‫have.

76
00:05:19,210 --> 00:05:21,460
‫Default values, so they're optional.

77
00:05:21,490 --> 00:05:25,940
‫Notice the rotation parameter is set to zero rotator.

78
00:05:25,960 --> 00:05:33,070
‫Now if we like, we could set the emitters rotation to the rotation of the actor by passing in get active

79
00:05:33,070 --> 00:05:34,030
‫rotation.

80
00:05:34,150 --> 00:05:39,730
‫It's not too important, but I would like to spawn the emitter with the same rotation as the actor.

81
00:05:39,730 --> 00:05:43,510
‫So I'd like to use get active rotation and pass that in.

82
00:05:43,510 --> 00:05:45,580
‫But the rest of these parameters are optional.

83
00:05:45,580 --> 00:05:52,240
‫So since we know we need to call spawn emitter at location to play a particle system, we can do this

84
00:05:52,240 --> 00:05:54,010
‫in the on hit function.

85
00:05:54,400 --> 00:06:00,310
‫We know the first parameter is a world context object and we already know how to handle that.

86
00:06:00,430 --> 00:06:06,100
‫We know that it takes a particle system and we have a particle system variable that we can pass in.

87
00:06:06,130 --> 00:06:13,390
‫We also know that we need a location and a rotation and we know how to get those for a given actor using

88
00:06:13,390 --> 00:06:16,330
‫get actor location and get actor rotation.

89
00:06:16,330 --> 00:06:22,450
‫So since you have all of this information, I'd like you to use the U gameplay statics function spawn

90
00:06:22,450 --> 00:06:27,580
‫emitter at location in the on hit function for the projectile class.

91
00:06:27,580 --> 00:06:34,120
‫So passing this for the world context object, passing the particle system itself, that new hit particles

92
00:06:34,120 --> 00:06:41,230
‫variable we created and use the projectiles location using get active location and use the projectiles

93
00:06:41,230 --> 00:06:44,290
‫rotation using get actor rotation.

94
00:06:44,680 --> 00:06:49,390
‫Pause the video and spawn the emitter in the on hit function now.

95
00:06:53,310 --> 00:06:53,640
‫Okay.

96
00:06:53,640 --> 00:06:59,460
‫So I'm going to remove this line that we used just to take a look at the function overloads for this

97
00:06:59,460 --> 00:07:00,090
‫function.

98
00:07:00,090 --> 00:07:04,080
‫And I'm going to place this just before we destroy the actor.

99
00:07:04,080 --> 00:07:08,010
‫So if you placed it, say, at the end of the function, that's okay.

100
00:07:08,040 --> 00:07:10,320
‫The challenge was left open ended for you.

101
00:07:10,320 --> 00:07:15,600
‫But we're going to place this just before we destroy the actor, because if we destroy the actor, then

102
00:07:15,600 --> 00:07:19,050
‫it doesn't make sense to spawn particles after that.

103
00:07:19,050 --> 00:07:21,570
‫So we're going to do this before we destroy the actor.

104
00:07:21,570 --> 00:07:30,210
‫So we're going to say you gameplay statics spawn emitter at location and first we need our world context

105
00:07:30,210 --> 00:07:32,370
‫object, which is going to be this.

106
00:07:32,400 --> 00:07:39,870
‫Next we need our hit particles followed by our actors location for which we're going to use get actor

107
00:07:39,870 --> 00:07:40,950
‫location.

108
00:07:41,040 --> 00:07:48,870
‫Next we're going to pass in the rotation for which we'll use get actor rotation and now we're spawning

109
00:07:48,870 --> 00:07:50,820
‫the emitter at the location.

110
00:07:50,850 --> 00:07:57,600
‫Now, one thing I'd like to point out is that if my owner is a null pointer and we return out of this

111
00:07:57,600 --> 00:08:02,520
‫function early, well, it might be a good idea to just destroy the projectile there.

112
00:08:02,520 --> 00:08:09,330
‫So I'd like to remove this return statement and give this if statement its own body will still return.

113
00:08:09,330 --> 00:08:13,920
‫But just before we return, we should be destroying the actor.

114
00:08:13,920 --> 00:08:17,160
‫So I'm going to call destroy here in this if check.

115
00:08:17,250 --> 00:08:24,300
‫And in fact, even if we fail all of the other checks here, checking the other actor, we don't want

116
00:08:24,330 --> 00:08:27,240
‫a projectile just lingering around in the world.

117
00:08:27,240 --> 00:08:31,770
‫So we should move this second call to destroy outside the if check.

118
00:08:31,770 --> 00:08:35,670
‫That way, we're destroying the projectile when it hits something, no matter what.

119
00:08:35,670 --> 00:08:39,120
‫That way, we don't have lingering projectiles for any reason.

120
00:08:39,120 --> 00:08:39,680
‫Okay?

121
00:08:39,690 --> 00:08:44,940
‫And while we're being safe, we should notice that we're using hit particles here, which means we should

122
00:08:44,940 --> 00:08:49,800
‫check to see if the hit particles are set before we attempt to use them.

123
00:08:49,800 --> 00:08:56,250
‫Otherwise, we might be using a hit particles variable that has not been set and we should not use null

124
00:08:56,250 --> 00:08:56,700
‫pointers.

125
00:08:56,700 --> 00:09:03,690
‫So we're going to place an if check here to see if hit particles is valid and if it is valid, then

126
00:09:03,690 --> 00:09:07,320
‫it's safe to call spawn emitter at location here.

127
00:09:07,350 --> 00:09:08,280
‫All right.

128
00:09:08,280 --> 00:09:10,380
‫Let's go ahead and compile this now.

129
00:09:10,470 --> 00:09:16,200
‫And with a successful compile, we'll hop back into the editor and here's BP projectile.

130
00:09:16,200 --> 00:09:21,420
‫We have our hit particles set, so make sure that that is set before we test this out.

131
00:09:21,420 --> 00:09:23,010
‫Otherwise, we won't see anything.

132
00:09:23,010 --> 00:09:30,300
‫And if we hit play, we can wait for our go widget and then boom, check that out.

133
00:09:30,300 --> 00:09:32,580
‫We have clouds of smoke.

134
00:09:32,580 --> 00:09:35,880
‫We have hit particles being spawned.

135
00:09:35,880 --> 00:09:42,630
‫I'm going to go ahead and shoot at our enemies and we'll see that they get spawned when we're hitting

136
00:09:42,630 --> 00:09:44,820
‫the enemies as well.

137
00:09:45,450 --> 00:09:46,440
‫Perfect.

138
00:09:46,800 --> 00:09:50,430
‫So in summary, we now have spawned some hit particles.

139
00:09:50,430 --> 00:09:54,780
‫We have explosions when our projectiles hit something in the world.

140
00:09:54,780 --> 00:10:00,540
‫And the next step is to have a smoke trail that follows behind the projectile as it's flying through

141
00:10:00,540 --> 00:10:00,960
‫the air.

142
00:10:00,960 --> 00:10:04,200
‫And we'll take care of that in the next video.

143
00:10:04,740 --> 00:10:05,700
‫I'll see you soon.

