Development and defense of mobile games-II. BASICS (Continuous updates) and basics of mobile game development

Source: Internet
Author: User

Development and defense of mobile games-II. BASICS (Continuous updates) and basics of mobile game development

Sorry, this article is not available until now because of the recent expansion of company members, technical training, and project issues.

Okay, let's not talk about anything else.


This article is suitable for people who have some knowledge about the basic components of Unity and want to know how to apply various components in the project.


This article describes some of Unity's knowledge with the example "Unity Projects Stealth" in Asset Store. So you may need to understand some concepts of Unity. In addition, the example "Unity Projects Stealth. unitypackage" has been shared with http://pan.baidu.com/s/1gd6a0l1. you can refer to the document for more information.


This project will be divided into the following parts (this directory may be slightly modified as needed in subsequent articles ):


1. Project Overview

2. Basis of the collision Generator

3. Control roles

4. Collision tool and its use

5. Monster AI

6. Event Notification

7. rendering special effects


OK.


Before that, let's talk about the environment for running the project in this article:

System: Window 7X64

Unity: 4.3.3f1


1. Project Overview:

Open unity, create a project, and import the downloaded "Unity Projects Stealth. unitypackage ". After the import is complete, you can see the structure of the "Project" View (if you do not open it, you can open it from the menu bar "Window"-"Project") as follows:


You can open each directory in it as needed:

Animations Some role animations
Animator The animation controller does not actually have anything in it -_-
Audio Audio files
Fonts Font
Gizmos Some icons used for debugging in the scene view
Materials Material
Models Model
Prefabs Preset
Scenes Scenario file. Nothing actually exists -_-
Scripts Script. Nothing actually exists -_-
Shaders Coloring er
Textures Some textures
Done The missing animation controllers, scene files, and scripts are included ~ ·

You can also classify different resources in different directories by referring to the above practice. As soon as you see these folders, you will know what resources are in them. This reduces communication costs and facilitates searching for resources.

Okay. If there is no problem, click the play button to run the project.


Use the WASD on the keyboard or the up, down, and down arrows to control the movements of people. At the lower left of the screen, there are text buttons for other operations. For example, "Z" can be enabled.


2. Basis of the collision Generator

Generally, if you want an object to have a volume directly and cause a collision, you need to add a collision tool "Collider" to the object ". In Unity, you only need to select an object, and then click "Component"-"Physics" in the menu bar, there are various collision machines, you can select a closer one based on your model. There are two kinds of Colliders in Unity: static and dynamic.

Static means that the collision server will not be displaced, rotated, or scaled during the game.

Dynamic means that the collision generator may be displaced, rotated, or scaled during the game. A rigid body component must be added to the Dynamic Collision generator. Otherwise, the collision may fail and the performance overhead may increase. (For example, if Unity is a UI component NGUI, its new version of Panel will check whether the current object has Rigibody. If not, it will automatically add one. It is to prevent developers from making some interface animations and forgetting to modify and add Rigibody, leading to invalid UI Button clicking .)


3. Control roles

The simplest way to control the movement of a role in the scene is to drag an object to the scene and press the buttons. Set the position value of the transform object. In this way, objects can be moved in the scene.

You can use the Input. GetAxis method to obtain keys. You can use the Input. GetAxis ("Horizontal") and Input. GetAxis ("Vertical") methods to obtain keys on the X and Z axes respectively. "Horizontal" and "Vertical" are actually configured in "Edit"-"Project Settings"-"Input.

In this example, how does one make the player's role motion?

First, find the "char_ethan" object in the "Hierarchy" View (If this view is not available, you can open it in "Window"-"Hierarchy") and select it.


Let's take a look at its "Inspector" View (if not, open it in "Window"-"Inspector ).


Note that in addition to the basic Transform components, there are also "Animator", "Capsule Collider", and "Rigidbody" components. These three components are actually used as a gold combination for movable objects (of course, "Character Controller" is also useful ).

There are also "Audio Source" and "Audio Listener", which are the components for playing and listening to sounds respectively.

Then, "Done Player Health (Script)", "Done Player Inventory (Script)", and "Done Player Movement (Script)" Are the scripts for role control. The purpose of each component is as follows:


A. Animator

It is actually a built-in animation controller of Unity, working in a state machine. For example, double-click the "Controller" attribute in the "Animator" panel above.


The edit window of the state machine appears.


B. Capsule Collider

Capsule collision tool. It is relatively simple and there is nothing to say.

C. Rigidbody

As mentioned above, the need for a dynamic collision generator.

The components A, B, and C are briefly introduced here, and will not be discussed here. If necessary, I will write an article later.

Next let's take a look at the control code section.

D. DonePlayerMovement

Here is a "DonePlayerMovement (Script)" component. From the perspective of its name, we know that it should be mobile control (So naming is important ). Let's open the source code of this component and look at it.

Using UnityEngine; using System. collections; public class DonePlayerMovement: MonoBehaviour {public AudioClip shoutingClip; // The Player shouted public float turnSmoothing = 15f; // the value of public float speedDampTime = 0.1f; // controls the time limit for changing from one value to another. private Animator anim; private DoneHashIDs hash; // saves hashvoid Awake () for various animation states () {anim = GetComponent <Animator> (); hash = GameObject. findGameObjectWithTag (DoneTags. gameContro Roller ). getComponent <DoneHashIDs> (); // Set the weight of the shouting layer to 1. anim. setLayerWeight (1, 1f);} void FixedUpdate () {// Cache the inputs. float h = Input. getAxis ("Horizontal"); float v = Input. getAxis ("Vertical"); bool sneak = Input. getButton ("Sneak"); MovementManagement (h, v, sneak);} void Update () {// Cache the attention attracting input. bool shout = Input. getButtonDown ("Attract"); // Set th E animator shouting parameter. anim. setBool (hash. shoutingBool, shout); AudioManagement (shout);} void MovementManagement (float horizontal, float vertical, bool sneaking) {// Set the sneaking parameter to the sneak input. anim. setBool (hash. sneakingBool, sneaking); // If there is some axis input... if (horizontal! = 0f | vertical! = 0f ){//... set the players rotation and set the speed parameter to 5.5f.Rotating (horizontal, vertical); anim. setFloat (hash. speedFloat, 5.5f, speedDampTime, Time. deltaTime);} else // Otherwise set the speed parameter to 0. anim. setFloat (hash. speedFloat, 0);} void Rotating (float horizontal, float vertical) {// Create a new vector of the horizontal and vertical inputs. vector3 targetDirection = new Vector 3 (horizontal, 0f, vertical); // Create a rotation based on this new vector assuming that up is the global y axis. quaternion targetRotation = Quaternion. lookRotation (targetDirection, Vector3.up); // Create a rotation that is an increment closer to the target rotation from the player's rotation. quaternion newRotation = Quaternion. lerp (rigidbody. rotation, targetRotation, turnSmoothing * Time. deltaTime); // Change the players rotation to this new rotation. rigidbody. moveRotation (newRotation);} void AudioManagement (bool shout) {// If the player is currently in the run state... if (anim. getCurrentAnimatorStateInfo (0 ). nameHash = hash. locomotionState ){//... and if the footsteps are not playing... if (! Audio. isPlaying )//... play them. audio. play ();} else // Otherwise stop the footsteps. audio. stop (); // If the shout input has been pressed... if (shout )//... play the shouting clip where we are. audioSource. playClipAtPoint (shoutingClip, transform. position );}}

The script is as follows:

In Awake, first find the Animator component and the DoneHashIds component used by the script, and then cache them (you do not need to find them frequently and save the CPU ).

During FixedUpdate, obtain the mobile key pressed by the player and handle the movement and Role orientation issues.

During the Update, obtain whether the player presses the "Attract" key and determines whether to play the animation and sound.


OK. Let's mainly look at the MovementManagement method:

void MovementManagement (float horizontal, float vertical, bool sneaking){// Set the sneaking parameter to the sneak input.anim.SetBool(hash.sneakingBool, sneaking);// If there is some axis input...if(horizontal != 0f || vertical != 0f){// ... set the players rotation and set the speed parameter to 5.5f.Rotating(horizontal, vertical);anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);}else// Otherwise set the speed parameter to 0.anim.SetFloat(hash.speedFloat, 0);}
We can find that, apart from the Rotating method, the anim is basically used to set the status. So what is in the Rotating method?

Void Rotating (float horizontal, float vertical) {// create a Vector3 to save the input horizontal displacement direction Vector3 targetDirection = new Vector3 (horizontal, 0f, vertical ); // calculate the orientation of the Quaternion targetRotation = Quaternion based on the preceding direction. lookRotation (targetDirection, Vector3.up); // creates an incremental rotation increment Quaternion newRotation = Quaternion from the player's current direction to the target. lerp (rigidbody. rotation, targetRotation, turnSmoothing * Time. deltaTime); // modify the player's direction to rigidbody. moveRotation (newRotation );}

The code above shows that the player's position is not moved either. Is it implemented in other components or is it processed in other ways? In addition, the next loop is decomposed.



What is the magic of mobile games?

How can I achieve this? I can only switch to a professional game. I have battles and 4 zones.

Qinshi Mingyue mobile game friends invited second task

You just need to give your invitation code to someone else and let him enter it, but he needs to meet the requirements of 15 levels. Thank you!
 

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.