Download the Realistic Rendering project from the Unreal Learn tab.
Open the project we made in Chapter 1
Migrate the VRpawn and PickupCHairBP to the Realistic Rendering project.
Add all the inputs from the CH 1 project.
Updating the BP_Pickup to be any mesh with simple collision.
Replacing meshes in the room with pickups
This project takes place immediately after Project 2. We’ll be using the teleporting movement system and the level opening UI that we made. We’ll simply be adding a gun to the MotionControllerPawn. So first things first open up that project and create a new level by going to File > New Level > Click on Default level.
Create a new folder called “Guns” in the content folder in the content browser. Create a new folder in that called “SmithandWesson” or the name of your gun and import the fbx to the folder, make sure the check box next to skeletal mesh is checked.
Then import your textures into the same folder. Right click on the diffuse version and click create material.
Hold T and left click in the open space twice to create to texture samples. Now the texture is created. Save and close this tab. Open the gun skeleton and add the new material in the material slot. Now you can drag the gun into the scene and increase the scale as needed to see it in the level.
Now import the SW_Fire_ANIM.fbx, SW_OutofAmmo_ANIM.fbx and the SW_Reload_ANIM.fbx. There will be a check box to import the mesh, leave this unchecked because these are animations that will use the SmithandWesson_SKEL as the mesh. Now when you double click on the SmithandWesson_SKEL mesh you can click over to the animation tab and see the three animations in the preview box. Click each one to see the animations play.
Importing the Gun Asset
Attaching the Gun to the BP_MotionController
Now we’ll attach the gun to the BP_MotionController. The mission here is have the gun hidden until the player equips it then make it visible and ready to shoot.
So open the BP_MotionController and in the Add Component tab find the HandMesh and select it so it’s highlighted. Now click on Add Component and add a skeletal mesh. Type in SmithandWesson_SKEL to attach the gun to the hand mesh. Now wherever the player moves the hand the gun will follow. You will want to adjust the size and angle of the mesh.
Set the location to X=16, Y=-5, Z=0
Set the rotation to X=90, Y=0, Z=-130
Make the scale X=2.5, Y=2.5, Z=2.5
Now if you play the level you should see the gun in your hand in a comfortable angle while using the vive motion controllers. You might want to adjust this for different types of controllers.
But we don't want the gun and hand visible at the same time. So go to the details panel with the gun selected and lets check the box that says “Hidden inGame”. This will hide the gun from the player until we tell it to make it visable.
Equiping the Gun
First we need to add the “EquipRWeapon” button input so open the project settings and find the input section. Under the Action Mappings add a new event named EquipRWeapon. Make it fire when the MotionController(R) Grip1 button is pressed.
Back in the BP_MotionController lets create 2 new boolean variables. Name one “HasSmithWessonR” and the other “IsSmithWessonREquiped”. Compile the graph and set the first one to true by checking the box in “HasSmithWessonR” and set the other to false by leaving “IsSmithWessonREquiped” unchecked.
Find an open place in the Event graph and right click and type in “Equip” and you should see the action event “EquipRWeapon” and place that in the event graph. Now create to branch nodes by holding B and left clicking in the event graph. Drag a line from pressed to the first branch and then a line from true to the next branch.
Drag out the bool variable “HasSmithWessonR” to the graph and select “get”. Plug it into the first branch. Do the same for the “IsSmithandWessonREquiped” but plug it in the second branch. Now you should have a simple system that says if the EquipRWeapon is pressed check to see if the player has the Smith and Wesson gun and if thats true check to see if the Smith and Wesson gun is equipped.
Now drag a hand mesh object reference out into the graph and a SmithandWessonSKEL reference to the right of that. Drag from the hand and type “hidden” and create a “SetHiddeninGame” node and then do it again and stack these nodes on top of each other. Do the same for the gun. When branch of “isSmithandWessonEquiped” is true we want to set the hand to visible and hide the gun. When that branch is false we want to hide the gun and make the gun visible. Use the check box next to “New Hidden” in all four “SetHiddeninGame” nodes to create this effect. Make sure the reference objects are plugged in, the hand to the first two and the gun to last two.
Finally drag out the “IsSmithandWessonREquipped” twice and create two “Set” nodes. The one on top will set the boolean to false and the bottom will set the node to true.
Now play in the level to make sure when the right hand motion controller grip 1 is pressed the gun becomes visible and the hand is hidden. And when pressed again the gun is hidden and the hand is visible again.
In the content browser find the Smith and Wesson folder and right click on the SW_Fire_ANIM and create a animation montage. This is like an animation but with more functionality like we can add more animations for it to play in a sequence, add particles and sound effects and things like that.
Once created double click it to open it up and see how it has the Skeleton, Mesh, Animation, Physics tabs at the upper right, click on Skeleton. Now right click the b_SW_Root bone from the Skeleton Tree list on the left. Add a socket to this bone. Name it BulletEjectSocket and set it’s position to the following:
Relative Location X= 3.0, Y= .1, Z= -2.8
Relative Rotation X= -100, Y= .2, Z= 1.0
Relative Scale 0,0,0
Now back in the Animation tab scroll down in the montage tab and where it says Notifies you can right click near the start of the animation on the white striped bar and add a Notify, then select the Play Particle Effect Notify. Name it 45BulletEjection.
Building the Smith and Wesson Anim Montage
Using Infinity Blade Effects
Now if you want a great collection of particle effects for free we can stop here save and quit Unreal. Open the Epic Games Launcher, go to the Marketplace and find the Infinity Blade Effects collection for free download. Once you have that installed in the Library tab you can add it to any project. So add it to this project. If you don’t want the cool effect you can skip ahead to the next part. But I suggest you get this package to start with as it’s license free so you can use it in any thing even if you sell it for money. It was also developed for a mobile game originally so everything is very easy on the CPU and perfect for building in VR.
Once you have it installed open the project back up and find the Infinity Blade Effects folder in the content browser. Now type “P_Smoke” in the search field and duplicate the P_SmokePuff particle effect and name it P_SW_SmokePuff and move it to the SmithandWesson folder under Guns.
Double click it to open the Cascade Editor. This is where we can make a few edits to make this better for a weapon firing effect to come off the gun when you pull the trigger. First click on initial size and make everything in the Max and Min slots 1. In the Lifetime tab make the Min .2 and the Max .5 and next move to the Initial Velocity tab and set the Max to all 10’s and the Min should be -10, -10, 1 to make the smoke puffs better sized and timed for you, feel free to edit these for your gun. And finally to stop it from looping we go to the Spawn Tab under Burst settings hit the + to add a Burst Count and set both the Count High and Low to 1 and then go to the Required tab and scroll down to see Duration settings and set Emitter Loops to 0. Back over in the Smith and Wesson montage tab you can plug this particle event into the Notify slot we made.
Building the Bullet Ejection Particle Effect
To create the Bullet ejection we’ll import a few more things. Right click in the SmithandWesson folder in the content browser and import the WPNT_Ammunition… BaseColor, Normal and RoughnessMetallic. Then create a material off the Base and create texture sample nodes like we did before by holding T and left clicking in the open space. Make one the metallic and roughness texture and plug it in both slots and then make the other the normal texture and plug it into normal.
Now import the WPN_cal_45.fbx and don’t check the skeletal mesh box this time. Open it up and add the material we just created. Then depending on if you already made the Infinity Blade P_SW_Smoke from the previous part open that up. Then right click to the right of the particle emitter and create a new Sprite Particle Emitter.
If you didn't choose to install those effects simply create a new Particle System in the content browser by right clicking and making it from the menu then name it P_SW_SmokePuff and open it up. Now your caught up lets move on.
On the Particle Emitter you can right click and select Type Data, and make a Mesh Data tab. Now you can plug the WPN_cal_45 into the mesh. In the Required tab set the Emitter Duration to 1 and the Loops to 1.
In the bullet Spawn tab create a Burst list with 1 as the Count and Count Low. Make sure the Rate Distribution and the Rate Scale Distribution are both set to none.
In the Lifetime part set the Min to 2 and the Max to 3. And in the Initial Size section set the MAX and Min X,Y,Z’s all to .5 and then in the initial Velocity set the Max X= 10, Y=10, Z=80, and set the Min to X= -10, Y= -10, Z= 80.
Now were going to add some new tabs. Right click to add these in this order, it’s important to note that the order of this list is the order in which the particle is taking insructions so tabs on the bottom will kick in after everything else above it. Create a Acceleration, Const Acceleration, Init Mesh Rotation, and Inish Mesh Rotation Rate.
The Acceleration tabs act like gravity. Set the Acceleration Max to X=200, Y=0, Z= 750, and set the Min to X= 100, Y=0, Z= 540. Set the Const Acceleration to X= 0, Y=0, Z= -800. This will make the bulletts pop up then fall to the ground.
Initial Mesh Rotation you can leave default because we just want the bullet to always start in the same position. But then set the Init Mesh Rotation Rate to Max X=1, Y=1, Z= 5 and set the Min to X= 0, Y= 0, Z= 2. This will give the bullets the slightest spin. Now save this particle effect and go plug it in the Notify Play Particle slot in the SW_Fire anime montage we created earlier.
This particle will plus a little haptic feedback in the controller will give the player a great feeling of immersion and shouldn't be left out of any shooting game.
Pulling the Trigger
Now we’ll setup the events for if the player pulls the trigger then after that we’ll create the haptic feedback and the projectile and then last we’ll make the targets to shoot and some other advanced tidbits like ammo count variables and reloading. It was important to at least make one of the pieces of firing so we get a feed back loop when were testing.
So open up the BP_MotionController and find the blueprints we made to equip the gun. Somewhere in an open space around that area right click and add InputActionGrabRight which is an event that fires when the right motion controller trigger is pulled. Now drop a Get reference from the IsSmithandWessonEquipped bool variable and make a branch so that only if this bool is true we move to then next step.
Now right click and type Play Anim… and select Play Animation (SmithandWessonSKEL) to create the node and the reference at the same time. Select the Fire Montage we created. Plug the branch true arrow into the play anime node. If you test it now, the gun will fire but the teleport and grab functions will still be called so lets go into the MotionControllerPawn and make sure those are blocked when we have the gun equipped.
Open the MotionControllerPawn and find the commented section handle Controller Input. underneath the InputActionGrabRight node drag the RightController object variable into the graph. From that drag out and type IsSmith… and select the IsSmithWessonREquipped bool to drop it on the graph. Next create a branch and from the false arrow drag it to the Grab Actor node that is plugged in to the InputActionGrabRight Pressed Arrow. Leave the True arrow unhooked. And now drag from the InputActionGrabRight Pressed arrow and plug it into the branch. Now the grab function will not be called when the gun is in the hand but when it’s not we can pick things up again.
Below there is a InputActionTeleportRight event. Do the same thing here you can even copy and paste the RightController, branch and IsSmithWessonREquipped bool all next to the event but this time the InputActionTeleportRight Pressed arrow will plug into the branch and the false from the branch will go into ActivateTeleporter.
Right click in the content browser in the SmithandWesson folder to create a new blueprint based off the Actor class. Open it up and add a sphere collision component. Set the radius to 20. Go down to the Collision settings and check the box next to Simulation Generates Hit Events. Change the collision preset to Physics Actor and now our bullets will interact with our PickUp blueprints we made in the earlier chapters!
Next drop a static mesh component under that and add the 45 cal bullet mesh to the slot. Set the scale to 1.5 and set the rotation of Z to -90.
Next add a point light to the sphere collision and set the color to an orangish shade and set the radius to 500. Now when you shoot the bullet will make a cool flash and light up the surrounding area.
Now go to the add component and add Projectile Movement. This is obviously where the movement comes into play. Set the Initial Speed and Max Speed to 2000. Change the Projectile Gravity Scale to 0 so the bullet doesn't drop. You could set it at like .2 if you want a slight drop or add arc to the projectile. However I would only do this for a bow and arrow projectile.
Now if you installed the Infinity Blade Effects you can add a cool fire trail to the bullets. if not skip to the next part. In the Infinity Blade Effect folder search for P_FireBall_Base and right click on it to duplicate it. Move the copy to the Smith and Wesson folder. Go into the cascade editor for the particle and click the red box under Point and Bright Blood. That will turn those effects off leaving only the FireTail and Mesh Data emitter on. Now go back to the projectile and add a particle emitter to the ShereCollision and select the P_FireBall_Base1 you just made. You’ll have to set the rotate to 90 in Z and set the Scale to .1 in X, Y and Z.
Creating the Bullet Projectile
Bullet Projectile Event Graph
In the Bullet Projectile Event Graph create an event on begin play and then hold D and left click to create a delay node. Set the node to .5 and then drag out and create a DestroyActor node. Plug the begin play into the delay and the delay into the destroy actor. This will make sure the projectile is destroyed and unloaded from the level a half second after it’s fired.
Under the event begin play node right click and search for the Event Hit node. Then create a Branch node and then create a Add Impulse at Location node after that. Plug the event hit into the branch and then from the true arrow drag out and attach to the add impulse event. Drag out from the Impulse arrow and plug into destroy actor. Now if the bullet hits something before the half second it’s going to fire an impulse moving physics objects and destroy itself.
From the Event Hit node drag out from Other Comp and plug it into the Target slot on the add impulse node. This makes sure the impulse is fired on the component the bullet hits. Now drag out from the Other Comp and right click and type NotEqual and place a NotEqual node. Under that create a GetPlayerCharacter node and plug that into the other blue slot in NotEqual node. Plug the result of the NotEqual node into the Branch after EventHit. This is will make sure the bullet doesn't hit the player character and cause a bug.
Next create the GetVelocity and GetActorLocation nodes. Drag out from the GetVelocity and hit the * key to find the Vector * int node. Type in 50 as the int to multiply the vector by. Then plug the result of that node into the Impulse slot in the Impulse at Location node. Plug GetActorLocation into the Location slot of the impulse at location node.
Firing The Projectile from the BP_MotionController
Next go back into the BP_MotionController so we can add the code that fires the projectile. We’ll first need to add an “Arrow” component to the SmithandWessonSKEL mesh. This is where we’ll get our vectors so the projectile shoots strait out of the gun. The arrow is attached to the gun mesh so where ever the gun moves the arrow will follow.
Set the location of the arrow to X= -11, Y= 0, Z= 2.2. Set the rotation to X= -180, Y= 0, Z= 180. Now the arrow should be facing right down the barrel of the gun. Set the scale to .3 in X, Y, and Z. Just so the arrow isn’t huge in the viewport.
Back in the event graph under the InputActionGrabRight event drop a reference to the arrow. Drag out of the arrow and make a GetWorldTransform. Drag from that and get a BreakTransform. Drag from the Location slot and create a MakeTransform node. Then drag from the Rotation slot and plug that into the same MakeTransform but to the Rotation slot. Type 1 for X, Y, Z in the scale. This is so the projectile doesn’t take the .3 scale values from the arrow that is hidden in the game and comes out at the same scale we made it in the projectile blueprint.
Last create a SpawnActor from Class node and use our SW_Projectile and drag the return value from the MakeTransform node into the SpawnTransform slot in the Spawn Actor class.
Now if you drop a few BP_PickupChair blueprints into the shooting range level you can test play the level and shoot the chairs! They should fly back from the impulse and if you teleport up to them you can also pick them up like before when you switch the gun back to the hand with the grip button.