This material and code is all from the 3D Infinite Runner Toolkit Project in the Unity Asset Store
Step Scenario One: Titlescene
1 setting up the start interface scene
This scene is the first screen to open the game, to choose to switch to a formal game scene or a store. Just drag into the main scene model and a role model to add UI resources.
2 adding character animation logic and scene switching logic scripts
The script that will switch is set to a variable name, and then the script that switches the scene can be reused between different buttons
<span style= "White-space:pre" ></span>void Onmouseupasbutton () {this.guiTexture.texture = normal; Application.loadlevel (levelname);}
Scene Two: Shop
1 building the store interface scene
Store scenes are used for character selection and purchase, and of course to extend other functions, such as buying props or something. You can also import the relevant model and add the UI resources.
2 Interface Interaction basic Logic script
Touch (Cursor swipe)
<span style= "White-space:pre" ></span>ienumerator selectdirection () {bool input = false;while (input = = False) {if ((input.mouseposition.x-getmousepos.x) < -40) {indexselect++;if (Indexselect >= players. Length-1) {Indexselect = players. Length-1;} input = true;} if ((input.mouseposition.x-getmousepos.x) > +) {indexselect--;if (indexselect <= 0) {indexselect = 0;} input = true;} if (input.getmousebuttonup (0)) {Input = true;} Yield return 0;}
Character shift
<span style= "White-space:pre" ></span>ienumerator moveToPoint () {while (Vector3.distance ( Transform.position, Point[indexselect]) > 0.01f) {transform.position = Vector3.lerp (transform.position, point[ Indexselect], ten * time.deltatime); yield return 0;} Transform.position = point [Indexselect]; Startcoroutine (Waitinput ());}
Scenario Three: PlayGame
1 building the game initial sceneLoad the initial model into the scene, such as the street, and adjust the diffuse light
2 Adding a game role
Add a rigid body, a collision body, a control script, and an animation controller to a character and save it as a preset. Role control has jump, slide, left shift right and two jumps, respectively, to override the keyboard control and touch control interface.
<span style= "White-space:pre" ></span>ienumerator moveback () {Float z = transform.position.z-0.5f;bool Complete = False;while (complete = false) {transform.position = Vector3.lerp (transform.position, New Vector3 ( transform.position.x,transform.position.y,z), 2*time.deltatime); if ((Transform.position.z-z) < 0.05f) {complete = true;} Yield return 0;} Yield return 0;} private void Moveforward () {speedmove = Gameattribute.gameattribute.speed;if (charactercontroller.isgrounded) { Movedir = vector3.zero;if (DirectInput = = directioninput.up) {jump (); if (isdoublejump) {Jumpsecond = true;}}} Else{if (DirectInput = = Directioninput.down) {quickground ();} if (DirectInput = = directioninput.up) {if (Jumpsecond) {jumpseccond (); jumpsecond = false;}} if (animationmanager.animationstate! = animationmanager.jump&& Animationmanager.animationstate! = animationmanager.jumpsecond&& animationmanager.animationstate! = animationmanager.roll) { Animationmanager.animationstate = Animationmanager.jumploop;}} movEdir.z = 0;movedir + = this.transform.TransformDirection (vector3.forward*speedmove); MOVEDIR.Y-= Gravity * Time.deltatime; Checksidecollision (); Charactercontroller.move ((movedir+direction) *time.deltatime);}
Collision logic with obstacles.
<span style= "White-space:pre" ></span>private void Checksidecollision () {if (Positionstand = = Position.right) {if (int) Charactercontroller.collisionflags = = 5 | | charactercontroller.collisionflags = = Collisionflags.sides) {if (Transform.position.x < 1.75f && Checksidecollision = = False) {Debug.Log ("hit"); CameraFollow.instace.ActiveShake ();p Ositionstand = Position.middle;checksidecollision = True;}}} if (Positionstand = = Position.left) {if (int) Charactercontroller.collisionflags = = 5 | | Charactercontroller.collisionflags = = collisionflags.sides) {if (Transform.position.x > -1.75f && Checksidecollision = = False) {Debug.Log ("hit"); CameraFollow.instace.ActiveShake ();p Ositionstand = Position.middle;checksidecollision = True;}}} if (Positionstand = = Position.middle) {if (int) Charactercontroller.collisionflags = = 5 | | Charactercontroller.collisionflags = = collisionflags.sides) {if (Transform.position.x < -0.05f && Checksidecollision = = False) {Debug.Log ("hit"); CaMeraFollow.instace.ActiveShake ();p Ositionstand = Position.left;checksidecollision = true;} else if (transform.position.x > 0.05f && checksidecollision = = False) {Debug.Log ("hit"); CameraFollow.instace.ActiveShake ();p Ositionstand = Position.right;checksidecollision = True;}}} if (checksidecollision = = True) {Countdeleyinput + = time.deltatime;if (countdeleyinput >= 1f) {checksidecollision = false;countdeleyinput = 0;}}}
Gold coin Pickup
<span style= "White-space:pre" ></span>public void Magnet (float time) {stopcoroutine ("canclemagnet"); Magnet. SetActive (true); timemagnet = time; Startcoroutine (Canclemagnet ());}
3 Creating a game controller
Add empty objects to scenes, mount scripts, load scenes asynchronously, start games, dynamically create game objects, score updates and resets.
<span style= "White-space:pre" ></span>//loading methodienumerator waitloading () {while ( Patsysm.loadingcomplete = = false) {yield return 0;} Startcoroutine (Initplayer ());} Spawn player Methodienumerator Initplayer () {Gameobject go = (gameobject) instantiate (Playerpref[selectplayer], Posstart, quaternion.identity); camerafol.target = Go.transform;yield return 0; Startcoroutine (Updateperdistance ());} Update distance Scoreienumerator updateperdistance () {while (true) {if (PatternSystem.instance.loadingComplete) {if ( GameAttribute.gameAttribute.pause = = false&& GameAttribute.gameAttribute.isPlaying = = true&& GameAttribute.gameAttribute.life > 0) {if (controller.instace.transform.position.z > 0) { GameAttribute.gameAttribute.distance + = GameAttribute.gameAttribute.speed * Time.deltatime;distancecheck + = GameAttribute.gameAttribute.speed * TIME.DELTATIME;IF (Distancecheck >= speedaddeverydistance) { GameAttribute.gameAttribute.speed + = Speedadd;if (gameattribute.gameattribute.Speed >= Speedmax) {GameAttribute.gameAttribute.speed = Speedmax;} countaddspeed++;d istancecheck = 0;}}} Yield return 0;}} Reset Gamepublic IEnumerator resetgame () {GameAttribute.gameAttribute.isPlaying = false; GUIManager.instance.showSumGUI = True;int Oldcoind = Gamedata.loadcoin (); Gamedata.savecoin ((int) gameattribute.gameattribute.coin+oldcoind);d Istancecheck = 0;countaddspeed = 0;yield return 0 ;}
4 Setting the camera to followMain camera mount script with follow, reset, and shake logic
<span style= "White-space:pre" ></span>void lateupdate () {if (target! = null) {if (target.position.z >= 0) { if (shake = = False) {poscamera.x = Mathf.lerp (poscamera.x, target.position.x, 5 * time.deltatime);p oscamera.y = Mathf.lerp (POSCAMERA.Y, TARGET.POSITION.Y + height, 5 * time.deltatime);p oscamera.z = Mathf.lerp (poscamera.z, target.position.z + D Istance, GameAttribute.gameAttribute.speed); * time.deltatime); transform.position = poscamera;anglecam.x = Angle;anglecam.y = Mathf.lerp (angleCam.y, 0, 1 * Time.del Tatime); anglecam.z = Transform.eulerangles.z;transform.eulerangles = Vector3.lerp (Transform.eulerangles, angleCam, 1 * time.deltatime);}} Else{if (PatternSystem.instance.loadingComplete = = true) {Vector3 dummy = vector3.zero;poscamera.x = Mathf.lerp ( poscamera.x, 0, 5 * time.deltatime);p oscamera.y = Mathf.lerp (poscamera.y, dummy.y + height, 5 * time.deltatime);p Oscamera. z = dummy.z + distance;transform.position = poscamera;anglecam.x = Angle;anglecam.y = Transform.eulerangles.Y;anglecam.z = Transform.eulerangles.z;transform.eulerangles = Anglecam;}}} Reset camera when charater diepublic void Reset () {shake = false; Vector3 dummy = vector3.zero;poscamera.x = 0;POSCAMERA.Y = dummy.y + height;poscamera.z = dummy.z + distance;transform.pos ition = poscamera;anglecam.x = Angle;anglecam.y = Transform.eulerangles.y;anglecam.z = transform.eulerAngles.z; Transform.eulerangles = Anglecam;} Shake camerapublic void Activeshake () {Shake = true; Startcoroutine (Shakecamera ());}
5 Adjusting the size of the scene objectA standard cube can be used as a ruler to measure the height and width of a scene object, making the game character and scene object proportional.
6 Adding a Game object instance management systemCreate empty objects, mount scripts for dynamic management of updates to scene objects (e.g. infinite extension of streets, random generation of obstructions, etc.)
<span style= "White-space:pre" ></span>ienumerator addbuilding () {Queuefloor qFloor = queneFloor[0]; Quenefloor.removerange (0,1); int i = 0;randompattern = Random.range (0, patternbuilding.count); Randomitem = Random.Range (0, Patternitem.count); while (I < building_script.count) {int J = 0;while (J < Patternbuilding[randompattern]. Statebuilding_left.length) {checkaddbuilding_left (I,j,qfloor); j + +;} j = 0;while (J < patternbuilding[randompattern].statebuilding_right.length) {checkaddbuilding_right (I,j,qFloor); j ++;} i++;} Yield return 0;i = 0; Checktypeitemformadd (Qfloor, i); yield return 0;qfloor.floorobj.transform.position = posfloorlast;posfloorlast.z + = Nextposfloor;quenefloor.add (Qfloor); Startcoroutine (Waitcheckfloor ()); yield return 0;}
<span style= "White-space:pre" ></span>void additemwihttype_subright (queuefloor floor, int slotIndex,int Type) {if (patternitem[randomitem].itemtype_subright[slotindex].x = = type) {int j = 0;while (J < Amount_item_pattern_ Subright[type-1]) {if (J < Item_type_script[type-1].itemlist.count) {if (Item_type_script[type-1].itemlist[j]. Itemactive = = False && Floor.flooritemslotclass.floor_slot_subright[slotindex] = = False) {Setpositem_ Subright_for_type (Slotindex,type-1,j,floor, patternitem[randomitem].itemtype_subright[slotindex].y); j = 0;}} j + +;}}}
7 setting up barrier objectsHere's a great tool for unity, which allows you to configure your internal data structure graphically to achieve WYSIWYG
, you can manually configure the street objects, to get obstacles, very good level settings editor.
Compiling compiles the project into an executable file, you need to add all the scenes to the build settings and note the order.
Game effects
Start Interface
Shop
in-game
Unity Store Demo Learning: Parkour games