Start a countdown timer after a collision
Hi guys,
Im adapting the beginner's roll a ball game that is used as a tutorial for Unity,
I currently have a ball rolling around picking up cubes. Ive added two extra cubes that alter the balls speed and physics upon collision. I only want these effects to last for 5 seconds after colliding with the cube, then to go back to normal.
I've tried countless things but nothing seems to work.
Here is my collision code
I've tried using Time.deltaTime but this won't work.
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;//causes the count to increase by one upon collision
setCountText ();
}
if (other.gameObject.CompareTag ("Special Pick Up")) {
other.gameObject.SetActive (false);
speed = speed * 3;
}
if (other.gameObject.CompareTag ("Special Pick Up 2")) {
other.gameObject.SetActive (false);
changeDirection = true;
speed = 5;
}
}
if anyone can help ill love you forever! thanks!!
Answer by MegaManX_777 · Oct 16, 2016 at 07:53 PM
You might want to use InvokeRepeating
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
setCountText ();
}
if (other.gameObject.CompareTag ("Special Pick Up")) {
other.gameObject.SetActive (false);
speed = speed * 3;
InvokeRepeating("CountDown", 1, 1); //Method name, how many times the method should be called, how often (in seconds)
}
if (other.gameObject.CompareTag ("Special Pick Up 2")) {
other.gameObject.SetActive (false);
changeDirection = true;
speed = 5;
InvokeRepeating("CountDown", 1, 1); //Method name, how many times the method should be called, how often (in seconds)
}
}
public void CountDown() {
if (count > 5) {
count++;
}
else {
count = 0;
speed = defaultSpeed;
CancelInvoke(); // Stops all repeating invokes
}
}
Your answer
Follow this Question
Related Questions
How do I create a timer. 1 Answer
Countdown Timer 1 Answer
Simple timer using InvokeRepeating 2 Answers
I want to stop timer when "time left" reach to zero. Any ideas ? 1 Answer
Clock doesn't stop when reaches 0.0. Can you help me with this. 1 Answer