Hi!
I have a Space Shooter, and I want the space ship to increase speed every 10 sec.
Is it possible to get some help with this?
I’m using Javascript
Thanks ![]()
Hi!
I have a Space Shooter, and I want the space ship to increase speed every 10 sec.
Is it possible to get some help with this?
I’m using Javascript
Thanks ![]()
This is just off the top of my head, but try something like this:
function Update () {
yield WaitForSeconds(10) ;
speed += speedIncrease ;
But this just increasing the speed once. Not every 10 sec. Other ideas?
Wrong. As it is in the update function, it is indeed increasing every 10 seconds. After the first increase, the yield WaitForSeconds will be called next frame again.
Why isn’t that doing it every 10 seconds? It’s in the update() section, called every frame.
So every frame, the yeild is called, and it waits 10 seconds, then increments it.
Alternatively, you could declare a float, add Time.deltaTime every update, and when the float is > 10 do your stuff, set float back to 0.
public float f = 0f;
void update()
{
f += Time.deltaTime;
if ( f > 10 )
{
speed += 10;
f = 0;
}
}
Either one should work.
Yield doesn’t work in Update…
Easiest would be to use InvokeRepeating
InvokeRepeating("IncreaseSpeed", 10f, 10f);
function IncreaseSpeed ()
{
//Increase speed here
}
^ This
miksumortti is right. Yield doesn’t work in Update. I should wrote that instead. I wrote wrong. Sorry.
InvokeRepeating("IncreaseSpeed", 10f, 10f);
function IncreaseSpeed ()
{
//Increase speed here
}
But what is the code for increase speed? Sorry, but i’m new to programming.
I know how to set a speed.
If you know how to set speed, then just set it again with some increase-variable line this:
speed += increaseAmount;
Something like that in the IncreaseSpeed function, where speed is the current speed and increaseAmount is a float or integer variable that tells how much to add. Then just set that new speed to be the objects travelling speed. Or you can just put the amount in numbers there if you don’t need to edit it on different objects.
I did this:
var speed : float = 10.0;
var increaseAmount :int = 5;
InvokeRepeating("IncreaseSpeed", 2, 2);
function IncreaseSpeed ()
{
//Increase speed here
transform.Rotate(Vector3.forward * speed * Time.deltaTime);
speed += increaseAmount;
}
The speed is increasing, but it’s only rotating every 2 sec. 2. sec, it rotate, then it stop. 2 sec. It rotate, then it stop. etc.
I made it:
This is the correct code. The rotate line need of course to be in an update function ![]()
var speed : float = 10.0;
var increaseAmount :int = 5;
InvokeRepeating("IncreaseSpeed", 2, 2);
function IncreaseSpeed ()
{
//Increase speed here
speed += increaseAmount;
}
function Update ()
{
transform.Rotate(Vector3.forward * speed * Time.deltaTime);
}
Good to see you solved your problem. Sorry about my dodgy advice, I forgot that yield doesn’t work in Update()