- Home /
Increasing speed for a few seconds
How would I increase the speed of my character for just a few seconds?
This is what I've got, but it doesn't seem to work.
bool ApplyPickup (PlayerScript playerStatus) { switch (pickupType) { case PickupType.Health: healthPickup(); break;
case PickupType.Speed:
PlayerScript.moveSpeed += 5000;
StartCoroutine("speedPickup");
break;
}
return true;
}
IEnumerator speedPickup () { yield return new WaitForSeconds(3); PlayerScript.moveSpeed = PlayerScript.originalSpeed; }
please edit your question with your moving script in it...
Answer by joedrigon · Oct 25, 2010 at 01:48 AM
You could set a variable to say, 0 for seconds then increment it with Time.time and when it hit say, 3 seconds you could stop the temp speed increase.
Thank you! I tried it out, but it doesn't seem to work. I definitely did it wrong...
Answer by · Oct 25, 2010 at 02:56 AM
Time.time is "the time in seconds since the start of the game".
I assume that speedPickup() is only called when you actually pick up the object, so it's only being called once. If it occurs more than 6 seconds after the start of the game, speedBoost will immediately set the moveSpeed back to the originalSpeed.
What you could do is:
void speedPickup () { AIscript.moveSpeed += 50; yield WaitForSeconds (6); revertSpeed(); }
void revertSpeed () { AIscript.moveSpeed = AIscript.originalSpeed; }
(Preserving original answer and adding edits below)
Have you tried adding debug printouts to your code? I use this to debug that the code is being executed when/where I'd expect.
bool ApplyPickup (PlayerScript playerStatus) { print("ApplyPickup() called"); // ApplyPickup() has been called switch (pickupType) { case PickupType.Health: healthPickup(); break;
case PickupType.Speed:
print("Speed pickup found"); // found pickup of Enum type 'Speed'
PlayerScript.moveSpeed += 5000;
StartCoroutine("speedPickup");
break;
}
return true;
}
IEnumerator speedPickup () { print("speedPickup() running"); // speedPickup() Coroutine running yield return new WaitForSeconds(3); PlayerScript.moveSpeed = PlayerScript.originalSpeed; print("moveSpeed reverted to original"); // reverted the move speed }
If all 4 messages print out when you pickup the speed boost, then your problem is with the application of your moveSpeed to your player movement. Otherwise, seeing which messages do and don't appear will help track down the problem.
The Invoke function you provided only seems to work when I put it in the Awake function, but not when I put it in speedPickup. speedPickup() is called in a Switch statement, not sure if that would be the reason why it's not working.
I have limited experience with 'Invoke', so I've updated it to a more familiar method. If this doesn't work, could you perhaps update your question with the code that calls 'speedPickup' too?
Hmm.. I totally thought that that would work, but it didn't... I've provided my updated code. (I changed "AIscript" to "PlayerScript" and tweaked a little bit of your code so that it was C# friendly)
I would've thought so too! Not to worry, there's more debugging that we can do. See my updated answer, and please comment back with the results!
Good idea! I've ended up with only "ApplyPickup() called", "Speed pickup found", and "speedPickup() running" showing, in that order.
Your answer
Follow this Question
Related Questions
How To Add A Simple Speed Boost On Collision With A Specific Object With A Character Controller 0 Answers
Objects moving faster after time 1 Answer
Increase ball speed by time 1 Answer
Help with my "speeding up time" script 1 Answer
When I use rigidbody.Moveposition, the object doesn't move accurately. 1 Answer