Confidentiality Dev Log 2: Enemy Script

This script controls the functionality of the enemy objects in the game. There is a version of this script for each enemy so the script works correctly for each enemy since the script is disabled at the end. The script starts by declaring the variables needed for the script to work. The ‘enemyAnimator’ variable refers to a sprite animator that will be linked outside of the script (it will be the animator on the enemy object the script is connected to). The two ‘GameObject’ variables refer to UI elements that need to be enabled or disabled at different points which are linked to outside of the script. The two ‘Transform’ variables are used to link to empty objects that will be used to generate ‘rays’. The ‘LayerMask’ variable will be used to link to a layer that will be used as the basis for the area that the enemy can patrol. The three float variables are different values that are needed for the script: ‘speed’ refers to the speed of the enemy while ‘distance’ and ‘detectionDis’ are used to determine how far the ‘rays’ created in the script go. The one bool variable is used to determine that the enemy is moving in a direction so the direction can be changed if the end of the patrol area is reached. The ‘private void Start’ part of the script is used to make sure that the ‘rays’ created from the enemy can’t interact with the colliders of the enemy.

This part of the script contains the functionality for how the enemy objects patrol and how they detect the player. For this section of the code, ‘void Update’ is used so that the game will always be checking for these parts to be triggered since they need to happen immediately and also include some input from the player. The first line after ‘void Update’ is used to move the enemy but unlike the Player script no animation code is needed for the walk animation as there are only two animations for the enemies so the walk is the default animation that plays until the other one is triggered. The part after this is used to make the enemy change direction when they reach the patrol area end so they endlessly patrol the area determined by the ‘patrolArea’ layer mask using a vertical ‘Raycast’ as the ‘ray’ will be what looks for the layer that allows the enemy to patrol. The next if statement detects common input that would be used after restarting a level to resume the game after the ‘timeScale’ is changed to 0 by reverting it back to 1. The last part of this section uses a horizontal ‘Raycast’ that looks for the player so the fail function can be triggered. It does this by disabling the UI that the player will see in regular gameplay and then enabling UI that overlays the entire screen while also telling the player that they failed as well as freezing the game by setting the ‘timeScale’ to 0.

This last part of the script is used to allow the player to take down the enemies in the game. It first sets a bool in the enemy animator to true that determines if the take down animation should play and then disables the collider that triggers the take down as well as ending the script so the enemy no longer has any functionality.

This is an alternative version of this script that removes the movement function as there is a stationary enemy in the game that only needs the detect function.