Reaction speed test
Here’s how to build a reaction speed test game with Unity.
Hold and release the button until the indicator is between two numbers.
Sample pictures
C# code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class Controller : MonoBehaviour { public float speed = 50; public int trueRange = 50; public Text minT, maxT, currentText, winORloseText,levelText; public Image againPanel , nextPanel , endPanel, levelPanelBack; Vector3 rotationPoint = Vector3.zero; float temp; int maxWinD; int minWinD; bool hold; int Lnum = 1; // Start is called before the first frame update void Start() { Lnum = PlayerPrefs.GetInt("Level num", 1); trueRange -= (Lnum - 1) * 10; speed *= 1+((float)(Lnum-1)/2); //speed *= Lnum; levelText.text = "speed = " + speed + " L E V E L " + Lnum + " range = " + trueRange; BackColor(); SetRange(); } // Update is called once per frame void Update() { temp = Mathf.Round(transform.rotation.eulerAngles.z); currentText.text = "" + temp; if (hold) { transform.RotateAround(rotationPoint, Vector3.forward, speed * Time.deltaTime); } } void BackColor() { switch (Lnum) { case 1: levelPanelBack.color = new Color32(0, 255,50,255); break; case 2: levelPanelBack.color = new Color32(0, 255, 228, 255); break; case 3: levelPanelBack.color = new Color32(0, 50, 255, 255); break; case 4: levelPanelBack.color = new Color32(160, 0, 255, 255); break; case 5: levelPanelBack.color = new Color32(255, 0, 130, 255); break; } } void SetRange() { int winD = Random.Range((trueRange / 2), 360 - (trueRange / 2)); maxWinD = winD + (trueRange / 2); minWinD = winD - (trueRange / 2); minT.text = "Min = " + minWinD; maxT.text = "Max = " + maxWinD; } void WinCheck() { if (temp <= maxWinD && temp >= minWinD) { winORloseText.text = "WIN"; if (trueRange > 10) nextPanel.gameObject.SetActive(true); else endPanel.gameObject.SetActive(true); } else { winORloseText.text = "LOSE"; againPanel.gameObject.SetActive(true); } } public void StartRotate() { hold = true; } public void EndRotate() { hold = false; WinCheck(); } public void Next() { Lnum += 1; PlayerPrefs.SetInt("Level num", Lnum); SceneManager.LoadScene(0); } public void Again() { SceneManager.LoadScene(0); } public void GotoLevel1() { PlayerPrefs.DeleteAll(); SceneManager.LoadScene(0); } } |