- Home /
Reset speed after a specified time
Im currently creating a 2d side scrolling game. When the character (which is a bike) hits the boost he speeds up, but I cant seem to get him to slow down again. I should point out that the character is actually immobile and the environment around him is moving (I did this because I wanted to create a parallax effect with the background). When he hits the boost, the environment speeds up. The codes something like this:
void OnTriggerEnter(Collider theCollision){
if(theCollision.gameObject.name == "Token Boost") {
Debug.Log ("Hit the item boost!");
Destroy (theCollision.gameObject);
foreach(GameObject go in GameObject.FindGameObjectsWithTag("environment")){
go.GetComponent<parallax>().Speed *= 0.5f;
}
}
If you could help me out id greatly appreciate it
I'm not sure what problem you are trying to solve. This code looks fine other than "*=0.5" seems like it would slow things down. Is this code working? What have you tried to get him to slow down?
The code works fine, it slows it down by half speed, but im wondering how you would reset the speed back to normal after a certain amount of time
Answer by robertbu · Mar 05, 2013 at 03:11 AM
Write a new method that slows it back down..essentially the methods would be identical to the 'foreach' code above, but would would change the speed the opposite way. Then right after the last like line in the code above you insert:
Invoke("SlowBackDown, 5.0f);
Where 'SlowBackDown' is the name of the method, and 5.0 is the time in seconds in the future you want the method to be called.
Your answer
Follow this Question
Related Questions
speed change in standalone compared to editor? 1 Answer
increasing speed and time of spawn game object 0 Answers
Reset speed after a specified time 0 Answers
Help with my "speeding up time" script 1 Answer
Reset a Gameobject to the original state 0 Answers