Here I have some code that flashes some text quickly up on the screen. It is important that it flashes like it does for aesthetic reasons. However, I’ve had a lot of trouble making it function consistently across computers with different framerates. Obviously, it’s going to look slower on computers with slower framerates; there’s not much we can do to help that, but it’s important that it flashes for at least one frame so that the player has a chance to read it for gameplay purposes. On slower computers, I find the text appears very infrequently or hardly at all. Sometimes the sound plays denoting that the text should flash but it doesn’t render for a visible frame. Why is the timing inconsistent across different framerates? And when it does flash, even though tremendously slower than with a high framerate, why does it sometimes miss rendering? How could these things be remedied? If anyone can shed some light on what I’m messing up I appreciate any help! Thanks!
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.SceneManagement;
public class terminalScreenGlitch : MonoBehaviour {
public string[] possibleTexts;
public Transform inputField;
public bool visible = false;
private Vector3 initPos;
public float xDis = 1;
public float yDis = 1;
public float zDis = 1;
public float jumpiness = 6;
public float changeChance = 3;
public float apearChance = 40;
public int howOftenToTest = 6;
private bool testAgain = true;
public float apearTime = 0.2f;
public bool goNow = false;
void Start(){
initPos = transform.position;
StartCoroutine (testApear());
}
void Update()
{
if (goNow) {
StartCoroutine (forceGo());
goNow = false;
}
if (testAgain) {
testAgain = false;
StartCoroutine(testApear());
}
if (visible) {
if (UnityEngine.Random.Range (0, changeChance) <= 1) {
inputField.GetComponent<TextMesh> ().text = "> " + possibleTexts [Mathf.RoundToInt (UnityEngine.Random.Range (0.0f, possibleTexts.Length - 1))];
}
if (UnityEngine.Random.Range (0, jumpiness) <= 1) {
transform.position = new Vector3 (initPos.x + UnityEngine.Random.Range (-xDis, xDis), initPos.y + UnityEngine.Random.Range (-yDis, yDis), initPos.z + UnityEngine.Random.Range (-zDis, zDis));
}
} else {
inputField.GetComponent<TextMesh> ().text = "";
}
}
IEnumerator testApear(){
yield return new WaitForSeconds (howOftenToTest);
if (UnityEngine.Random.Range (0, apearChance) <= 1) {
visible = true;
GetComponent<AudioSource> ().Play ();
yield return null;
yield return new WaitForSeconds (apearTime);
visible = false;
GetComponent<AudioSource> ().Stop ();
}
testAgain = true;
}
IEnumerator forceGo(){
visible = true;
GetComponent<AudioSource> ().Play ();
yield return new WaitForSeconds (apearTime);
visible = false;
GetComponent<AudioSource> ().Stop ();
}
}