STEPS:
1. Created gameObjects - Cube, Sphere, Finish Line. Created UI elements - Canvas then Button & Text.
2. Added raceLogic script to an empty gameObject, raceLogic. Added finishLine script to gameObject, finishLine.
3. Wrote the startGame() method to get the shapes to move. Used transform.position to keep the shapes in the same spot before the race starts. Once "Start Race" button is hit, the shapes' speed were changed from 0 to a positive value and text is changed to "Go!"
4. Wrote the OnTriggerEnter() method for FinishLine. Used the tags to determine wether or not the cube wins or the sphere. Used the same method to change the text to display which shape wins.
5. Altered raceLogic so that if the shapes get to a certain point, they stop so it looks more realistic (can take this out if you need).
SCRIPTS:
RaceLogic
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class RaceLogic : MonoBehaviour
{
  
    public GameObject cube;
    public GameObject sphere;
    public Button startButton;
    public TMP_Text text;
    public float cubeSpeed = 0.0f;
    public float sphereSpeed = 0.0f;
    private bool alreadyBegun = false;
    private string startText = "On you marks, get set...";
    private string raceText = "GO!";

    // Start is called before the first frame update
    void Start()
    { 
        startButton.onClick.AddListener(startGame);
        text.text = startText;
    }
    // Update is called once per frame
    void Update()
    {
        cube.transform.position = new Vector3(cube.transform.position.x + cubeSpeed * Time.deltaTime ,
                                              cube.transform.position.y,
                                              cube.transform.position.z);
        sphere.transform.position = new Vector3(sphere.transform.position.x + sphereSpeed * Time.deltaTime,
                                                sphere.transform.position.y,
                                                sphere.transform.position.z);
        //if (cube.transform.position.x > 12.0f || sphere.transform.position.x > 12.0f)
        //{
        //    cubeSpeed = 0.0f;
        //    sphereSpeed = 0.0f;
        //}
        if( cube.transform.position.x > 12.0f)
        {
            cubeSpeed = 0.0f;
        }
        if( sphere.transform.position.x > 12.0f)
        {
            sphereSpeed = 0.0f;
        }
    }
    void startGame()
    {  
        if (!alreadyBegun)
        {
            //start race
            text.text = raceText;
            cubeSpeed = 5f;
            sphereSpeed = 2f;
            alreadyBegun = true;
        }
       
    }

}


FinishLine
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class FinishLine : MonoBehaviour
{
    // Start is called before the first frame update
    public TMP_Text text;
    private bool raceOver;
    void Start()
    {
        raceOver = false;
    }
    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnTriggerEnter(Collider other)
    {
        if (!raceOver)
        {
            if (other.gameObject.tag == "Cube")
            {
                Debug.Log("Cube Wins");
                text.text = "Cube Wins!";
                raceOver = true;
            }
            if (other.gameObject.tag == "Sphere")
            {
                Debug.Log("Sphere Wins");
                text.text = "Sphere Wins!";
                raceOver = true;
            }
        }
    }
}


Back to Top