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

Source: Internet
Author: User

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.


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

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.