Free Unity3D Script to Handle Weapon Damage to Enemy With Multiple Child Components

Today while programming a Boss for my Super Space Trooper video game in Unity3D I needed to write some scripts that would handle the damage caused to my Boss and some of its child game objects individually.  I want to have particular child objects explode once they are out of power due to weapon impacts from my player’s multiple weapons.

The scripts that I wrote are broken into three parts, all of which can be found below for your use if you wish.

The Three Components

The first component is the receiver.  Basically it receives messages from the child game objects when they’re impacted by the player’s weapon.  Additionally, it accounts for the different amount of damage caused by the different types of player weapons.  Also, each game object can have different damage values as well.  So for example, something made out of glass would be more easily destroyed than something made of metal.

The second component is the message sender, which resides on the child game objects.  It is attached to each game object that damage can be applied to.  It’s what sends the details to the receiver on the parent object.

The third component also resides on the children.  It receives the updates from the parent when damage is to be applied to the child.  It also determines when the game object will explode or do whatever is needed.

Instructions for the Scripts

The first script, “BossDamageReceiver.js” should be attached to the parent object.  The healthTime variable can be removed.  I have it on mine as the Boss’s energy levels can regenerate.  That may not apply to your needs.  If not necessary you can completely remove the Update function.  The totalDamage variable is damage applied to the boss overall as apposed to each individual child game object.  These scripts don’t do anything with totalDamage right now, but you could easily add something.  This script also sends the different explosion types to the children, so you’ll need to assign the explosions to this script (not BossItemHealth), which assigns it in the children.

/* Written by Cassius Adams for the Super Space Trooper video game. http://www.SuperSpaceTrooper.com */
private var totalDamage : float = 100; // Overall boss health
private var tempItem : GameObject;
private var applyDamage : DesertiaBossItemHealth;
 
var smallExplosion : GameObject; // This is pulled by the child item(s) when they are destroyed
var largeExplosion : GameObject; // This is pulled by the child item(s) when they are destroyed
 
var healthTime : float = 0; // enemy regains 1% health every 4 seconds
 
function Start () {
	healthTime = Time.time;
}
 
function Update() {
	if(healthTime <= Time.time) {
		totalDamage += 1; // Add 1% health
		healthTime = Time.time+4; // Every 4 seconds
		totalDamage = Mathf.Clamp(totalDamage,0,100); // No more than 100% health
	}
}
 
function ApplyBossDamage (damage : Array) {
	if(damage[0]) {
                totalDamage -= damage[1];
	        tempItem = damage[0]; // Holds the gameObject of the impacted item
	        applyDamage = tempItem.GetComponent(BossItemHealth); // Get the health script from the impacted item
	        applyDamage.itemHealth -= damage[1]; // Send the amount of damage to the impacted item
	        applyDamage.typeOfExplosion = damage[2]; // Send the type of explosion to the impacted item
        }
}

The second script is “BossDamageSender.js”.  This should be added to each of the child game objects that will be damaged by the player’s weapons fire.  All references to damageArray[1] should be replaced with whatever value you may need.

/* Written by Cassius Adams for the Super Space Trooper video game. http://www.SuperSpaceTrooper.com */
var damageArray : Array = new Array(); // Array to hold impacted GameObject, Damage amount, & Explosion type
 
damageArray.length = 3; // Hard coding the length of the array
 
function OnTriggerEnter (playerWeapon : Collider) {
 
	// First we set up the array with the gameobject, damage amount, and the explosion type so we can send it to the receiver
 
	if(gameObject.name == "Fuel") {
 
		damageArray = new Array();
 
		damageArray[0] = gameObject;
 
		damageArray[1] = 15; // The default damage caused
 
		damageArray[2] = "small"; // The explosion type
 
	} else if(gameObject.name == "GunBody") {
 
		damageArray = new Array();
 
		damageArray[0] = gameObject;
 
		damageArray[1] = 0; // The default damage caused
 
		damageArray[2] = "large"; // The explosion type
 
	} else if(gameObject.name == "GunTurnstile") {
 
		damageArray = new Array();
 
		damageArray[0] = gameObject;
 
		damageArray[1] = 0; // The default damage caused
 
		damageArray[2] = "large"; // The explosion type
 
	} else if(gameObject.name == "GunCache") {
 
		damageArray = new Array();
 
		damageArray[0] = gameObject;
 
		damageArray[1] = 0; // The default damage caused
 
		damageArray[2] = "small"; // The explosion type
 
	}
 
	/* Now that we have the details to send to the receiver, we'll specify the damage caused
 
	by each of the Player's weapons.  It will be included with the already-existing gameObject
 
	that got hit, as well as the explosion type the weapon causes once the impacted item is destroyed.
 
	The explosion type will in turn be sent from the receiver to the impacted object so it know
 
	which explosion to use.  This is all sent below using the sendmessageupwards function */
 
    if(playerWeapon.name == "Player Weapon 1") {
 
    	// Calls the function ApplyDamage with a value of damageCaused
 
		SendMessageUpwards ("ApplyBossDamage", damageArray, SendMessageOptions.RequireReceiver);
 
    } else if(playerWeapon.name == "Player Weapon 2") {
 
    	damageArray[1] = damageArray[1] * 2; // double original damage with this weapon
 
    	// Calls the function ApplyDamage with a value of 5
 
		SendMessageUpwards ("ApplyBossDamage", damageArray,SendMessageOptions.RequireReceiver);
 
    } else if(playerWeapon.name == "Player Weapon 3") {
 
    	damageArray[1] = damageArray[1] * 2; // double original damage with this weapon
 
    	// Calls the function ApplyDamage with a value of 5
 
		SendMessageUpwards ("ApplyBossDamage", damageArray,SendMessageOptions.RequireReceiver);
 
    } else if(playerWeapon.name == "Player Weapon 4") {
 
    	damageArray[1] = damageArray[1] * 5; // damage with this weapon is 5 times original weapon
 
    	// Calls the function ApplyDamage with a value of 5
 
		SendMessageUpwards ("ApplyBossDamage", damageArray,SendMessageOptions.RequireReceiver);
 
    }
 
}

The third script named “BossItemHealth.js” is what tracks the current object’s health.  It also instantiates the explosions when the game object runs out of energy.

/* Written by Cassius Adams for the Super Space Trooper video game. http://www.SuperSpaceTrooper.com */
var itemHealth : float = 100;
var typeOfExplosion : String = "";
var damageReceiver : BossDamageReceiver;
var bossObject : GameObject;
var smallExplosion : GameObject;
var largeExplosion : GameObject;
 
function Start() {
	bossObject = GameObject.Find("Big Boss"); // The name of the parent object
	damageReceiver = bossObject.GetComponent(BossDamageReceiver); // Get the damage receiver script
	smallExplosion = damageReceiver.smallExplosion; // Automatically assigns smallExplosion from damage receiver
	largeExplosion = damageReceiver.largeExplosion; // Automatically assigns largeExplosion from damage receiver
}
function LateUpdate () {
	if(itemHealth <= 0)
		Explode();
}
 
function Explode() {
	// Do explosions instantiation
	if(typeOfExplosion == "small") {
		// Instantiate the small explosion
		var explodeSmall = Instantiate(smallExplosion, transform.position, transform.rotation);
	} else if(typeOfExplosion == "large") {
		// Instantiate the large explosion
		var explodeLarge = Instantiate(largeExplosion, transform.position, transform.rotation);
	}
	Destroy(gameObject);
}

Feel free to use this script set for your project if you’d like.  Providing credit would be nice, but not necessary.  I would however like to see how your project turns out so if you use the scripts be sure to post a comment here! :)

Posted in Javascript JS, Super Space Trooper, Unity 3D, Video Game Development
0 comments on “Free Unity3D Script to Handle Weapon Damage to Enemy With Multiple Child Components
1 Pings/Trackbacks for "Free Unity3D Script to Handle Weapon Damage to Enemy With Multiple Child Components"
  1. […] They now send details through the SendMessageUpwards function and send an array of details (now that I know how) that can be used.  Things such as weapon damage, enemy type that fired it, what the weapon should […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>