Search Unity

Infinite runner script

Discussion in 'Scripting' started by suctioncup, Sep 13, 2012.

  1. suctioncup

    suctioncup

    Joined:
    May 19, 2012
    Posts:
    273
    Ok,

    Code (csharp):
    1. #pragma strict
    2.  
    3. //Rand is used to randomly select and object
    4. private var rand : int;
    5. //Time is the time between moving the blocks
    6. var time : float = 1;
    7. //Offset is how far the blocks are away from eachother
    8. var offset : float = 5;
    9.  
    10. //Blocks
    11. var blockNormal : GameObject;
    12. var blockCollapse : GameObject;
    13. var blockRocks : GameObject;
    14.  
    15. //Previous block position
    16. private var prevBlock : Transform;
    17.  
    18. InvokeRepeating("Place",0,time);
    19.  
    20. function Update()
    21. {
    22. //Get a random number between 1 and 3 (Inclusive)
    23. rand = Random.Range(1,4);
    24. //Debugging info
    25. Debug.Log(rand);
    26. }
    27.  
    28. function Place()
    29. {  
    30.     //if rand is 1, then move blockNormal
    31.     if(rand == 1)
    32.     {
    33.     blockNormal.transform.position.z = prevBlock.transform.position.z + offset;
    34.     prevBlock.transform.position.z = blockNormal.transform.position.z;
    35.     } else if(rand == 2) //If rand is 2, move blockCollapse
    36.     {
    37.     blockCollapse.transform.position.z = prevBlock.transform.position.z + offset;
    38.     prevBlock.transform.position.z = blockCollapse.transform.position.z;
    39.     } else if(rand == 3) //If rand is 3, move blockRocks
    40.     {
    41.     blockRocks.transform.position.z = prevBlock.transform.position.z + offset;
    42.     prevBlock.transform.position.z = blockRocks.transform.position.z;
    43.     }
    44. }
    The code should randomly select a block from the 3 GameObjects, and then place it in front of the previous block. However, it is not doing anything. Help?

    EDIT: The three game objects are already present in the scene, parented to an empty. That empty has this code.
     
    Last edited: Sep 13, 2012
  2. mrKaizen

    mrKaizen

    Joined:
    Feb 14, 2011
    Posts:
    139
    2 Things:
    1. prevblock is private and not assigned;
    2. do this -_^ :
    Code (csharp):
    1.    
    2.  function Start() {
    3.      
    4.     prevBlock = blockNormal.transform; //maybe wrong
    5.     InvokeRepeating("Place",0,time);
    6.  }
     
  3. suctioncup

    suctioncup

    Joined:
    May 19, 2012
    Posts:
    273
    Thanks. This sort of got it to work,

    Code (csharp):
    1. #pragma strict
    2.  
    3. //Rand is used to randomly select and object
    4. var rand : int;
    5. //Time is the time between moving the blocks
    6. var time : float = 1;
    7. //Offset is how far the blocks are away from eachother
    8. var offset : float = 5;
    9.  
    10. //Blocks
    11. var blockNormal : GameObject;
    12. var blockCollapse : GameObject;
    13. var blockRocks : GameObject;
    14.  
    15. //Previous block position
    16. var prevBlock : Transform;
    17.  
    18. InvokeRepeating("Place",0,time);
    19.  
    20. function Start()
    21. {
    22. //Sets prevBlock to a location so we can start movement
    23. prevBlock = blockNormal.transform; //maybe wrong
    24. }
    25.  
    26. function Update()
    27. {
    28. //Get a random number between 1 and 3 (Inclusive)
    29. rand = Random.Range(1,4);
    30. //Debugging info
    31. Debug.Log(rand);
    32. }
    33.  
    34. function Place()
    35. {  
    36.     //if rand is 1, then move blockNormal
    37.     if(rand == 1)
    38.     {
    39.     blockNormal.transform.position.z = prevBlock.transform.position.z + offset;
    40.     prevBlock.transform.position.z = blockNormal.transform.position.z;
    41.     } else if(rand == 2) //If rand is 2, move blockCollapse
    42.     {
    43.     blockCollapse.transform.position.z = prevBlock.transform.position.z + offset;
    44.     prevBlock.transform.position.z = blockCollapse.transform.position.z;
    45.     } else if(rand == 3)
    46.     {
    47.     blockRocks.transform.position.z = prevBlock.transform.position.z + offset;
    48.     prevBlock.transform.position.z = blockRocks.transform.position.z;
    49.     }
    50. }
    For some reason giant holes start appearing between objects. I understand why this is happening, (Because, for example, '1' is generated a couple of times in a row so that individual block just moves forward. How do I stop this?
     
  4. suctioncup

    suctioncup

    Joined:
    May 19, 2012
    Posts:
    273
    Sorry for bump, but this is really annoying me. I cannot make it so that I can never get a consecutive number, because that would be way to difficult for the player -- so what would I do?
     
  5. f34rtehninja

    f34rtehninja

    Joined:
    Jan 18, 2012
    Posts:
    1
    To get it so that its non consecutive just add a line after you roll for the random number to check if it was one higher than the last one. You'd need a variable for this as well.

    Code (csharp):
    1.  
    2. // At the top
    3. // prevRand is used to hold the previous random number so that we can manage the order of blocks
    4. private var prevRand : int = 0; // starts as 0 so we don't get any weird errors, could be set in start/awake as well.
    5.  
    6. // In your Update Function, replacing what you currently have
    7. // Check to make sure we don't have consecutive blocks, so 1 -> 2 is not okay, 2 -> 3 is not okay.
    8. do
    9. {
    10.    rand = Random.Range(1,4);
    11. } while (rand != prevRand + 1);
    12. prevRand = rand; // Set up prevRand so we can check next update
    13.  
    Should be noted that this can be weird and give you a lot of runs through the loop in a row which can slow you down a little bit. It's not a big deal, you won't be able to notice it likely and if you add more blocks it will repeat the loop less and less. Getting a random number takes no time at all, but I wouldn't do this kind of thing with something more taxing. This will allow 3 -> 1 as well, not sure if you want that or not but you can add an or statement in the while statement to fix it if you want.

    Make sense? This work for you?
     
  6. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    You need to either not select a block that isn't last (meaning, always just bring the last block around to the front), or instantiate multiple copies and clear out blocks that trail out of view.

    Either way, using an array will absolutely make the logic simpler.