Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by byurocks23 · Mar 02, 2014 at 06:59 AM · javascriptyieldyield waitforsecondsjavascript-specific

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;
 
 
Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Mar 02, 2014 at 07:01 AM 1
Share

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).

avatar image byurocks23 · Mar 02, 2014 at 07:08 AM 0
Share

I posted the rest of my code

avatar image whydoidoit · Mar 02, 2014 at 07:10 AM 1
Share

So is the problem that things like bikes etc are different to semis ? because that will happen

avatar image whydoidoit · Mar 02, 2014 at 07:17 AM 1
Share

Presu$$anonymous$$g you are moving them using Time.deltaTime - what is the fps stuff about?

avatar image whydoidoit · Mar 02, 2014 at 07:51 AM 1
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges