Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Time Interval Timing 2d

Discussion in 'Scripting' started by Riskitamu, Oct 14, 2018.

  1. Riskitamu

    Riskitamu

    Joined:
    Oct 14, 2018
    Posts:
    6
    Hi guys....I've recently created an endless runnner game spawning obstacles in editor it is working fine but when i build it the interval between obstacle is different from the editor...Thanks

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;


    public class GameManagerScript : MonoBehaviour {


    public int myScore;
    public Text myScoreGUI;

    public Transform spawn1, spawn2, spawn3, spawn4, spawn5, spawn6, spawn7, spawn8, spawn9, spawn10, spawn11, spawn12, spawn13, spawn14, spawn15, spawn16, spawn17;

    public AudioSource audioSource;

    public float spawntime;

    // Use this for initialization
    void Start () {
    myScore = 0;

    myScoreGUI = GameObject.Find("ScoreText")
    .GetComponent<Text>();

    InvokeRepeating("ObstacleSpawner", 0.5f,spawntime*Time.deltaTime);

    audioSource = gameObject.GetComponent<AudioSource>();
    }


    public void GmAddScore()
    {
    this.myScore++;
    myScoreGUI.text = myScore.ToString();
    audioSource.Play();
    }

    private void ObstacleSpawner()
    {
    int rand = Random.Range(0, 16);
    switch (rand)
    {
    case 0:
    Instantiate(
    spawn1,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 1:
    Instantiate(
    spawn2,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 2:
    Instantiate(
    spawn3,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 3:
    Instantiate(
    spawn4,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 4:
    Instantiate(
    spawn5,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 5:
    Instantiate(
    spawn6,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 6:
    Instantiate(
    spawn7,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 7:
    Instantiate(
    spawn8,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 8:
    Instantiate(
    spawn9,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 9:
    Instantiate(
    spawn10,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 10:
    Instantiate(
    spawn11,
    new Vector2(5.5f, - 3.2f),
    Quaternion.identity);

    break;
    /// case 11
    case 11:
    Instantiate(
    spawn12,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 12:
    Instantiate(
    spawn13,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 13:
    Instantiate(
    spawn14,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 14:
    Instantiate(
    spawn15,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 15:
    Instantiate(
    spawn16,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;

    case 16:
    Instantiate(
    spawn17,
    new Vector2(5.5f, -3.2f),
    Quaternion.identity);

    break;
    }
    }

    public void Update()
    {
    PlayerPrefs.SetInt("yourscore", myScore);
    }

    public void HiScore()
    {
    int savedScore = PlayerPrefs.GetInt("HighScore");
    if(myScore > savedScore)
    {
    PlayerPrefs.SetInt("HighScore", myScore);
    }
    }


    }
     
  2. Riskitamu

    Riskitamu

    Joined:
    Oct 14, 2018
    Posts:
    6
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class GameManagerScript : MonoBehaviour {
    8.  
    9.  
    10.     public int myScore;
    11.     public Text myScoreGUI;
    12.  
    13.     public Transform spawn1, spawn2, spawn3, spawn4, spawn5, spawn6, spawn7, spawn8, spawn9, spawn10, spawn11, spawn12, spawn13, spawn14, spawn15, spawn16, spawn17;
    14.  
    15.     public AudioSource audioSource;
    16.  
    17.     public float spawntime;
    18.  
    19.     // Use this for initialization
    20.     void Start () {
    21.         myScore = 0;
    22.  
    23.         myScoreGUI = GameObject.Find("ScoreText")
    24.             .GetComponent<Text>();
    25.  
    26.         InvokeRepeating("ObstacleSpawner", 0.5f,spawntime*Time.deltaTime);
    27.  
    28.         audioSource = gameObject.GetComponent<AudioSource>();
    29.     }
    30.  
    31.  
    32.     public void GmAddScore()
    33.     {
    34.         this.myScore++;
    35.         myScoreGUI.text = myScore.ToString();
    36.         audioSource.Play();
    37.     }
    38.  
    39.     private void ObstacleSpawner()
    40.     {
    41.         int rand = Random.Range(0, 16);
    42.         switch (rand)
    43.         {
    44.             case 0:
    45.                 Instantiate(
    46.                     spawn1,
    47.                     new Vector2(5.5f, -3.2f),
    48.                     Quaternion.identity);
    49.  
    50.                 break;
    51.  
    52.             case 1:
    53.                 Instantiate(
    54.                     spawn2,
    55.                     new Vector2(5.5f, -3.2f),
    56.                     Quaternion.identity);
    57.  
    58.                 break;
    59.  
    60.             case 2:
    61.                 Instantiate(
    62.                     spawn3,
    63.                     new Vector2(5.5f, -3.2f),
    64.                     Quaternion.identity);
    65.  
    66.                 break;
    67.  
    68.             case 3:
    69.                 Instantiate(
    70.                     spawn4,
    71.                     new Vector2(5.5f, -3.2f),
    72.                     Quaternion.identity);
    73.  
    74.                 break;
    75.  
    76.             case 4:
    77.                 Instantiate(
    78.                     spawn5,
    79.                     new Vector2(5.5f, -3.2f),
    80.                     Quaternion.identity);
    81.  
    82.                 break;
    83.  
    84.             case 5:
    85.                 Instantiate(
    86.                     spawn6,
    87.                     new Vector2(5.5f, -3.2f),
    88.                     Quaternion.identity);
    89.  
    90.                 break;
    91.  
    92.             case 6:
    93.                 Instantiate(
    94.                     spawn7,
    95.                     new Vector2(5.5f, -3.2f),
    96.                     Quaternion.identity);
    97.  
    98.                 break;
    99.  
    100.             case 7:
    101.                 Instantiate(
    102.                     spawn8,
    103.                     new Vector2(5.5f, -3.2f),
    104.                     Quaternion.identity);
    105.  
    106.                 break;
    107.  
    108.             case 8:
    109.                 Instantiate(
    110.                     spawn9,
    111.                     new Vector2(5.5f, -3.2f),
    112.                     Quaternion.identity);
    113.  
    114.                 break;
    115.  
    116.             case 9:
    117.                 Instantiate(
    118.                     spawn10,
    119.                     new Vector2(5.5f, -3.2f),
    120.                     Quaternion.identity);
    121.  
    122.                 break;
    123.  
    124.             case 10:
    125.                 Instantiate(
    126.                     spawn11,
    127.                     new Vector2(5.5f, - 3.2f),
    128.                     Quaternion.identity);
    129.  
    130.                 break;
    131.             /// case 11
    132.             case 11:
    133.                 Instantiate(
    134.                     spawn12,
    135.                     new Vector2(5.5f, -3.2f),
    136.                     Quaternion.identity);
    137.  
    138.                 break;
    139.  
    140.             case 12:
    141.                 Instantiate(
    142.                     spawn13,
    143.                     new Vector2(5.5f, -3.2f),
    144.                     Quaternion.identity);
    145.  
    146.                 break;
    147.  
    148.             case 13:
    149.                 Instantiate(
    150.                     spawn14,
    151.                     new Vector2(5.5f, -3.2f),
    152.                     Quaternion.identity);
    153.  
    154.                 break;
    155.  
    156.             case 14:
    157.                 Instantiate(
    158.                     spawn15,
    159.                     new Vector2(5.5f, -3.2f),
    160.                     Quaternion.identity);
    161.  
    162.                 break;
    163.  
    164.             case 15:
    165.                 Instantiate(
    166.                     spawn16,
    167.                     new Vector2(5.5f, -3.2f),
    168.                     Quaternion.identity);
    169.  
    170.                 break;
    171.  
    172.             case 16:
    173.                 Instantiate(
    174.                     spawn17,
    175.                     new Vector2(5.5f, -3.2f),
    176.                     Quaternion.identity);
    177.  
    178.                 break;
    179.         }
    180.     }
    181.  
    182.     public void Update()
    183.     {
    184.         PlayerPrefs.SetInt("yourscore", myScore);
    185.     }
    186.  
    187.     public void HiScore()
    188.     {
    189.         int savedScore = PlayerPrefs.GetInt("HighScore");
    190.         if(myScore > savedScore)
    191.         {
    192.             PlayerPrefs.SetInt("HighScore", myScore);
    193.         }
    194.     }
    195.  
    196.  
    197. }
     
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I would guess you are getting a different viewport in the editor than when it's full screen on the build. You can simplify your program by using an array:
    Code (csharp):
    1.  
    2. public Transform[] spawns;
    3.  
    Then just put the number of spawns in the editor and it will drop down a menu for you to drag them in on.

    Then use the random number for the index rather than using a switch/case.
     
    AdamRamberg likes this.
  4. AdamRamberg

    AdamRamberg

    Joined:
    Dec 8, 2016
    Posts:
    22
    The problem is probably due to this line:
    Code (CSharp):
    1. InvokeRepeating("ObstacleSpawner", 0.5f,spawntime*Time.deltaTime);
    Why are you using Time.deltaTime here? Should be:
    Code (CSharp):
    1. [code=CSharp]InvokeRepeating("ObstacleSpawner", 0.5f,spawntime);
    I would also suggest to go through and adjust your code according to @fire7side suggestions.
     
    SparrowGS likes this.
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    The delta time on the first few frame fluctuates wildy, probably why your seeing incosistent resualts
     
  6. Riskitamu

    Riskitamu

    Joined:
    Oct 14, 2018
    Posts:
    6
    @AdamRamberg , @SparrowsNest ...........i thought the problem was framerate that's why I added the Time.deltaTime in the InvokeRepeating function, and tried it on different devices it has the same problem....I tried the suggestion of @fire7side , still the same.....Thanks for your responses btw
     
  7. Riskitamu

    Riskitamu

    Joined:
    Oct 14, 2018
    Posts:
    6
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class GameManagerScript : MonoBehaviour {
    8.     public int myScore;
    9.     public Text myScoreGUI;
    10.  
    11.     public Transform[] spawns;
    12.  
    13.     public AudioSource audioSource;
    14.  
    15.     //public float spawntime;
    16.  
    17.     // Use this for initialization
    18.     void Start()
    19.     {
    20.         myScore = 0;
    21.  
    22.         myScoreGUI = GameObject.Find("ScoreText")
    23.             .GetComponent<Text>();
    24.  
    25.         InvokeRepeating("ObstacleSpawner", 0.5f, 12.5f);
    26.  
    27.         audioSource = gameObject.GetComponent<AudioSource>();
    28.     }
    29.  
    30.  
    31.     public void GmAddScore()
    32.     {
    33.         this.myScore++;
    34.         myScoreGUI.text = myScore.ToString();
    35.         audioSource.Play();
    36.     }
    37.  
    38.     private void ObstacleSpawner()
    39.     {
    40.         Instantiate(spawns[Random.Range(0, spawns.Length)], new Vector2(5.5f, -3.2f),Quaternion.identity);
    41.     }
    42.  
    43.     public void Update()
    44.     {
    45.         PlayerPrefs.SetInt("yourscore", myScore);
    46.     }
    47.  
    48.     public void HiScore()
    49.     {
    50.         int savedScore = PlayerPrefs.GetInt("HighScore");
    51.         if (myScore > savedScore)
    52.         {
    53.             PlayerPrefs.SetInt("HighScore", myScore);
    54.         }
    55.     }
    56. }