In two months ago wrote a "Unity3d actual combat" 0 basis of a step by stage to teach you to make Parkour games (1), the inside step by step demonstration of the production of parkour games, however, because of time, only wrote to let the character move forward so far. The pit has no time to fill, (although not many people see it), today just have time to complete a Parkour game demo. Put it up for interested friends to see.
Demo source and corresponding material download: Link: http://pan.baidu.com/s/1i4QkkuD Password: p04w
Game Brief Description
Game type: Parkour games (Demo, non-full game)
Operating mode: Left and RIGHT ARROW keys (can be transplanted to the mobile phone side)
Game elements:
1. The role of the game will automatically run forward, the player can be left and right arrow keys to move around
2. There are obstacles in the game, players need to avoid these obstacles, or will be blocked because of obstacles to the cause of progress
3. When a game character disappears from view because it is blocked, it is considered a failure
4. When a game character is left behind because it is blocked, it will increase its movement speed until it returns to its original screen position.
Game Scene Setup
Use the prepared material (road, character, obstacle), make the material into prefab, and then set up the scene according to your liking. Such as:
Game Script WritingGame Role Controller Movecontroller:Create a new C # file, name Movecontroller, and then open it.Because the character needs to move forward, left, and right, we need to have itsspeed in the forward directionAndvelocity in the left and right direction, respectively named: Movevspeed, Movehspeed,At the same time, since the player needs to be accelerated in the case of backwardness, we declare two variables:minimum moving speed in the forward direction minvspeedAndmaximum moving speed in the forward direction Maxvspeed。So we can get the following script:
<span style= "White-space:pre" ></span>//Advance movement speed <span style= "White-space:pre" ></span>float Movevspeed;<span style= "White-space:pre" ></span>//horizontal movement speed <span style= "White-space:pre" ></ span>public float movehspeed = 5.0f;<span style= "white-space:pre" ></span>//Max speed <span style= " White-space:pre "></span>public float maxvspeed = 10.0f;<span style=" White-space:pre "></span>// Min. speed <span style= "white-space:pre" ></span>public float minvspeed = 5.0f;
Where Movehspeed, Maxvspeed, Minvspeed declared public, convenient to modify on the panel.
Next, define the initial value of the Movevspeed in the Start () function:
Movevspeed = minvspeed;to enable the character to move in Update ():
Float h = input.getaxis ("horizontal"); Vector3 vspeed = new Vector3 (this.transform.forward.x, THIS.TRANSFORM.FORWARD.Y, this.transform.forward.z) * Movevspeed; Vector3 hspeed = new Vector3 (This.transform.right.x, THIS.TRANSFORM.RIGHT.Y, this.transform.right.z) * Movehspeed * h;< C3/>vector3 jumpspeed = new Vector3 (this.transform.up.x, THIS.TRANSFORM.UP.Y, this.transform.up.z) * JumpHeight * m_ Jumpstate; This.transform.position + = (vspeed + hspeed + jumpspeed) * time.deltatime;
Save the CS file, switch to unity, and attach the script to the character object, leaving the default or manual settings:
run the game, see if you can run successfully, and can control the movement around the character by the key.
Watch the characters run farther and farther away, and finally disappear in the distance ... Eh Coach, this is not the same as the deal! How did the characters disappear?
cough, that's because we didn't let the camera follow it, and then we let the camera move with the character ~
Open the C # file just now and declare a public variable
//Camera position
Public Transform cameratransform;
In the update () function, add the following code:
//Set camera movement speed Vector3 vcameraspeed = new Vector3 (this.transform.forward.x, This.transform.forward.y, This.transform.forward.z) * MINVSPEED;Let the camera run.cameratransform.position + = (vcameraspeed) * time.deltatime;
Note that the speed of the camera we define here is somewhat different from the speed of the character movement:1. Camera does not move around2. The camera has a constant speed of minvspeed, which is the minimum movement speed of the characters we define (of course, the characters are always moving at this rate)
Go to unity and look at the move controller component on the person, and there should be one more variable waiting for you to set it up:
We areDrag the camera to camera transform, and then run the game. At this point you should be able to see the characters moving forward, but the position on the screen is unchanged, as the camera moves together.
The character is walking, gee, why is there no way ahead? Don't worry, let's get out of the way. Infinite Extension ~
First we will gameobject a few of the road, I here is a total of 3 road Gameobject, named Road1,road2,road3, respectively
Then, under each road, add a cube, close the mesh renderer of the cube, and hook up its box collider is trigger. Name is Arrivepos. (I'm not going to tell you that this step should be done before the last line!) )
will beMany roads are put together to form a straight road.
Then create a new empty object named Gamemanager, create a new c#script GameManager.cs for it, and open the script.
Declare multiple variables: (Note the reference namespace using System.Collections.Generic;
Create an obstacle point list public list<transform> bornposlist = new list<transform> (); Road list public list<transform> roadlist = new list<transform> (); Arrival point list Public list<transform> arriveposlist = new list<transform> (); List of obstructions public list<gameobject> objprefablist = new list<gameobject> (); Current obstacle dictionary<string, list<gameobject>> objdict = new dictionary<string, List<gameobject >> (); Road interval distance public int roaddistance;
and define the function:
Cut out the new road public void Changeroad (Transform arrivepos) {int index = Arriveposlist.indexof (Arrivepos); if (index >= 0) {int lastIndex = index-1; if (LastIndex < 0) LastIndex = roadlist.count-1; Mobile Road roadlist[index].position = roadlist[lastindex].position + new Vector3 (roaddistance, 0, 0); Initroad (index); } else {debug.logerror ("Arrivepos index is Error"); Return }} void Initroad (int index) {string roadname = Roadlist[index].name; Empty existing obstructions foreach (Gameobject obj in Objdict[roadname]) {Destroy (obj); } Objdict[roadname]. Clear (); Add obstacle foreach (Transform pos in Bornposlist[index]) {gameobject prefab = objprefablist[random. Range (0, Objprefablist.count)]; Vector3 eulerangle = new Vector3 (0, Random.range (0, 360), 0); Gameobject obj = Instantiate (prefab, Pos.position, Quaternion.eulerangles (Eulerangle)) as Gameobject; Obj.tag = "obstacle"; Objdict[roadname]. ADD (obj); } }
In Start ():
void Start () { foreach (Transform Road in Roadlist) { list<gameobject> objlist = new list< Gameobject> (); Objdict.add (Road.name, objlist); } Initroad (0); Initroad (1); }
Then open the previous MoveController.cs, declaring the variable:
Game Manager
Public Gamemanager Gamemanager;To define a function:
void Ontriggerenter (Collider other) { //If it is the arrival point if (Other.name.Equals ("Arrivepos")) { Gamemanager.changeroad (Other.transform); } If it is the transparent wall else if (other.tag.Equals ("Alphawall")) {// Nothing } //If it is an obstruction else if ( Other.tag.Equals ("obstacle")) { } }
Call, a large string of code, we knock tired not tired, what! Are you copy the past? It's too much! I'm going to get a knife!
Well, switch back to unity and click on the Gamemanager object to set the value of its Gamemanager component:
The bornpos here refers to the barrier spawn Point, which defines one or more spawn points for each road shown, and the spawn point of each route is managed with an empty Bornpos object:then drag the spawn point to the number of the road it is on (set the value of size first, and 3 to 3) the same is true of roadlist, which drags the roads in by serial numbers.
here the arriveposlist to notice, not directly by the number of the road to drag into, but the next one, namely: road2 road3........ Roadn Road1
In this order, drag their corresponding arrivepos into the list
Then drag the prefab file of the obstacle you want to create into the objprefablistSets the distance between roads (that is, the center point of a road to the center point of the next road distance = ROAD1.LENGTH/2 + ROAD2.LENGTH/2 presumably)
By this step, the setup of Gamemanager is basically complete. Click on the character's Gameobject, set Movecontroller, and drag Gamemanager's game object to the specified location:
Yes, there is one more important setting:Add collider and Rightbody to the characters,Add collider for all obstructions and pavements (note not to be Trigger)
Then run the game.
Call, this time there is no problem should be able to see there are obstacles appear, the characters go to the obstacles will be blocked, and the road will automatically splicing mobile, endless go down, go down, go down ...
This demo also basically came to an end, next, do the final game failure to judge and let the character back to normal position.
Open GameManager.cs, declaring variables:
public bool Isend = FALSE;
Open the MoveController.cs declaration variable:
Distance of the camera from the character
public float cameradistance;
Add the following code in the update () function:
When the character and camera distance is less than cameradistance, let it accelerate if (This.transform.position.x-cameratransform.position.x < cameradistance ) { movevspeed + = 0.1f; if (Movevspeed > Maxvspeed) { movevspeed = maxvspeed; } } Over time let the camera catch the else if (this.transform.position.x-cameratransform.position.x > Cameradistance) { Movevspeed = Minvspeed; Cameratransform.position = new Vector3 (this.transform.position.x-cameradistance, CAMERATRANSFORM.POSITION.Y, CAMERATRANSFORM.POSITION.Z); } Camera over people if (cameratransform.position.x-this.transform.position.x > 0.0001f) { Debug.Log ("You lost!!!!!!! !!! "); Gamemanager.isend = true; }
Define the Ongui () function:
void Ongui () { if (gamemanager.isend) { Guistyle style = new Guistyle (); Style.alignment = Textanchor.middlecenter; Style.fontsize = +; Style.normal.textColor = color.red; Gui. Label (New Rect (SCREEN.WIDTH/2-SCREEN.HEIGHT/2-50, 200, 100), "You lose ~", style);} }
In this step, the demo is written. (because it is 4 o'clock in the morning is too tired, so this article is basically directly affixed to the code, if there is any problem with friends can directly message ha ~)
"Unity3d actual Combat" 0 Foundation step in one step to teach you to make Parkour games (pits full version)