Nonsense not much to say directly on the code
usingUnityengine;usingSystem.Collections; Public classCoroutinedemos:monobehaviour { PublicTransform Targetts; Public BOOLKispress =false; Public BOOLIsmoreonepositon =false; Public BOOLIslessonepositon =false; //Use this for initialization voidStart () {} Public voidBtnkeycheck () {Startcoroutine (Keycheck (KEYCODE.A)); } Public voidCheckpos () {Startcoroutine (Checktransform (Targetts)); } Public voidCheckpostwo () {Startcoroutine (Checktransformtwo (Targetts)); } IEnumerator Keycheck (keycode code) { while(!kispress) { if(Input.getkeyup (code)) {startgame (); Break; } yield return NULL; } Debug.Log ("Game is end"); } voidStartgame () {kispress=true; Print ("startgame!!!!!!!!!!!!!!!!!!!"); } IEnumerator Checktransform (Transform t) { while(!Ismoreonepositon) { if(T.position.x <0.2) {newposition (); Break; } yield return NULL; } Debug.Log ("Checktransform is end"); Ismoreonepositon=false; } voidnewposition () {Ismoreonepositon=true; Print (targetts.position.x+"--newposition!!!!!!!!!!!!!!!!!!!" ); } IEnumerator Checktransformtwo (Transform t) { while(!Islessonepositon) { if(T.position.x >4.9) {newpositiontwo (); Break; } yield return NULL; } Debug.Log ("Checktransformtwo is end"); Islessonepositon=false; } voidNewpositiontwo () {Islessonepositon=true; Print (targetts.position.x+"--newpositiontwo!!!!!!!!!!!!!!!!!!!" ); }}View Code
A total of three collaborative programs
One to detect the user pressed a key on the keyboard event
Two other events that detect the x-coordinate of a cube object greater than 4.9 or less than 0.2, respectively
Give three button click events respectively
Cooperative program fascinated, if there are errors, welcome to correct, thank you!
UNITY3D__ Collaborative Program