Saturday, 31 May 2014

Prototype - Adding Shotgun

There is couple of weapons that I would like to add. After adding shotgun I can start working on enemies and gameplay (I should do that btw) but I promise to add weapons first.

What I will do:
- Add shotgun that will fire couple of shells,
- Projectiles will bounce,
- Add damage variables to Shotgun and Reaper,
- Add spread to Shotgun and Reaper,

As I said earlier the game will be about surviving - there will be couple of enemy types and each type will have his weak point, that will motivate player to change weapon during gameplay. 

Add Shotgun and enable projectile bouncing
if you weren't reading earlier post please read how I have added Reaper weapon.

Here's great sci-fi weapon pack from Stronghold Creative
Sci-fi weapon pack from Stronghold Creative. Easy to import to UE4!
You will need to:

  • Add another variable to your WeaponType Enum,
  • Add new socket for Shotgun,
  • Use your ChangeWeapon function to set static mesh and attach weapon to socket,
  • Change Posts Begin Play function to use Shotgun, not reaper.
At this point you will have your Shotgun spawned in game. Create a copy of MyProjectile and name it Shotgun_Projectile. Open it.

1. Break link from Post Begin Play - we will use different settings there,
2. In Event Hit - destroy link to Spawn Emitter and Play Sound,
3. Go to Components,
4. Select Projectile Component,
5. Find projectile bounces category and set it to true, leave values as it,
Basically Epic created bounces for us so you don't even do it by yourself like here:
You can implement bounciness like this but it isn't needed. UE4 can do it for us.

6. Go back to MyCharacter blueprint and implement shoot functionality for shotgun. Just simple spawn Shotgun_Projectile. You can test out bouncing at this point.
Add spread to Shotgun and Reaper
Do the same on both weapons for spawning Projectile translation.
Epic have another way to do this:
I won't use Epic approach (don't have time to copy it), but if you want you can use it.

You should float variables for each weapon - Spread. So we can upgrade this later. Remember that they need to be "-" for example "-0.1" because we ABSing them.

So here's my final function for Spread.

Shotgun - firing couple of shells 
This will be super easy. First create int variable Shells and set it to 6.

1. Create For Loop block before spawning Shotgun Projectile,
2. Assign Shells to LastIndex,
3. Use For Loop to spawn Projectiles as here:
Shotgun functionality is almost ready. There is one problem - projectiles will collide with each other when spawning. You can fix that by:
1. Go to your Shotgun Projectile,
2. Select Collision Component and disable Projectile Collisions like here:

Add Damage - Reaper
Open MyCharacter blueprint and add two int variables: Reaper_MinDamage (default 20) and Reaper_MaxDamage (default 40) add those variables to Weapon_Reaper category. It will be easier to find them later.

Open MyProjectile blueprint (which is Reaper projectile) and add the same variables. Make sure Expose on Spawn is set to true for both of them.

Go back to MyCharacter blueprint and update Spawn Actor MyProjectile block. You will see created variables in MyProjectile. Connect Reaper_MinDamage and Reaper_MaxDamage to it.

In MyProjectile create Rand Range block and assing Min and Max damage variables to it. You can use Print String to check damage variables.
That's all for now - we will use those variables when dealing damage to enemies and when upgrading weapons.

Add Damage - Shotgun
Just do the same like in Reaper. Try by yourself!

So yeah we have an Shotgun functionality ready! Experiment with different settings to get better results. (you should change lifetime of Shotgun Projectile to lower value - you can change that in Defaults -> Initial Life Span.) 
Another thing is that you can limit bounces using int like here (it's event hit from shotgun projectile):

Here's shotgun functionality:

In next post we will be adding effects to it and different recoil. 

Due to refactoring I'm moving to new blog which can be found here:

See you there!