- Home /
How can I stop a stopwatch using "on trigger enter"?
Hi, I'm new to Unity and I'm still learnig. I made a little level where a ball has to reach the end before the timer reach the 0. When the player reach the end a win screen appear and show you the score you have made and your time to complete the level. What I want to know is: How can I stop the time in the winscreen after the player reached the end?
,Hi, I'm new to Unity and I'm still learnig. I made a little level where a ball has to reach the end before the timer reach the 0. When the player reach the end a win screen appear and show you the score you have made and your time to complete the level. What I want to know is: How can I stop the time in the winscreen after the player reached the end?
Answer by AaronBacon · Jun 25, 2020 at 03:30 PM
Would be helpful if you showed the scripts that handle the timer, but one simple way to do it would be to have a collider on the GameObject at the ending that is set to be a trigger, and also tag it (the preset "Finish" tag sounds like a good fit):
Then, on the Player, you just do a function like this:
void StopFuction()
{
// put some code to stop the clock from counting up
// could be just setting a boolean that the clock will check
}
void OnTriggerEnter(Collider coll) // "coll" will now refer to the object you collided with
{
if(coll.CompareTag("Finish")) // Check if the collider is tagged "Finish"
{
StopFunction();
}
}
Of course you can do this the other way round too, tagging the player as "Player" and instead have the Ending collider have a script on it that waits for a collision with something tagged "Player".
Like I said, hard to do without seeing your code, but that's how you could generally do it.
Thanks for the good advices I've tried to write the code you've posted but it didn't work (I think because I miss a reference somewhere) So I'll try to share the script. The stopwatch script is literally made by copying and pasting the countdon script and I've changed the timeStart -= Time.deltaTime with the plus:
Stopwatch script:
public class stopwatch : $$anonymous$$onoBehaviour
{
public float timeStart = 0;
public Text$$anonymous$$eshProUGUI textBox;
private bool isGameStarted = false;
private bool gameEnded = false;
// Start is called before the first frame update
void Start()
{
textBox.text = timeStart.ToString();
}
// Update is called once per frame
void Update()
{
if (gameEnded == false)
{
timeStart += Time.deltaTime;
Debug.Log(timeStart);
textBox.text = $$anonymous$$athf.Round(timeStart).ToString();
}
if (timeStart < 0)
{
timeStart = 0;
isGameStarted = false;
gameEnded = true;
}
if (gameEnded == true)
{
FindObjectOfType<Gamemanager>().EndGame();
}
}
End trigger script:
public class Endtrigger : $$anonymous$$onoBehaviour
{
public Gamemanager Gamemanager;
public GameObject Scermatadicountdown;
private void OnTriggerEnter(Collider other)
{
Scermatadicountdown.GetComponent<countdown>().enabled = false;
Gamemanager.Completelevel();
}
}
Answer by Darkmatter520 · Jun 25, 2020 at 03:36 PM
You can use trigger events like this:
private void OnTriggerEnter(Collider other) {
// check if the Collider is the Ball
if (other.gameObject.name == "Ball, or whatever it is called in the editor") {
// stop the timer
}
}
Let me know if you need more help!