Search Unity

Objects spawning at regular intervals, create gaps - road generator.

Discussion in 'Scripting' started by PsychoPsam, Sep 11, 2012.

  1. PsychoPsam

    PsychoPsam

    Joined:
    Aug 1, 2012
    Posts:
    34
    I'm working on a random road generator and every n seconds based on the first .js code it moves all the pieces toward the player. The second .js code spawns objects at regular intervals at the specified position every time. Problem is, some gaps appear every now and then. I think it's something to do with the point the object spawn at isn't always exactly the same spot because of varying speeds of CPU's but I'm not sure of the Math to displace it correctly. Anyone know what might be wrong with my code?

    MoveScene.js
    Code (csharp):
    1. #pragma strict
    2. var speed = 0.01;
    3.  
    4. function Start () {
    5.  
    6. }
    7.  
    8. function Update () {
    9.     var blocks : GameObject[] = GameObject.FindGameObjectsWithTag ("Piece") as GameObject[];
    10.  
    11.     for (var block : GameObject in blocks)
    12.     {
    13.         block.transform.position.z += Time.deltaTime * speed;
    14.         if (block.transform.position.z < -5)
    15.             Destroy (block);
    16.     }
    17. }
    ObjectSpawner.js
    Code (csharp):
    1. #pragma strict
    2.  
    3. // time variables
    4. private var cubeCount = 0;
    5. private var currentTime:float = 0;
    6. public var spawnRate:float = 1.0;
    7. public var distance:float = 15;
    8.  
    9. var newObject : Transform;
    10. var newObject1 : Transform;
    11. var newObject2 : Transform;
    12. var newObject3 : Transform;
    13. var newObject4 : Transform;
    14.  
    15. function Awake () {
    16.  
    17.     // create one piece to start with
    18.     CreatePrefab();
    19.    
    20.     // start the timer ticking
    21.     TimerTick();
    22.    
    23. }
    24.  
    25. function Update () {
    26.    
    27.      
    28. }
    29. function CreatePrefab()
    30. {
    31.     var pos : Vector3 = new Vector3(0,0,distance);
    32.     var choice = Random.Range(0,4);
    33.     var obj : Transform;
    34.    
    35.     // tend to paste straights
    36.     var straight = Random.Range(0,4);
    37.    
    38.     if (straight <2)
    39.         choice =5;
    40.        
    41.    
    42.     if (choice == 0 || choice ==5)
    43.         obj = Instantiate(newObject, pos, transform.rotation);
    44.     if (choice == 1)
    45.         obj = Instantiate(newObject1, pos, transform.rotation);
    46.     if (choice == 2)
    47.         obj = Instantiate(newObject2, pos, transform.rotation);
    48.     if (choice == 3)
    49.         obj = Instantiate(newObject3, pos, transform.rotation);
    50.     if (choice == 4)
    51.         obj = Instantiate(newObject4, pos, transform.rotation);
    52.        
    53.     Debug.Log("Road piece created");
    54.     cubeCount ++;
    55.     obj.gameObject.tag = "Piece";
    56. }
    57.  
    58.  
    59. function TimerTick()
    60. {
    61.     // while there are seconds left
    62.     while( currentTime>-1)
    63.     {
    64.         // increase the time
    65.         currentTime++;
    66.          
    67.         CreatePrefab();
    68.        
    69.         // wait for 3 second
    70.         yield WaitForSeconds(spawnRate);
    71.  
    72.      }
    73.      // otherwise game has ended.
    74.      
    75. }
     
    Last edited: Sep 11, 2012
  2. kristijan

    kristijan

    Joined:
    Jul 31, 2012
    Posts:
    2
    on line 32 var choice = Random.Range(0,4); You will get 0,1,2,3 and never 4, so on line 50, if (choice == 4) will never be true, hope this helps a bit