Catalogue

Monday, June 20, 2016

Scripting 1: Script: Enemy_Health

Enemy_Health is a script required by every enemy character. Its purpose it to control the enemy HP, taking damage, reacting to damage, spawning and despawning, and holding important variables.

This is an overview of the Enemy_Health Script.

Due to the nature of this script, there are 32 public and 6 private variables. Some variables are public for the sake of customization on a per-enemy basis, others are public so that other attached scripts are able to read them.

7 of the public variables are spawner variables, which get passed on to a spawner object created by the Enemy_Health script on startup. This spawner is responsible for respawning the enemy to which the script is attached.

VOID Start()  Creates the permanent variables.
  • maxEnemyHP = enemyHP; sets both variables to be equal.
  • nightlight = gameobject with "NightLight" tag; This light is used to control colorizer.
  • body = attached Rigidbody2D; This is optional, most enemies do not have a Rigidbody2D.

Colorizer Variables

  • alterGlow and savGlow are reset to uniqueGlow's value.
  • appearance = SpriteRenderer; Saves 2D renderer information for colorizing.
  • if there is no SpriteRenderer: appearance3D = Renderer; Saves 3D renderer information.

Spawner Variables

  • enemySpawned: The enemy to spawn, always set to "this.gameObject";
  • distanceToSpawn: Radius of CircleCast around the spawner. Defaults to 30;
  • infiniteSpawner: A countdown to auto respawn the enemy, does not auto respawn if int == 0;
  • respawnLimits: An int that controls exactly how the enemy will spawn.

respawnLimit meaning:
 0= Regains ability to respawn when the player is far away.
 1= Only respawns when the player dies.
 2= Will keep respawning until the enemy is killed. Useful for minibosses.
 3+= Will not spawn or create a spawner. Useful for event-based enemy spawning.

And finally, the enemy gameObject is always set to be inactive after the first frame.

VOID OnEnable()  This acts as a reseter.
  • enemyHP = maxEnemyHP; Resets HP to maximum.
  • Colorizer(); Resets color.
  • focusPoint is set to active. Useful for minibosses that need the camera to stay still.
  • canBeControlled = false; Resets debugging functionality. 
  • deactivates spawnObjects- spawnObjects are meant to spawn after the enemy dies.
  • reactivates killObjects- killObjects are meant to despawn after the enemy dies.
VOID OnDisable
focusPoint is set to inactive. Optional, focusers tell the camera where to go.

VOID Update ()
  • movRef variables are set. movRef references the enemy's Movement, if attached.
  • if there is no movRef, the hSpeed and fSpeed variables are set instead. These variables Lerp towards 0, and are used to tranform.translate the object. These variables control knockback.
  • finally, countdown variables (hurt, para, etc.) are updated by -0.1f; 
After the initial variables are set, the colorizer function is called.
To override the initial colorizer is the Pain Throb feature. When an enemy's HP falls below 25% they will begin to glow and throb red. After these variables are set, the colorizer is called a second time.

Finally, the dying variables are set. This section reads the HP, and if it's below 0 the enemy begins dying. 
At this point spawnObjects are spawned, and killObjects are despawned.

VOID Colorizer() Changes sprite and model color based on the light and atmosphere.
The Enemy_Health Script contains its own version of the Colorizer script, this is to create the throbbing effect. Enemies can also have a unique glow.

VOID OnTriggerEnter2D(Collider2D other)
If the variable "touchdamage" is greater than 0, the enemy will damage the player on contact. This variable is turned off for almost every enemy in the game.

No comments:

Post a Comment