SCRIPTING

Scripting is the code which makes the game function, this page will display code for some of the scripts i have programmed in the game.

The script you see here is called 

playerHealth.js (unity script)

 

This script tracks the player's health and shield values while the game is running ( It does not save to a file )

 

 

var health : int; // in C# this is: Public int health;

var shield : int // in C# this is: Public int shield;

var playerCharacter : GameObject // in C# this is: Public GameObject playerCharacter;

var playerAnimator : Animator // in C# this is: Public Animator playerAnimator;

 

function Update () { // Here is where we keep the data updated when the player's health or shielding change.

 

   if( shield < 1 ){ shield = 0; } // This means if the shield happens to drop below 1, it will now equal 0.

 

   if( health < 1 ){ health = 0; } // If the player's health goes below 1, it now equals 0.

 

   if( health == 0 ) {

   playerAnimator.SetBool("DeathAnimation",true); // If the player's health is now 0, play the death animation from the animator.

   Destroy( playerCharacter, 3 ); 

   }// Since the death animation takes 3 seconds to complete, we'll need to wait that long before we can remove the player from the game.

 

 

}// End of Update function.

 

* In C# function is called void 

* Not all scripts that are written in js will work in C# even though they are similar, the main problem you will run into is the use of the " yield WaitForSeconds ( ); and object.transform.position.xyz -- these are programmed very differently in the game and are not as straight forward to apply for people that are just learning to script.

 

the script above will convert to C# with no problems at all; if you make changes as noted in the script.

 

Since 2017 + Unity has removed the js(UnityScript) from its game engine and has only C# to work with so, anyone that is just now starting to learn how to program may want to download the newer version of unity, and people that have projects consisting of unityScript files may want to either stick with the game engine they currently have, or try their hand at converting their scripts ( Which can be a pain )

 

** Important tips: Try to avoid using static variables in scripts like this, this is because static scripts are accessed anywhere and are affected at the same time ( meaning if you have 2 or more players with static health, and one dies, all other players die as well. )**

 

 

 

 

The script you see here is called 

damagePlayer.js (unity script)

 

This script is attached to an object with a box collider and is marked as trigger. -- The script removes health and shielding from the player if they happen to step into the trigger.

 

 

var damage: int; // in C# this is: Public int damage;

 

function OnTriggerEnter ( other : Collider ){

// In C# this is: void OnTriggerEnter (Collider other){}

 

if(other.tag=="Player"){

// Here we are checking the tag of the playerObject, to make sure the player has entered the trigger.

 

if(other.GetComponent(playerHealth).shield > 1){ // In C# this would be: if(other.GetComponent<playerHealth>().shield > 1)

// Here we are checking the health script that is attached to the player, to see if the shield value is over 1.

 

other.GetComponent(playerHealth).shield -= damage;

// If the player's Shielding is indeed over 1, we can start damaging the shield.

}

 

if(other.GetComponent(playerHealth).shield ==0){

// Here we are checking to see if the player's shield value is 0.

 

other.GetComponent(playerHealth).health-= damage;

// If the shielding does equal 0, then apply damage to the health value;

}

 

}

 

}// End of Trigger function 

 

 

* Trigger Functions are: TriggerEnter(), TriggerExit(), TriggerStay(), 

 

The script you see here is called 

healthPack.js (unity script)

 

This script is attached to an object with a box collider and is marked as trigger. -- The script adds health (and or shielding) to the player when they either run over it, or either interact with it, via a key press on the keyboard.

 

 

var healthGain: int; // in C# this is: Public int healthGain;

var shieldGain: int; // in C# this is: Public int shieldGain;

var removeOnTrigger= false; // in C# this is: Public bool removeOnTrigger=false;

 

function OnTriggerEnter ( other : Collider ){

 

if(other.tag == "Player"){

 

other.GetComponent(playerHealrh).health += healthGain;

other.GetComponent(playerHealrh).health += shieldGain;

 

if(removeOnTrigger == true){

Destroy(gameObject);

}

 

}

 

}// End of Trigger function 

 

 

 

** Notes ** This script can be programmed with a boolean (true or false statement) to either persist through the event, or it can be removed from the game upon triggering the object. 

The script you see here is called 

enemyHealth.js (unity script)

 

This script is attached to an object with a collider. -- The script is very similar to the player's health script except we are adding it on an enemy object.

 

 

var health:int; 

var shield:int;

 

var npcObject:GameObject;

 

function Update(){

 

if( shield < 1 ){ shield = 0; }

 

if( health < 1 ){ health = 0; }

 

if( health == 0 ){ 

 

Destroy(npcObject);

// or if we have a animation, we can play that before removing the game object from the scene.

 

}

 

}