Here my code increases speed over time, where the speed starts at 0, and reaches till destroyTime. However, I want the speed to be constant for a duration of time, then increase it after a couple seconds.
You could use a simple countdown variable to see when it’s time to change the speed
const float SPEED_INCREASE_INTERVAL = 3f; // how often do you want the speed to change
const float SPEED_CHANGE = 1; // how much do you want the speed to change
float _ItervalTimer = SPEED_INCREASE_INTERVAL;
void FixedUpdate ()
{
if (isBackground == false){
_ItervalTimer -= Time.deltaTime;
if (_IntervalTimer < 0)
{
speed -= SPEED_CHANGE;
Vector2 velocity = new Vector2(speed, 0);
rigidbody2D.velocity = velocity;
Destroy(gameObject,destroyTime);
_IntervalTimer = SPEED_INCREASE_INTERVAL;
}
}
}
EDIT:
To keep spawning objects with increased moving speeds as requested:
You need to have a script that stores the time when the level started. This timestamp needs to be captured before every time you enter a level to play it (don’t put this into every GameObject. Put it in just one)
public static float _TimeSinceStart;
public void StartGame() // call this every time the player enters a level to play it
{
_TimeSinceStart = Time.realtimeSinceStartup;
}
Then you can spawn objects that move faster depending on how long the level has been played:
const float SPEED_INCREASE_INTERVAL = 15f; // how often do you want the speed to change
const float SPEED_CHANGE = -1; // how much do you want the speed to change
const float SPEED_AT_START = -2;
void FixedUpdate ()
{
if (isBackground == false)
{
// how much time has passed since game started
int secondsSinceGameStart = (int)(Time.realtimeSinceStartup - Game._TimeSinceStart);
// how many times has the speed changing interval passed
int interval = secondsSinceGameStart / SPEED_INCREASE_INTERVAL;
// current speed = start speed + speed increase for every interval that has passed
speed -= SPEED_AT_START + (interval * SPEED_CHANGE);
Vector2 velocity = new Vector2(speed, 0);
rigidbody2D.velocity = velocity;
Destroy(gameObject, destroyTime);
}
}
Hi, thank you for the answer. I have tested this and it works well. Is it possible to make the speed of the spawned object the same as the speed at which it was say 15 seconds ago? So the default speed of the spawned obstacles start at 2 and stay at 2 for 15 seconds, then after 15 seconds, it goes up to 3 and whenever the objects are spawned, they all spawn at 3 and not at 2 etc. Im not sure if I explained properly?
@nosekills Hi, Thank you for the updated answer. I have incorporated this into my script but Im not sure what I must do for the 2 errors I have got. 1. error CS0103: The name Game' does not exist in the current context 2. Assets/Scripts/TimeAttackMode.cs(26,29): error CS0266: Cannot implicitly convert type float' to `int'. An explicit conversion exists (are you missing a cast?) What do these mean?
Hi, thank you for the answer. I have tested this and it works well. Is it possible to make the speed of the spawned object the same as the speed at which it was say 15 seconds ago? So the default speed of the spawned obstacles start at 2 and stay at 2 for 15 seconds, then after 15 seconds, it goes up to 3 and whenever the objects are spawned, they all spawn at 3 and not at 2 etc. Im not sure if I explained properly?
– Ilkzz@nosekills Hi, Thank you for the updated answer. I have incorporated this into my script but Im not sure what I must do for the 2 errors I have got. 1. error CS0103: The name
– IlkzzGame' does not exist in the current context 2. Assets/Scripts/TimeAttackMode.cs(26,29): error CS0266: Cannot implicitly convert typefloat' to `int'. An explicit conversion exists (are you missing a cast?) What do these mean?