Unity uses Gameobject to create simple, movable objects

Source: Internet
Author: User

The game is developed on unity and requires game scripting to be combined with 3D modeling to drag a written script into a 3D object to achieve the effect.

Here's a small example, using unity 3D to achieve a control of the movement of the villain, the villain can move forward, backward, left and right.

1. Create a scene from the File-> New scene;

2. Create a gameobject by creating Empty, which is the game object;

3, in the Inspector panel to change the name of this object to "Hero" (Remember to press ENTER), wait a while the script will use this name to find this object, such as:


4. Click Add Component, select Mesh---mesh Renderer as shown:



5. Click on Add Component again to add mesh-and mesh Filter



6. Click the small button in the mesh Filter and select Quad on the popup page.


7. Create a folder in Project resources and create four folders in it: Forward,back,right,left, these four folders are used to place picture files;

Create the file: 2d.js, and use the editor to open the code as follows:

<strong><span style= "Font-size:18px;color: #ff0000;" > #pragma strictprivate var hero:gameobject;//map array private var forward:object[];p rivate var left:object[];p Rivate v Ar right:object[];p rivate var back:object[];//Figure Walk Chart counter private var counter:int;//frames per second private var fps:float = 2;//Limit Frame Time private var time:float = 0;//Determines whether the button is pressed with a Boolean value of private var keyforward:boolean;private var keyback:boolean;private va R keyleft:boolean;private Var keyright:boolean;//current map Array private var anim:object[];//current direction private var Pointer:int = 1 ;//The value of the direction vector, that is, the size of the speed private var f:float = 0.001f;function Start () {hero = Gameobject.find ("Hero"); forward = Resources.loadal L ("Woman/forward"); left = Resources.loadall ("Woman/left"), right = Resources.loadall ("Woman/right"), back = Resource S.loadall ("Woman/back"); anim = Forward;counter = 0;} function Ongui () {guilayout.beginhorizontal ();//four buttons arranged horizontally keyforward = Guilayout.repeatbutton ("forward"); Keyleft = Guilayout.repeatbutton ("left"); keyright = GuilayoUt. RepeatButton ("right"); Keyback = Guilayout.repeatbutton ("backwards"); Guilayout.endhorizontal ();//the button to determine the button if (keyforward) {setanimation (1,forward); Hero.transform.Translate ( Vector3.forward * f);} if (keyleft) {setanimation (2,left); Hero.transform.Translate (-vector3.right * f);} if (keyright) {setanimation (3,right); Hero.transform.Translate (Vector3.right * f);} if (keyback) {setanimation (4,back); Hero.transform.Translate (Vector3.back * f);} *///determine the current direction of switch (pointer) {case 1:hero.transform.translate (-vector3.forward * f); Break;case 2: Hero.transform.Translate (-vector3.right * f); Break;case 3:hero.transform.translate (Vector3.right * f); Break;case 4: Hero.transform.Translate (Vector3.forward * f); break;} Response Animation Handleanimation (ANIM);} function Setanimation (p:int,o:object[]) {pointer = p;//specified direction is Panim = o;//Specify map array}function handleanimation (tex:object[]) {//Calculate limit frame time + + time.deltatime;//limit frame toggle Picture if (Time >= 1.0/fps) {counter++;time = 0;if (counter >= anim.length) {C Ounter = 0;}} Assigns the corresponding map to the protagonist object Hero.renderer.Material.maintexture = Tex[counter];var Rect:rect = rect (100,100,32,48); Gui. Drawtexture (rect,tex[counter],scalemode.stretchtofill,true,0); Side of the Thumbnail object}</span></strong>

8, after saving, drag the file to hero;

9. Select material for mesh renderer


10, by adjusting the camera and hero position, so that the camera can see Hero;

11. Operation:



Unity uses Gameobject to create simple, movable objects

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.