Sunday, 9 December 2018

Game - Shooting enemy turrets

In my Unreal game, I wanted to add a series of enemy targets to shoot at the player. I decided to add these turrets into the game, as it adds a challenge for the player to overcome, and I feel it adds an element of enjoyment and fun.

The enemy turrets are static gameobjects, which perform a series of mechanics using the Sequence function.

  1. Rotates to face the player
  2. Shoots projectiles in the player's direction

Enemy Model:
The enemy model was taken from the Unreal FPS template kit. It is a built in object which felt like a turret-like model. I added a custom red material onto this component to give it a distinct look. The red color was used, as it usually represents a negative connotation.



How the code works:
The Blueprint class works around the On See Pawn (PawnSensing) event, and runs the exact same as the enemy boss class.

If the player is within a close proximity of the enemy turret, then the enemy turret sets the look rotation (using FindLookAtRotation, starting at the turret's location with GetActorLocation, and targets the new rotation to be towards the player's location with GetActorLocation).

To fire the projectile, the Blueprint checks a custom boolean variable (SpawnProjectile) to see if a projectile can be spawned. If not, a projectile will be spawned (SpawnActor), and an impulse (AddImpulse) towards the forward vector (GetForwardVector) multiplied by a large speed will fire it. It will then set the boolean to false and add a delay (Delay).

Just like the boss, the boolean acts as a cool down mechanic to avoid an overload of projectiles to be fired. If there was an large number of projectiles in the scene, it would cause the game to lag, and make it difficult to play.

The boss enemy class extends on this mechanic to perform the movement, which is something I did not plan to include on this.


About PawnSensing:
PawnSensing is a feature within UE4 which allows an actor to use logic to understand the movement of other actors. This is a feature which is a good way to add an AI which could perform a mechanic, such as movement.

PawnSensing can be added as a component onto a game object. It will add a series of lines and shapes onto the viewport of the affected actor. The green shapes in the image below show the 'eyes' or area which the actor will look to find other actors - the distance and radius of this can be adjusted at the developer's preference.



The developer can change different features such as checking for only player-controlled actors (which I have set to true, to avoid enemies interacting with each other) and listening for footstep sounds.

There is a similar feature called AI Perception, which is a newer feature which allows for sensing of actors in the scene.



Blueprint code (right-click and drag):



What I have learnt:
In this mechanic, I was happy to be able to utilize a built-in Pawn Sensing method which I have not encountered within Unity. I found this method to make it easier to add add logic to the enemy characters, and I would happily use the PawnSensing mechanic in the future again. I have been able to understand the PawnSensing mechanic, which I have described above.

During development, I was experiencing an issue relating to the shooting of the projectiles from the enemy point of view, which gave me the error below:

FirstPersonProjectile_C_0.Sphere FirstPersonProjectileMesh has to have 'Simulate Physics' enabled if you'd like to AddImpulse. 

To fix this, I was required to enable Simulate Physics on the Projectile game object details, and change the Collisions Preset to Ragdoll.