When the player passes though the “WinZone” (Has a box collider with isTrigger checked) I want to stop the timer on the Timer.cs . I don’t know how to use triggers though, and any advice would help!
My Code for Timer
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Internal;
using TMPro;
public class Timer : MonoBehaviour
{
public TextMeshProUGUI timerText;
public TextMeshProUGUI highscore;
private float startTime;
private bool finished = false;
void GameFinished()
{
float t = Time.time - startTime;
if (t < PlayerPrefs.GetFloat("HighScore", float.MaxValue))
{
PlayerPrefs.SetFloat("HighScore", t);
highscore.text = t.ToString();
PlayerPrefs.Save();
}
}
void Start()
{
startTime = Time.time;
highscore.text = PlayerPrefs.GetFloat("HighScore", 0).ToString();
}
void Update()
{
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
if (/* Check if player finished the game */)
GameFinished();
}
}
My Code for WinZone
// This script is responsible for detecting collision with the player and letting the
// Game Manager know
using UnityEngine;
public class WinZone : MonoBehaviour
{
int playerLayer; //The layer the player game object is on
void Start()
{
//Get the integer representation of the "Player" layer
playerLayer = LayerMask.NameToLayer("Player");
}
public void OnTriggerEnter2D(Collider2D win)
{
//If the collision wasn't with the player, exit
if (win.gameObject.layer != playerLayer)
return;
//Write "Player Won" to the console and tell the Game Manager that the player
//won
Debug.Log("Player Won!");
GameMaster.PlayerWon();
}
}