We need to handle collisions between our enemies and ActiveBarriers. Since we have a Rigidbody attached to our Enemy prefab, it will receive the OnTriggerEnter()
event when it hits the collider of an ActiveBarrier GameObject.
Once the collisions with ActiveBarriers are implemented, we'll add collisions with the bottom of the screen, which will reduce the player's health.
First of all, we must disable the ActiveBarrier's collider by default and enable it when the barrier is built in the following manner:
ActiveBarrierController.cs
script attached to it.built
boolean that will help us know if the barrier has finished its building process. Along with our UISlider
and UILocalize
variables, declare the following:private bool built = false;
BuildFinished()
method://Set the build value to true and activate collider built = true; collider.enabled = true;
HitByEnemy()
method with the concerned enemy
passed as a parameter that will destroy the barrier and the enemy in the following manner:public void HitByEnemy(EnemyController enemy) { //If the barrier isn't built, don't go further if(!built) return; //Else, kill the enemy StartCoroutine(enemy.Kill()); //Kill the barrier too StartCoroutine(RemoveBarrier()); }
RemoveBarrier()
coroutine now with the following code snippet:IEnumerator RemoveBarrier() { //Tween for smooth disappearance TweenScale.Begin(gameObject, 0.2f, Vector3.zero); //Notify the Viewport that a Barrier has been removed transform.parent.SendMessage("BarrierRemoved"); //Wait for end of tween, then destroy the barrier yield return new WaitForSeconds(0.2f); Destroy(gameObject); }
The coroutine in the previous code scales down the barrier before it is destroyed. We send a message to the parent (Viewport) because we need to decrease the barrierCount
value.
BarrierRemoved()
method in the ViewportHolder.cs
script. In the Hierarchy view, select our Viewport GameObject and open the ViewportHolder.cs
script attached to it.ViewportHolder.cs
script, add the following new BarrierRemoved()
method:void BarrierRemoved() { //Decrease the barrierCount value barrierCount--; }
barrierCount
value will be updated as soon as a barrier is destroyed. Now, let's open the EnemyController.cs
script and add the Kill()
coroutine as shown in the following code snippet:public IEnumerator Kill() { //Tween for smooth disappearance TweenScale.Begin(gameObject, 0.2f, Vector3.zero); //Deactivate the collider now collider.enabled = false; //Wait end of tween, then destroy the enemy yield return new WaitForSeconds(0.2f); Destroy(gameObject); }
HitByEnemy()
method of the concerned ActiveBarrier when a collision occurs.OnTriggerEnter()
method inside our EnemyController.cs
script, which will call this method only if the collided object actually is a barrier:void OnTriggerEnter(Collider other) { //Store the collided object's ActiveBarrierController ActiveBarrierController barrierController = other.GetComponent<ActiveBarrierController>(); //If it has a BarrierController, call HitByEnemy if(barrierController != null) barrierController.HitByEnemy(this); }
If you place a barrier on an enemy's trajectory, both of them will be destroyed when they collide! If the building process isn't over, nothing happens.
In the case where a barrier finishes its building process while an enemy is still inside it, a collision will occur. Perfect!
Now that the player can destroy his or her enemies, let's add a way for the enemies to destroy the player.
We can now add a collider at the bottom of the viewport's background that will destroy enemies and reduce the player's health. Before we do this, let's create a Healthbar with a HealthController script.
To create this Healthbar, we need the Button.png
file available in the Assets.zip
file. If you haven't added it to the Game Atlas as a sliced sprite yet, please do so before you continue.
We will use a Progress Bar to create a Healthbar on which we will add a HealthController.cs
script to handle the display of damage and health points. Perform the following steps to do so:
Healthbar
.-160
, -30
}.255
, 120
, 120
, 140
}.320
x 42
.6
for slicing parameters.25
, 245
, 255
, 255
}.320
x 42
.Ok, we have a configured health bar centered at the top of the screen. We need to add a script to it that will handle health points and modify the value of Slider accordingly. The steps to do so are as follows:
HealthController.cs
script to it.HealthController.cs
script.In this new script, we will save a static reference to the instance of the HealthController
class so that its methods are easily accessible from other scripts. First, let's declare necessary variables and initialize them on Awake()
as shown in the following code:
//Static variable that will store this instance public static HealthController Instance; //We will need the attached slider and a HP value private UISlider slider; private float hp = 100; void Awake() { //Store this instance in the Instance variable Instance = this; //Get the slider Component slider = GetComponent<UISlider>(); }
Ok, our variables are now initialized correctly. Let's create a Damage()
method that will reduce the hp
value and update the slider as follows:
public void Damage(float dmgValue) { //Set new HP value with a clamp between 0 and 100 hp = Mathf.Clamp(hp - dmgValue, 0, 100); //Update the slider to a value between 0 and 1 slider.value = hp * 0.01f; //If hp <= 0, restart level if(hp <= 0) Application.LoadLevel(Application.loadedLevel); }
Great! The Damage()
method is ready. Let's create the EndOfScreen widget that will collide with the enemies.
Let's create the EndOfScreen widget that will help detect enemy collisions as follows:
EndOfScreen
.3840
, 43
, 1
}.0
, 33
}.DamageZone
tag.255
, G: 120
, B: 120
, and A: 255
.2
.3840
x 43
.Good. We now have an EndOfScreen limit with a sprite and a collider. Now, we need to edit our EnemyController.cs
script's OnTriggerEnter()
method to check if the collided object has the DamageZone tag and hurt the player if needed. Perform the following steps to do so:
EnemyController.cs
script attached to it.EnemyController.cs
script, at the very first line of the OnTriggerEnter()
method, add the following lines to check if the collided object has a DamageZone tag://Is the collided object a DamageZone? if(other.CompareTag("DamageZone")) { //In that case, hurt the player HealthController.Instance.Damage(30f); //Then, kill the enemy and don't go further StartCoroutine(Kill()); return; }
Now, let's add another way to destroy our enemies.
3.147.85.221