[Unity3D] Unity3D game development-Parkour game project

Source: Internet
Author: User

Good evening, everyone. I'm Qin Yuanpei. Welcome to follow my blog. My blog address is ghost. The blogger got up at seven o'clock this morning and finally finished the game at ten o'clock AM. Therefore, the topic of today's blog is "Unity3D Game Development's parkour game project explanation".

From the perspective of bloggers, when you choose to do what you love, your heart must be filled with passion and courage. You are willing to see your own efforts, you are willing to see your own efforts. We have grown up to promote the continuous improvement of our cognition. Therefore, we should treat our lives with a sincere and humble attitude, we may not be able to choose from our origins, but we can choose to work towards our favorite lives. Maybe I am not really a person in the world with these people. Many things have come to an end today, and I have no idea how to try to communicate with them any more, maybe the four years of college are cool, and the only thing I can feel is the loneliness. Now, let's get started with today's content!


I. Game Planning

The game uses a 2D interface, where the role runs from left to right. Obstacles and gold coins are randomly generated in road sections. players need to use the jump function to avoid obstacles. In the game, the more coins the players collect, the longer the distance, the higher the score. First, let's talk about the principle of the game. Here we use a fixed road section and a mobile camera method. In other words, when the role starts to move, the camera and scene follow the role slowly to the right. When the role runs 2/3 of the distance from each road section, calculate the location of the next road section and generate a new road section at this location. In this way, an infinitely distant road section can be generated in the game scenario, when a route segment leaves the camera's view, it is immediately destroyed. At the same time, we randomly generate obstacles and gold coins on each road section, and then perform Collision Detection on the role.



Ii. Role Control

Here, we only focus on the role status, that is, whether the role is running or dead. In this status, we adopt different processing methods for roles. If the role is running, the position of the role, camera position, and background is updated. Otherwise, the role will die after being hit by an obstacle. Let's take a look at the following script:

Using UnityEngine; using System. collections; public class Player: MonoBehaviour {// define the role's moving speed public float mMoveSpeed = 2.5F; // camera private Transform mCamera; // The background image private Transform mBackground; // whether the role is running private bool isRuning = true; // In the scenario, the total number of road sections is private int mCount = 1; // the road section defaults to public GameObject CubeWay; // number of dead animation playback Times private int DeathCount = 0; // number of coins collected private int mCoinCount = 0; public int CoinCount {get {return mCoinCount ;}} // The current running distance is private int mLength = 0; public int Length {get {return mLength ;}// the current score is private int mGrade = 0; public int Grade {get {return mGrade ;}} void Start () {// get the Camera mCamera = Camera. main. transform; // obtain the background mBackground = GameObject. find ("Background "). transform;} void Update () {// if the role is running, Move the role, camera, and scenario if (isRuning) {Move (); CreateCubeWay (); Jump (); updateData () ;}else {Death () ;}/// <summary> /// update player game data /// </summary> private void UpdateData () {// calculate the running distance mLength = (int) (transform. position. x + 25) * 25); // calculate the player score mGrade = (int) (mLength * 0.8 + mCoinCount * 0.2);} // The role's Death private void Death () {// to prevent death animation from being updated at each frame, use DeathCount to limit if (DeathCount <= 1) {// play the death animation transform. animation. play ("Lose"); // times + 1 DeathCount + = 1; // Save the current record // PlayerPrefs. setInt ("enter a unique value here", Grade) ;}} private void Jump () {// The rigid body structure cannot be used here, so you can use a manual method to implement skip if (Input. getKeyDown (KeyCode. space) | Input. getMouseButton (0) {while (transform. position. y <= 1) {float y = transform. position. y + 0.02F; transform. position = new Vector3 (transform. position. x, y, transform. position. z); transform. animation. play ("Jump") ;}startcoroutine ("Wait") ;}} IEnumerator Wait () {yield return new WaitForSeconds (0.8F); // The role continues running while (transform. position. y> 0.125F) {float y = transform. position. y-0.02F; transform. position = new Vector3 (transform. position. x, y, transform. position. z); transform. animation. play ("Run") ;}}// Move the role, camera, and scene private void Move () {// Run transform from left to right. translate (Vector3.forward * mMoveSpeed * Time. deltaTime); // Mobile Camera mCamera. translate (Vector3.right * mMoveSpeed * Time. deltaTime); // move the background mBackground. translate (Vector3.left * mMoveSpeed * Time. deltaTime);} // create a new road section private void CreateCubeWay () {// when the role finishes 2/3 of a road section, create a new road section // calculate the distance between the first n-1 road sections based on the total distance that the role ran. That is, if (transform. position. x + 30-(mCount-1) * 50> = 50*2/3) {// clone road section // calculate the distance from the first road section to GameObject mObject = (GameObject) Instantiate (CubeWay, new Vector3 (-5F + mCount * 50F, 0F,-2F), Quaternion. identity); mObject. transform. localScale = new Vector3 (50F, 0.25F, 1F); // Add 1 mCount + = 1 ;}} void OnTriggerEnter (Collider mCollider) {// if you encounter a gold coin, then the gold coin disappears, and the number of gold coins increases by 1; if (mCollider. gameObject. tag = "Coin") {Destroy (mCollider. gameObject); mCoinCount + = 1;} // if an obstacle is encountered, the game ends else if (mCollider. gameObject. tag = "Rock") {isRuning = false ;}}}
Here we need to pay attention to the following content:

1. The Update () method and the Move (), Jump (), CreateCubeWay (), and Death () methods are the core control methods of a role during running.

2. The CreateCubeWay () method is used to generate a new road section at the specified position when the player finishes running 2/3 of each road section. Assume that there are n road sections in the current scenario. The distance between players running on each road section = the length from the start point to the current position-the distance between the first n-1 road sections. In this way, we can determine the relative position of players on each road section. After this position is determined, we compare it with 2/3 of the road section length. If the distance is greater than or equal to this distance, a new road section is generated, the n + 1 section is equal to the position of the first section + the total length of n sections. As a result, a new road section is generated at the specified position, so that a new road section is continuously generated in the scenario.

3. The collision detection of gold coins and obstacles collected by players is implemented in the OnTrigger method. We use a bool-type identifier variable isRuning to indicate the role status, this status directly affects the execution of the Update () method. You can find it in your code.

4. Here, the role Jump is simulated by a script, because using a rigid body here does not seem to be able to achieve the effect that the blogger wants to achieve. You can refer to the Jump () method.


3. Scenario deployment

Here we use the 2D plane as the background of the game and use NGUI to display the text content on the interface. To use NGUI in Unity3D, you need to set the camera and Anchor to the same level and set the camera depth so that both camera systems can work. Section CubeWay is a preset Cube that generates gold coins and obstacles on CubeWay. This object is associated with the Player Script. The player role is a 3D character model, with the final effect set in the scenario:



Iv. preset Definition

In this game, the objects to be reused include section CubeWay, Coin, and obstacle Rock. Let's look at their scripts respectively:

Using UnityEngine; using System. collections; public class CubeWay: MonoBehaviour {// gold coins and obstacles displayed on the Road public GameObject [] mObjects; void Start () {// 20 to 50 items are randomly generated on each section. int mCount = Random. range (20, 50); for (int I = 0; I <mCount; I ++) {Instantiate (mObjects [0], new Vector3 (Random. range (this. transform. position. x-25, this. transform. position. x + 25), 1F,-2F), Quaternion. euler (new Vector3 (90F, 180F, 0F);} // 5 to 10 obstacles are randomly generated on each section. mCount = Random. range (5, 10); for (int I = 0; I <mCount; I ++) {Instantiate (mObjects [1], new Vector3 (Random. range (this. transform. position. x-25, this. transform. position. x + 25), 0.5F,-2F), Quaternion. euler (new Vector3 (90F, 180F, 0F) ;}// Destroy void OnBecameInvisible () {Destroy (this. gameObject );}}

Using UnityEngine; using System. collections; public class Coin: MonoBehaviour {// here is a script for currency rotation control void Update () {transform. rotate (Vector3.forward * 50F * Time. deltaTime);} // Destroy void OnBecameInvisible () {Destroy (this. gameObject );}}

Using UnityEngine; using System. Collections; public class Rock: MonoBehaviour {// Destroy void OnBecameInvisible () {Destroy (this. gameObject) immediately when you leave the camera's field of view );}}

CubeWay is a Cube, Coin is a cylinder, in order to make the gold coins look good, we let the gold coins rotate in the air, Rock is a flat map. Here Rock and Coin will be bound to the mObjects of CubeWay.

V. Game Management

Finally, the interface data is updated. The script definition is as follows:

Using UnityEngine; using System. collections; public class GameManager: MonoBehaviour {// game interface root node private Transform GameUI; // player private Transform mPlayer; // number of coins on the interface and the distance from private Transform mCoins; private Transform mLength; void Start () {GameUI = GameObject. find ("2DUI "). transform; mPlayer = GameObject. find ("People "). transform; mCoins = GameUI. findChild ("Anchor/Panel/Coins "). transform; mLength = GameUI. findChild ("Anchor/Panel/Length "). transform;} void Update () {mCoins. getComponent <UILabel> (). text = "gold coin:" + mPlayer. getComponent <Player> (). coinCount; mLength. getComponent <UILabel> (). text = "distance:" + mPlayer. getComponent <Player> (). length ;}}

Now the entire game has been explained. I believe you can't wait to see the final effect. Well, let's take a look at it:




I hope you will like the content of today. I hope you will continue to support and follow up on my blog. If we make some modifications to today's example, we can make the 3D version cool, the classic temple escape game was developed based on the Unity3D game engine.


Daily proverbs: You must have good hopes and do your best to pursue them. You must have great dreams and do your best to achieve them.




If you like my blog, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei
Reprinted please indicate the source, Author: Qin Yuanpei, the source of this article: http://blog.csdn.net/qinyuanpei/article/details/25510579


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.