Turret Tutorial

UE4Editor 2015-08-28 15-04-53-112

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.

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.

setisdead

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:

targetturret

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.

Components

hierachy

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.

pivot

Create one new float variable named LineOfSightScale – default should be 1. Open Construction Script and add this:

changepivot

Now you can mess with LineOfSightScale float variable and you will see what I mean.

Variables

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),

Functions

GetTurretTargetComponent – pure function

  • Local Variable: Target (TurretTargetComponent),
  • Input: CurrentTargetActor (Actor),
  • Return: TargetComponent (TurretTargetComponent),

getturrettargetcomponent

This is helper function which will return TurretTargetComponent for requested Actor.

FindFirstTarget – pure function

  • Local Variable: Target (Actor),
  • Return: NewTarget (Actor),

FindFirstTarget

This is checking for all overlaping actors if there is actor that have TurretTargetComponent and isn’t dead. If yes – return this actor.

Behavior

First I will focus on idle left right animations.

tick_part_0

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.

tick_part_1

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.

tick_part_2

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.

Firing

Create new custom event named Fire.

fire

Simple: check if we have target – fire with spread, wait, and fire again.

Overlapp

The last thing to make this working is to create OnBeginOverlapp event from TurretLineOfSight Box Collision component.

onbeginoverlap

This event starts everything. Now we have our base and we can start creating Turrets!

Importing Turret

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:

sentrymat

Create new Blueprint extending from BP_BaseTurret. Name it BP_Turret_Sentry.

Components

TurretRotation:

  • Location: (X=0.000000,Y=-0.000046,Z=106.696434)

MuzzleFlashLocation:

  • Location: (X=52.466785,Y=-0.000748,Z=6.361336)

FXMuzzleFlash:

  • 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.

SentryGun

  • 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.

componentsw

Before we move to variables let’s create new Projectile for this Turret.

Turret Projectile

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,

Variables:

  • 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!

Creating ShooterTutorial takes a lot of my free time.
Buy Now Button
If you want you can help me out! I will use your donation to buy better assets packs and you will be added to Credits /Backers page as well.

Implementing game is taking time but writing about it is taking much more effort!

4 thoughts on “Turret Tutorial

  1. Havent started working with your tutorials yet but i will soon.

    Just wanted to let you know that i really appreciate all the hard work you put into this.

    thanks! 🙂

Leave a Reply