Unity 3D uses Gameobject to create a simple movable object

Source: Internet
Author: User

In the development of Unity 3D games. The game script requires a 3D simulation combination, the script will be written to drag 3D in order to reach the effect object.

Here's a small example that uses unity 3D to implement a villain that controls movement. 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, for example:


4. Click Add Component. Select Mesh---mesh Renderer, for example, to see:



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



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


7. Create a directory resources in project and create four directories in it: Forward,back. Right,left. These four directories are used to place picture files;

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

<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;//Infers whether the button is pressed with a Boolean value of private Var keyforward:boolean;private var Keyback:boolean;privat  e var keyleft:boolean;private var keyright:boolean;//current map Array private var anim:object[];//current direction private Var Pointer:int = The value of the 1;//direction vector. That is, the size of the speed private var f:float = 0.001f;function Start () {hero = Gameobject.find ("Hero"); forward = Resources.loadall ("woman/f Orward "), left = Resources.loadall (" Woman/left "), right = Resources.loadall (" Woman/right "), Back = Resources.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 ()//Inference button buttonif (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);} *///inference Current Direction 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;}} Assign the corresponding map to the protagonistObject 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. Enable the camera to see the hero;

11. Execution:



Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Unity 3D uses Gameobject to create a simple movable object

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.