- Home /
Starting/Stopping a Timer On/Off Collision
Hello, I am trying to create a game where you have a ball in a box (Check) And there is another ball in the box except the second ball is fixed, no physics (Check), and now I want to add a piece of code that will allow me have it so that when I jump on the fixed ball with the movable one, a timer will start, and off contact of the ball the timer will stop and it will have a value stored in a variable that I can display, from there I think I can figure out storing best time and what not, thanks (Off/On collision or something? idk how it all works)
PS I would like for the timer to be in Minutes, Seconds and Milliseconds
Ok I keep on not having my questions approved (unapproved!), I suggest you read your mod blocking rules and see that your suppose to give me a answer why it's blocked as I have no idea so will continue to submit until other wise, thanks.
Answer by Basen · Mar 27, 2015 at 05:14 PM
Greetings,
I am still very new to this, but I would do something like this:
float timer = 0;
bool timerCheck = false;
float timerMin = 0;
float timerSec = 0;
float timerMil = 0;
void Update()
{
if(timerCheck)
{
timer += Time.deltaTime;
}
}
void OnCollisionEnter2D(Collider2D col)
{
timerCheck = true;
}
void OnCollisionExit2D(Collider2D col)
{
timerCheck = false;
}
to display the actual minutes seconds and miliseconds, I would do the following: timerMinutes = Mathf.Floor(timer/60); timerSec = Mathf.Floor(timer%60);
Not sure about milisecs... Sorry, hope this helps at least a bit...