- Home /
Problem with yield WaitForSeconds (I think) -javascript)
I am creating a top to bottom scroller game where every so many seconds, a enemy is spawned and moves down. Despite the enemies differing lengths, I want the same amount of space between each enemy. The problem is the space isn't the same between each enemy. My code is
yield WaitForSeconds (timeWaitSemi);
I've printed timeWaitSemi to the console and the number is correct so my math isn't the issue. The number is 2.962963. I'm not sure if yield WaitForSeconds rounds that number or what but I can't seem to find the solution. Let me know if you think it might be a different problem and I'll post more code (I only posted what I thought what was relevant).
So here is all my code
Script - MasterScript
#pragma strict
var bike : GameObject;
var car : GameObject;
var semi : GameObject;
var semiLane1: Vector2;
var semiLane2 : Vector2;
var semiLane3 : Vector2;
var bikeLane1: Vector2;
var bikeLane2 : Vector2;
var bikeLane3 : Vector2;
var carLane1: Vector2;
var carLane2 : Vector2;
var carLane3 : Vector2;
function Spawn (what : GameObject, where : Vector2) {
var vehicle : GameObject = Instantiate (what, where, Quaternion.identity);
vehicle.AddComponent("EnemyMovement");
Destroy(vehicle, Variables.travelTime);
}
Debug.Log(Variables.timeWaitSemi);
function enemyFunction () : IEnumerator {
while (true){
var lane: int = Random.Range(1,4);
var vehicleType: int = Random.Range(1,7);
switch (vehicleType){
case 1: //Semi
case 2:
switch (lane){
case 1:
Spawn (semi, semiLane1);
yield WaitForSeconds (Variables.timeWaitSemi);
break;
case 2:
Spawn (semi, semiLane2);
yield WaitForSeconds (Variables.timeWaitSemi);
break;
case 3:
Spawn (semi, semiLane3);
Debug.Log("Semi, lane 3");
yield WaitForSeconds (Variables.timeWaitSemi);
break;
}
break;
case 3: //bike
switch (lane){
case 1:
Spawn (bike, bikeLane1);
yield WaitForSeconds (Variables.timeWaitBike);
break;
case 2:
Spawn (bike, bikeLane2);
yield WaitForSeconds (Variables.timeWaitBike);
break;
case 3:
Spawn (bike, bikeLane3);
yield WaitForSeconds (Variables.timeWaitBike);
break;
}
break;
case 4: //car
case 5:
case 6:
switch (lane){
case 1:
Spawn (car, carLane1);
yield WaitForSeconds (Variables.timeWaitCar);
break;
case 2:
Spawn (car, carLane2);
yield WaitForSeconds (Variables.timeWaitCar);
break;
case 3:
Spawn (car, carLane3);
yield WaitForSeconds (Variables.timeWaitCar);
break;
}
break;
}
}
}
enemyFunction();
Script- EnemyMovement
#pragma strict
iTween.MoveTo(gameObject,{"y": -20, "easetype": "linear","time": Variables.travelTime });
Script - Variables
#pragma strict
static var enemyFps: float = 88.0;
static var followingDistance : float = 20.0;
static var overallDistance: float = 400.0;
static var semiLength: float = 60.0;
static var bikeLenght: float = 6.0;
static var carLength: float = 12.0;
static var overallSemiLength: float = 80.0; //semiLength + followingDistance
static var overallBikeLength: float = 26.0; //bikeLength + followingDistance
static var overallCarLength: float = 32.0; //carLength + followingDistance
static var playerFps: float = 115.0;
static var actualFps: float = playerFps - enemyFps;
static var travelTime: float = overallDistance / actualFps;
static var timeWaitSemi: float = overallSemiLength / actualFps;
static var timeWaitCar: float = overallCarLength / actualFps;
static var timeWaitBike: float = overallBikeLength / actualFps;
It's possibly in the rest of the code. WaitForSeconds isn't going to be rounding (apart to the next frame in which the condition is true - that would cause $$anonymous$$or differences, really $$anonymous$$or though).
So is the problem that things like bikes etc are different to semis ? because that will happen
Presu$$anonymous$$g you are moving them using Time.deltaTime - what is the fps stuff about?
Ok, a bit worried that iTween is the cause here - I'm not sure how that would affect it but any kind of easing would be a worry. I gave up using external tweening for everything apart from UI elements.
Answer by byurocks23 · Mar 02, 2014 at 10:01 PM
The problem is itween. When I did my math I forgot that when you move an object to a certain place, it puts the middle of the object at that point. So each of the object were moving different amounts in the same amount of time, thus they were moving at different speeds and were getting off track.
Your answer
Follow this Question
Related Questions
Function with itself called inside it. (JavaScript) 2 Answers
yield WaitForSeconds(5); IS WAITING FOR EVER 1 Answer
Yield Waitforseconds not working at all 3 Answers
yield WaitForSeconds preventing further actions. 1 Answer
Reload Level on Collision 4 Answers