Sunday, 9 December 2018

Game - Rotating target and door opening

In my Unreal game, I added a series of targets which the player would have to shoot to progress through the level.

I decided to add these targets into my game, because it adds an objective to help the player progress. In some of the commercial games which I researched, I noticed that this 'shooting a target' idea was used frequently, so players might expect this as a mechanic in the level.


How the code works:
I use two different classes during this process. I use the Level Blueprint to allow me to access the target game objects, and I use the individual target blueprints to run the Destroy event.

In the Target Blueprint class, I use an Event AnyDamage to check if the target has taken any damage. If so, then destroy itself (DestroyActor).

In the Level Blueprint, I use an Event Tick to add the rotation onto the targets. These targets are attached to a parent cube (which is an actor, to allow it to move). Each delta second of Event Tick will be multiplied by a rotation speed, changing the rotation on the Z-axis. This is then applied to the actor using AddActorLocalRotation. When the two targets are destroyed, I run the OnDestroyed event to run a custom function - OpenDoor.

The OpenDoor custom method will get the door which I choose to open, and change the object's position (SetActorLocation) and rotation (SetActorRotation).


Level Blueprint Code (use right-click and drag):

*Please note, the OnDestroyed methods in this class would link up to 'Target1' and 'Target2', instead of 'None'.

Target Blueprint Code:


OpenDoor custom method:



Final Mechanic:


What I have learnt:
I have been able to understand that you can use multiple blueprints to create one mechanic. In this mechanic, I used two different Blueprint classes, because I needed to access different game objects within the level, while also changing the state of the game objects (destroying them).

I was happy that I was able to make the targets rotate, as it helped add a bit of a challenge to the gameplay, instead of using a single static target.

If I did this in the future, I would attempt to change the custom method by creating one method, instead of creating two separate methods. I could do this by creating one method, and pass a series of parameters between them. I feel that this is bad coding practice to create two individual methods.