ACTIVITY 5: Life and Death

Yoh guys! nice to see you again here in my 5th blog making games in Unity. So if you're following me and interested in my works, go and check out my previous blog.
Here's the link: HOW TO CREATE A SIMPLE COIN COUNTER IN YOUR UNITY GAME?

So this time, I made a Life and Death Game, How to Gain Life? and How to Loss Life. So without further ado, lets start making one.

Authored By: Jen Carlo Napoto
Developed By: John Ray Vino

The concept of the game is a Billiard Pool Maze with enemies and obstacles in the way. You as a player must find away to go to the hole by making way to obstacles and enemies on your path. Reaching the hole finishes the game.

First things first, we need to make a Project in Unity under 2D, will name it Act5 Life and Death. Then open file in Unity, wait for the UI to appear. Once done, we can now put our sprites in our assets.




This time I used an 8 ball as our Player, on the background is a texture of a Billiard Pool bordered with planks as frame of our game and as barriers for the maze. We also but hearts and skull on the plain which adds and subtract lives. And on top of the plain is 3 lives on board represented by hearts.




So we put first these sprites and obstacles on their places before I start putting its scripts. If done, then were going to put now our PlayerController to our Player. But we have to put OnTriggerEnter2D on our PlayerController so the Player can destroy the skulls and hearts in contact once it is triggered. So remember that all of our objects or sprites must have Collider2d, for our barriers we use BoxCollider2D and those with in trigger was in CircleCollider2D. So I'll show you the code for OnTriggerEnter2D to be put on our PlayerController. Same with our previous game, we merge the 2 scripts together.


Here the script of OnTriggerEnter2D to be put on our PlayerController:


private void OnTriggerEnter2D(Collider2D collision)

    {

        switch (collision.name)

        {

            case "heal1":

                Destroy(collision.gameObject);

                break;

            case "heal2":

                Destroy(collision.gameObject);

                break;

            case "heal3":

                Destroy(collision.gameObject);

                break;

            case "heal4":

                Destroy(collision.gameObject);

                break;

            case "heal5":

                Destroy(collision.gameObject);

                break;

            case "heal6":

                Destroy(collision.gameObject);

                break;


            case "skull1":

                Destroy(collision.gameObject);

                break;

            case "skull2":

                Destroy(collision.gameObject);

                break;

            case "skull3":

                Destroy(collision.gameObject);

                break;

            case "skull4":

                Destroy(collision.gameObject);

                break;

            case "skull5":

                Destroy(collision.gameObject);

                break;

            case "skull6":

                Destroy(collision.gameObject);

                break;

            case "skull7":

                Destroy(collision.gameObject);

                break;

            case "skull8":

                Destroy(collision.gameObject);

                break;

            case "skull9":

                Destroy(collision.gameObject);

                break;

            case "skull10":

                Destroy(collision.gameObject);

                break;

            case "skull11":

                Destroy(collision.gameObject);

                break;

            case "skull12":

                Destroy(collision.gameObject);

                break;

            case "skull13":

                Destroy(collision.gameObject);

                break;

            case "skull14":

                Destroy(collision.gameObject);

                break;

            case "skull15":

                Destroy(collision.gameObject);

                break;

            case "skull16":

                Destroy(collision.gameObject);

                break;

            case "skull17":

                Destroy(collision.gameObject);

                break;

            case "skull18":

                Destroy(collision.gameObject);

                break;

            case "skull19":

                Destroy(collision.gameObject);

                break;

            case "skull20":

                Destroy(collision.gameObject);

                break;

        }

    }

So as shown, all objects that are in trigger are put on here, so it enables the Player to destroy the object in contact.

Next is to put the script for our Heal (Hearts) and Skull. So put these to scripts, although they look identical. they do the opposite to one another. the Heal adds 1 heart and the Skull subtracts 1 heart.



So here's the code to be use in both Heal(Hearts) and Skull:


using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class Heart : MonoBehaviour

{

    private void OnTriggerEnter2D(Collider2D collision)

    {

        GameControllerScript.health += 1;

    }

}

____________________________________________________________________________________________


using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class Skull : MonoBehaviour

{

    private void OnTriggerEnter2D(Collider2D collision)

    {

        GameControllerScript.health -= 1;

    }

}


Once we had put them in place, we must put them on each Heart objects(exept for the lives), and on our Skulls.



Let's move on to the next step, which is the GameController. But before we even do that, we must already have our Hearts on top of the plane.



Then we also need a text that's shows "Game Over!!!" in it. so in the Hierarchy - right click - UI - text, we will put on the canvas as "GameOver" then our text as "GameOver!!!" then set to size 90 and change it to red.



Once done with that, then we can start putting our GameController in the hierarchy named Lives, so you need to make a game object and name it "Lives", then put there this script.



Script for GameObject:

using System.Collections;

using System.Collections.Generic;

using System.Threading;

using UnityEngine;


public class GameControllerScript : MonoBehaviour

{

    // Start is called before the first frame update

    public GameObject heart1, heart2, heart3, GameOver;

    public static int health;


    void Start()

    {

        health = 3;

        heart1.gameObject.SetActive(true);

        heart2.gameObject.SetActive(true);

        heart3.gameObject.SetActive(true);

        GameOver.gameObject.SetActive(false);

       

    }

    void Update()

    {

        if (health > 3)

            health = 3;

        switch (health)

        {

            case 3:

                heart1.gameObject.SetActive(true);

                heart2.gameObject.SetActive(true);

                heart3.gameObject.SetActive(true);

               

                break;

            case 2:

                heart1.gameObject.SetActive(true);

                heart2.gameObject.SetActive(true);

                heart3.gameObject.SetActive(false);

                break;

            case 1:

                heart1.gameObject.SetActive(true);

                heart2.gameObject.SetActive(false);

                heart3.gameObject.SetActive(false);

                break;

            case 0:

                heart1.gameObject.SetActive(false);

                heart2.gameObject.SetActive(false);

                heart3.gameObject.SetActive(false);

                GameOver.gameObject.SetActive(true);

                Time.timeScale = 0;

                break;

        }

    }

}

Then look at the inspector panel and drag the 3 hearts for our Lives, and also the GameOver text from the canvas.

Once done, where almost finish but seems there no objects that are moving except for our player. Well do you remember in our first blog that we put Move scripts to objects to make it move back and forth, up and down, and also to make it rotate on it's axis. Well that's what were looking for. So this time I change the script for or move, because we want the objects to move one specific points and not make it move farther away to where we want it to be. So this script allows as to put specific coordinates as their movement or waypoint. So this script must be put on one by one in those objects that we want to move.



So here's the script for Move:

using UnityEngine;

using System.Collections;


public class Move : MonoBehaviour

{

    public Vector3 pointB;


    IEnumerator Start()

    {

        var pointA = transform.position;

        while (true)

        {

            yield return StartCoroutine(MoveObject(transform, pointA, pointB, 2.5f));

            yield return StartCoroutine(MoveObject(transform, pointB, pointA, 2.5f));

        }

    }


    IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)

    {

        var i = 0.0f;

        var rate = 1.0f / time;

        while (i < 1.0f)

        {

            i += Time.deltaTime * rate;

            thisTransform.position = Vector3.Lerp(startPos, endPos, i);

            yield return null;

        }

    }

}

And this one is for the rotation of object:


using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class rotateLocalAxis : MonoBehaviour

{

    public Vector3 rotationDirection;

    public float smoothTime;

    private float convertedTime = 200;

    private float smooth;

    void Start()

    {

    }

    void Update()

    {

        smooth = Time.deltaTime * smoothTime * convertedTime;

        transform.Rotate(rotationDirection * smooth);

    }

}

Once we had place it on those objects then were done. Make sure to play first the game, you may adjust the movement of objects on Move.cs, then also if the object has Collider 2D or if its indicated in or Player on OnTriggerEnter2D.




So yah hope you followed my instructions. For question you may DM me on my fb Acct: @TrflgrD.WtrLwjr.



WATCH THE VIDEO BELOW







Comments

Popular Posts