Here is the speed-run so you can finish the game. =)
Create a 3D object (it can be any basic shape).
Create a material to change its color or download an image from the internet.
On the collider check "Is Trigger"
Add Rigidbody and check "Is Kinematic"
Create a sciprt called "Collect" and add the following code to it (Copy and paste).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collect : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
// Call the GameManager's method to handle collection
GameManager.instance.CollectItem();
// Destroy the collectable
Destroy(gameObject);
}
}
}
Attach the script to the 3D object.
Duplicate the object so that you have 5 of them and place them around your track.
Create a new script called "GameManager" and add the following code:
using UnityEngine;
using TMPro;
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public int totalCollectables = 5;
public int collectedCount = 0;
public TextMeshProUGUI collectedText;
public Timer timer; // Reference to the Timer script
private void Awake()
{
// Ensure there is only one instance of GameManager
if (instance == null)
{
instance = this;
}
else
{
Destroy(gameObject);
}
}
public void CollectItem()
{
collectedCount++;
collectedText.text = "Collected: " + collectedCount + "/" + totalCollectables;
if (collectedCount >= totalCollectables)
{
// Stop the timer
timer.StopTimer();
}
}
}
Create one more script called "Timer" and add the following code:
using UnityEngine;
using TMPro;
public class Timer : MonoBehaviour
{
public TextMeshProUGUI timerText;
private float startTime;
private bool isRunning = true;
private void Start()
{
startTime = Time.time;
}
private void Update()
{
if (isRunning)
{
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString("00");
string seconds = (t % 60).ToString("00.00");
timerText.text = minutes + ":" + seconds;
}
}
public void StopTimer()
{
isRunning = false;
}
}
Right click in the hierarchy and select "UI" an then "Text Mesh Pro"
Rename the "Text (TMP)" to "Collected Text"
Right click in the hierarchy and select "UI" an then "Text Mesh Pro" again
Rename it to "Timer"
You can change the color, font, and location of the text in the inspector.
In the hierarchy, right click and "Create Empty"
Rename it to Game Manager and drag the game manager script to it.
Drag the collected text form the hierarchy to the collected text in the inspector.
Right click and "Create Empty" again.
Rename to "timer"
Attach the timer script to the timer game object.
Drag the timer text in the hierarchy to the timer in the inspector.
Go to the game manager game object and drag the timer script to the timer section.
On your car, in the inspector at the top it says "Tag". Select "Player"
Test it out!