ACTIVITY 5: Life and Death
Here's the link: HOW TO CREATE A SIMPLE COIN COUNTER IN YOUR UNITY GAME?
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.

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;
}
}
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.
WATCH THE VIDEO BELOW













Comments
Post a Comment