Skip to Main Content






Unity tutorial

Updating Ball script

We will now add a lives-counter for the ball, every time the ball spawns after falling it substracts a life. When the lives are zero the scene will change to the End Game scene.

 

Copy the following code for the Ball script and save the file:

using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;

public class Ball : MonoBehaviour
{
    private bool jumpKeywaspressed = false;
    private int space_press_count = 0;
    private float horizontal_Input;
    private Rigidbody rigidbody_component;
    [SerializeField] private Transform ground_check;
    [SerializeField] private Transform SpawnPoint;
    public GameObject level1;
    public int platformcounter = 0;
    public int life = 3; //define the max number of lives
    public bool flag = true;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody_component = GetComponent<Rigidbody>();
        level1 = GameObject.Find("Level 1");
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            jumpKeywaspressed = true;

        }
        horizontal_Input = Input.GetAxis("Horizontal");
        if (GetComponent<Transform>().position.y < (level1.GetComponent<Transform>().position.y - 3.0f))
        {
            GetComponent<Transform>().position = SpawnPoint.transform.position;

            //when all three lives are over the scene change to gameover screen
            if (life > 1)
            {
                life = life - 1;
                flag = true; //flag is true so that when the ball respawn it has 3 jump in mid-air 
            }
            else
            {
                SceneManager.LoadScene(2);
            }
        }

    }

    private void FixedUpdate()
    {
        rigidbody_component.velocity = new Vector3(3 * horizontal_Input, rigidbody_component.velocity.y, 0);
        if (Physics.OverlapSphere(ground_check.position, 0.1f).Length == 1)
        {
            if (flag == true)
            {
                if (jumpKeywaspressed && space_press_count < 3 && rigidbody_component.velocity.y < 0)
                {
                    rigidbody_component.AddForce(Vector3.up * 8, ForceMode.VelocityChange);
                    jumpKeywaspressed = false;
                    space_press_count = space_press_count + 1;
                }

                if (jumpKeywaspressed && space_press_count < 3 && rigidbody_component.velocity.y > 0)
                {
                    rigidbody_component.AddForce(Vector3.up * 6, ForceMode.VelocityChange);
                    jumpKeywaspressed = false;
                    space_press_count = space_press_count + 1;
                }

                jumpKeywaspressed = false;

            }
            else
            {
                if (jumpKeywaspressed && space_press_count < 2 && rigidbody_component.velocity.y < 0)
                {
                    rigidbody_component.AddForce(Vector3.up * 8, ForceMode.VelocityChange);
                    jumpKeywaspressed = false;
                    space_press_count = space_press_count + 1;
                }

                if (jumpKeywaspressed && space_press_count < 2 && rigidbody_component.velocity.y > 0)
                {
                    rigidbody_component.AddForce(Vector3.up * 6, ForceMode.VelocityChange);
                    jumpKeywaspressed = false;
                    space_press_count = space_press_count + 1;
                }

                jumpKeywaspressed = false;

            }
        }
        else
        {
            space_press_count = 0;
            if (jumpKeywaspressed)
            {
                flag = false;
                rigidbody_component.AddForce(Vector3.up * 6, ForceMode.VelocityChange);
                jumpKeywaspressed = false;
            }
        }
    }
}

Lives display

Right-click on the Canvas > UI > Text - TextMeshPro

Click on Import TMP Essentials on the TMP Importer window, 

Right-click on the "Text (TMP)" > Rename > "Display2", in the Inspector window:

  • Change the PosX to -640, PosY to 290
  • Change the "New Text" to "Lives:"
  • Check the Bold button in the Font Style
  • Check Color Gradient and drag the "Yellow-Gold" color gradient into the placeholder
  • Check the Underlay box at the bottom
    • Click on "Click to expand" and change the Offset X to 1, Offset Y to -1, Softness to 0.6

(In the Game tab, the text might not appear in the position shown in the video, that is due to a different resolution screen, the final build of the game is going to be the same)

Script for displaying lives

We will now add the lives counter to display with the score.

 

Navigate to the Scripts folder

Right click in the folder > Create > C# Script

Name the script "DisplayLives", click on the script to open it in Visual Studio

Copy the following code into the script and save the file:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

public class Score2 : MonoBehaviour
{
    public TextMeshProUGUI scoreUI;
    public GameObject Ball;
    // Update is called once per frame
    void Update()
    {
        scoreUI.text = "Lives:" + Ball.GetComponent<Ball>().life.ToString();
    }
}

 

Click on the dropdown for Canvas and select Display2

Click on the Scripts folder and drag the DisplayLives script into the inspector window of Display2

  • Drag the "TextMeshPro - Text (UI)" component into the Score UI placeholder
  • Drag Ball from the Hierarchy window into the Ball placeholder