- Home /
How to limit an effect to a certain duration
I'm trying to add a basic nitro boost to a car racing game. I want to give each player 5 seconds worth of nitro boost per game, which will double their speed while it's active. I'm an extreme beginner to game programming, however, and im not sure how to put what i want into code.
The basic structure of what i want is:
If C is pushed AND Nitropercent is > 0: increase speed by X decrease Nitropercent by 20*seconds active.
I know if( Input.GetKey( KeyCode.C ) )
is what i want for checking if C is pushed, transform.position += transform.forward * 8.0f * Time.deltaTime;
is what i want to use to increase speed, and multiplying by Time.deltaTime
is how you determine the number of seconds; but im not sure how to put it all together into a cohesive script.
Any help would be appreciated. The project isn't anything fancy, i just wanted to program a game to play with my friends that i could make as practice for using unity.
Answer by pad406 · Dec 29, 2012 at 03:56 AM
I'v just finished implememting something similar in my game, but I allow the user to stop using their acceleration and let it build up again. They can only start using the accelerant when their 'tank' is full.
You'll need to set up a few variables first (c#)
private float m_tank = 0.0F;
private float m_tank_tofill = 30.0F; //No of seconds it takes to fill tank
private float m_tank_usage = 10.0F; //No of seconds it can be used for
private bool m_accelerating; //Are we accelearting?
private float m_accelerateSaveSpeed; //Used to store speed we were at before accelerating
I also have a variable MovementSpeed which holds the current speed I'm travelling at.
In FixedUpdate you'll need to add an if block
if (m_accelerating)
{
m_tank -= (1F/m_tank_usage) * Time.deltatime;
if(m_tank <= 0)
{
m_accelerating = false;
m_tank = 0;
//reset your speed here to what it was before
}
}
elseif (m_tank < 1)
{
m_tank += (1F/M_tank_tofill) * Time.deltatime;
}
What this will do is
If you're accelerating your tank is reduced to 0 over a period of m_tank_usage seconds
If you're just travelling normally your tank will fill up over a period of m_tank_tofill seconds.
Finally in Update check if your accelerate key is pressed. In my case it's CTRL and if it's pressed again the user stops accelerating. Code looks like this;
if (Input.GetKeyDown (KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl))
{
AccelerateMe(); // A seperate function
}
void AccelerateMe()
{
if (m_accelerating) // if we're accelerating then turn it off and reset speed
{
MovementSpeed = m_accelerateSaveSpeed;
m_accelerating = false;
}
else if (m_cur_accel >= 1) //OK to accelerate as we have accumulated enough
{
m_accelerateSaveSpeed = MovementSpeed;
MovementSpeed = MovementSpeed * 2.5F;
m_accelerating = true;
}
else
{
// I play a sound here to indicate that tank is not full
}
}
Have fun!!
Answer by NickCh · Dec 29, 2012 at 02:09 AM
You could call the WaitForSeconds func and then deactivate the "boost" http://docs.unity3d.com/Documentation/ScriptReference/WaitForSeconds.html
Your answer
Follow this Question
Related Questions
How to make this display milliseconds? 3 Answers
DeltaTime Timer wont increment when game is focused 2 Answers
Timer reset 1 Answer
Can anyone tell how can i add 2 seconds to my TIMER from another script ? 2 Answers
timer starts in the menu scene 1 Answer