In this tutorial I will focus on Turrets. Couple of goals:
- Create base turret blueprint to be able to drive custom turrets,
- Implement idle state and moving state,
- Manage targets,
- Easy way to tell Turret which type of Actors should fire,
Remember the moment from Alien 2 movie with turrets? This will be almost the same.
<strong>This Tutorial has been created using Unreal Engine 4.8.2</strong>.
Make sure you are working on the same version of the engine.
Turret Target Component
Basically some of our enemies aren’t extending from BP_BaseEnemy – for example Spider. That’s why I have decided to create different way to decide if Actor should be targeted by Turret.
I will use scene component for this. Each actor that have this component will be targeted by turret. Additionally component will tell turret where to aim.
Create new blueprint extending from SceneComponent. Name it TurretTargetComponent. Add one bool variable: isDead and one function SetIsDead which will set isDead bool.
That’s all here.
Now open BP_BaseEnemy and add TurretTargetComponent to it. Do the same for every actor that you would like to target with Turret.
Turret need to know if target is alive. That’s why on all of your Die events set isDead bool in TurretTargetComponent. For example in BP_BaseEnemy there is Die function and you could add this here:
Creating base turret blueprint
My plan is to create one base class which turrets will extend from. Please create new Blueprint extending from Actor. Name it BP_BaseTurret.
TurretRotation – Scene Component.
This will be pivot for rotating turret.
MuzzleFlashLocation – Scene Component.
Will spawn projectiles from this location.
FX_MuzzleFlash – Particle System Component.
Make sure AutoActivate is set to False. This will be just muzzle flash effect.
LineOfSightPivot – Scene Component.
Basically when you scale this TurretLineOfSign Box component will scale as well. It will be used for deciding what’s the distance of turret.
TurretLineOfSight – Box Collision:
Will check all overlaping actors with this box.
- Box Extend: (X=500.000000,Y=400.000000,Z=500.000000),
- Location: (X=500.000000,Y=0.000000,Z=0.000000)
- Collision Presets: Custom: Overlap Everything. Make sure Object Type is Pawn,
Why I’m messing with Location here? Because I would like to have LineOfSightPivot scale to update the Box collision.
Create one new float variable named LineOfSightScale – default should be 1. Open Construction Script and add this:
Now you can mess with LineOfSightScale float variable and you will see what I mean.
Open Event Graph and add those variables:
- MaxVerticalRotation (float, default: -10) – yes minus,
- MaxHorizontalRotation (float, default: -60),
- ProjectileClass (Actor Class),
- RotationSpeed (float, default: 3),
- RateOfFire (float, default: 0.2),
- DeltaTime (float),
- CurrentTarget (Actor),
- IdleRotationSpeed (float, default: 1),
- Spread (float, default :5),
GetTurretTargetComponent – pure function
- Local Variable: Target (TurretTargetComponent),
- Input: CurrentTargetActor (Actor),
- Return: TargetComponent (TurretTargetComponent),
This is helper function which will return TurretTargetComponent for requested Actor.
FindFirstTarget – pure function
- Local Variable: Target (Actor),
- Return: NewTarget (Actor),
This is checking for all overlaping actors if there is actor that have TurretTargetComponent and isn’t dead. If yes – return this actor.
First I will focus on idle left right animations.
This should be straightforward for you guys. If turret don’t have target just rotate left and right.
Now if we have an target but the target isn’t dead let’s rotate to target.
The last part is to clamp maximum rotation so you could create turret which can only aim up by 5 angles. This is only looking complicated but it’s really easy.
Now what if we have target but the target is dead? We need to check if there is another target in Box Collision.
I love those wires 😀 Hope Epic will implement States in Blueprints as it was in Unreal Script. It would be much more easier to create this 🙂
So basically it’s checking if something is overlapping with box collisoin – if yes – assign new target.
That’s all in Tick. And that’s almost all behavior of the Turret.
Create new custom event named Fire.
Simple: check if we have target – fire with spread, wait, and fire again.
The last thing to make this working is to create OnBeginOverlapp event from TurretLineOfSight Box Collision component.
This event starts everything. Now we have our base and we can start creating Turrets!
I will use Sentry Guns package.
You should import all meshes without a problem. It’s great package. The only thins missing is design for up/down rotation, but you can live without it. For material editor beginner’s here’s my material for them:
Create new Blueprint extending from BP_BaseTurret. Name it BP_Turret_Sentry.
- Location: (X=0.000000,Y=-0.000046,Z=106.696434)
- Location: (X=52.466785,Y=-0.000748,Z=6.361336)
- Template: P_AssaultRifle_MuzzleFlash (which can be found in Shooter Example)
Add new Static Mesh Component from SentryGun3 static mesh. Attach the component to TurretRotation.
- Location: (X=0.000000,Y=-0.000010,Z=-110.000000)
- Rotation: (Pitch=0.000000,Yaw=-90.000000,Roll=-0.000000)
Add new Static Mesh Component from SentryGunStand1 – don’t need to attach it to anything.
Before we move to variables let’s create new Projectile for this Turret.
Create new blueprint extending from BP_BaseProjectile. Name it SentryProjectile.
Add one new Particle System component from P_AssaultRifle_Tracer_01 which can be found in Military Weapons Silver package.
Projectile Movement Properties:
- Initial Speed: 4000,
- Max Speed: 4000,
- Rotation Follows Velocity: True,
- MinWeaponDamage: 1,
- MaxWeaponDamage: 1,
- CritDamageModifier: 5,
- AmmoData -> Damage: 5,
- Impact Effect: Impact_Pistol,
Final Turret Blueprint
Open back BP_Turret_Sentry again and assing your Projectile.
- ProjectileClass: SentryProjectile,
That’s all guys! As always take your time and play with the blueprints!
Implementing game is taking time but writing about it is taking much more effort!