Ulmagor
December 5, 2013, 10:45pm
1
So this is my code.
void Update(){
if(speedDirection >= 10f){
speedDirection += 100f;
}else{
++speedDirection;
}
}
How do I make the speedDirection to update at each second or else? Is there some sort of Timer to make the speedDirection increase or decrease by 1 every second?
Sorry for my english…
The easiest solution would be to use InvokeRepeating():
void Start() {
InvokeRepeating("SpeedTest", 0.0, 1.0);
}
void SpeedTest(){
if(speedDirection >= 10f){
speedDirection += 100f;
}else{
++speedDirection;
}
}
If you want to repeat this forever, use InvokeRepeating :
void Start(){
// call IncSpeed in one second and repeat every second:
InvokeRepeating("IncSpeed", 1, 1);
}
void IncSpeed(){
if(speedDirection >= 10f){
speedDirection += 100f;
} else {
++speedDirection;
}
}
Another option is to create your own timer:
float nextTime = 0;
void Update(){
if (Time.time > nextTime){
nextTime = Time.time + 1;
IncSpeed();
}
}
void IncSpeed(){
if(speedDirection >= 10f){
speedDirection += 100f;
} else {
++speedDirection;
}
}