Search Unity

Making things smoother

Discussion in 'Scripting' started by BubyMB, Feb 4, 2017.

  1. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Hey guys I am using this;
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class DialogueScript : MonoBehaviour {
    7.  
    8.     public Text DialogueText;
    9.     public Text Name;
    10.     public GameObject Dialogue;
    11.     public float maxTime;
    12.     public float minTime;
    13.  
    14.     public int DialogueNumber = 1;
    15.     private bool _isDone = true;
    16.  
    17.     public void addDialogue(string Text, string _Name, int number){
    18.         StartCoroutine (_addDialogue (Text, _Name, number));
    19.     }
    20.  
    21.     IEnumerator _addDialogue(string Text, string _Name, int number){
    22.         DialogueNumber = number;
    23.         Name.text = _Name + ":";
    24.         DialogueText.text = "";
    25.         foreach (char letter in Text) {
    26.             _isDone = false;
    27.             DialogueText.text += letter;
    28.             if (letter != ' ')
    29.                 yield return new WaitForSecondsRealtime (Random.Range (minTime, maxTime + 1));
    30.         }
    31.  
    32.         if (DialogueText.text == Text) {
    33.             _isDone = true;
    34.             DialogueNumber++;
    35.             if (isDone () && DialogueNumber > 2)
    36.             yield return new WaitForSeconds (1);
    37.             Dialogue.SetActive (false);
    38.         }
    39.     }
    40.  
    41.     public bool isDone(){
    42.         return _isDone;
    43.     }
    44. }
    for my dialogue, it works fine. It's just that when i go under about 0.05 on min and max time, it gets very choppy. I try to go lower but it stays the same speed. Even if i set both min and max to the same, it still varies in time for some reason.
    (offtopic) This also happens when you change Time.timesclae low.

    Is there any way around this or a way to make it smoother
     
  2. mikael_juhala

    mikael_juhala

    Joined:
    Mar 9, 2015
    Posts:
    247
    You're using Random.Range(minTime, maxTime + 1), so even if you set min and max to the same value, there would still be a 1 second range.

    Note that Random.Range for floats is inclusive, meaning that e.g. Random.Range(0.5f, 1.5f) is a range between 0.5 and 1.5. However, Random.Range for integers is exclusive, which means that e.g. Random.Range(0, 3) is a range between 0 and 2.

    It seems you have used the overload for floats in the same way as the one for integers, thus adding 1 to include the max value.

    https://docs.unity3d.com/ScriptReference/Random.Range.html