Unity-2017.2 Official Example Tutorial Roll-a-ball (i)

Source: Internet
Author: User

Statement:

This article is reproduced, due to unity version, there are some minor changes in the text, the original address: http://www.jianshu.com/p/6e4b0435e30e

The list of chapters in this article is as follows:

First, how to create a new project (project)

Second, how to create a new game scenario (scene)

Iii. How to add a basic game object (Gameobject)

Iv. How to add lights (light)

V. How to add a script to a game object

First, how to create a new project

There are two ways to create a project, the first of which is to create a project without opening the Unity project, and the second is to build a new project with the Unity project already open, both of which are described below:

Method One: Double-click to open Unity, start the panel to create a new item, as shown in, tap NEW:

Enter the project name, select the location of the project file, and the game mode (3d/2d), click Create Project to complete the project creation.

Method Two: Click File->new Project, and when you are finished, the currently open project will be closed automatically.

The interface after creation is as follows:

The layout of the page can be set according to personal preferences, the setting method is: Window->layouts, or through the top right corner of the drop-down list selection, the drop-down list is displayed as the current layout, there are many ways to choose, you can try.

Second, create a new game scenario (scene)

After you create a new project, Unity creates a scene with the default name, and if you want to create a new scene, you need to save the current scene and then click File->new Scene to create a new scene, or if the scene is default, and the scene doesn't make any changes, Then clicking on the file->new scene does not recreate a scene. All of the game resource files in unity are saved under the Assets folder, and to facilitate resource management, we need to classify resources, which is very helpful for a lot of game resources. First we set up a scenes folder to save all the scene files, there are three ways to create the method, the following steps:

Method One: Click on the menu bar Assets->create->folder, named scenes

Method Two: Right-click on the Assets->create->folder in Project view, named scenes

Method Three: Create a new folder directly under the Assets folder under the project directory, named scenes,unity automatically import the folder into the project

If you want to rename the new folder, you can use the left mouse button to click the folder, and then click (Note: not double-click), enter a new file name.

Save the current scene can use the shortcut key ctrl+s or click File->save Scenes, note the location of the scene to save must be in the Scenes folder:

Iii. How to add Basic Game Object (Gameobject) (i), add game Object--platform (Plane)

After saving the scene, we can add the object we need in the scene, we need to add the first game object is the game running platform, here we use unity comes with the plane object, add an object method has 3, specifically described as follows:

The first method: by clicking the Gameobject->3d object->plane in the menu bar

The second method: created by using the Create drop-down list below the hierarchy view, click Create->3d object->plane

The third approach: create by right-clicking in the hierarchy view, right-->3d Object->plane

After creating the game object, we can rename it a meaningful name to facilitate understanding, here we rename plane to ground, there are two ways to rename:

The first way: in the hierarchy view, click on the Game object plane, and then click (Note: not double-click), enter a new name to

The second way: Right click on the Game object plane, select Rename, enter a new name can

After the name modification, we also need to do a very important thing (the official strong recommendation), we put the platform's Transform property to reset, we found the Transform component in the Inspector view, Then click on the pinion in the top right corner of it and select the Reset option, such as:

Tips: This reset operation, the official is recommended every time we add a new gameobject after the best to do, good habits we develop early

After the platform has been added successfully, we can make some adjustments to its properties, for example, we adjust the size of the platform, the gaze lock to scene view , we can be intuitive to modify the platform's properties, here are 3 useful shortcuts can be used:

W: Switch to Mobile mode, we can drag the platform arbitrarily, change its position

E: Switch to rotation mode, this mode we can rotate the platform arbitrarily, change its angle

R: Switch to zoom mode, this mode we can arbitrarily zoom the platform, change its size

Of course, we can also directly enter the desired values in the Transform , in this case, we set the platform scale X and Z values to 2

Tips: Select a gameobject and then click on the F shortcut to align the camera to your selected Gameobject in the scene window, allowing you to quickly locate

(ii), add the protagonist of the game-the sphere (Sphere)

With the platform, make Roll-a-ball game I also need a sphere, we can use the arrow keys to control the movement of the sphere, the way to add a sphere, and the above to add a platform, just the way we choose is Sphere object. What is the first thing we need to do when the sphere object is successfully created? Yes, it is the official recommendation, we reset the Transform property of the sphere, and then give him a good name, and here we name it player, because in this game, the ball represents the player.

Tips: resetting transform and renaming These two actions, we try to develop a good habit, after creating a new Gameobject, do these two standardized actions

Then we select the sphere, through the F shortcut, to the sphere, we will find that the ball has a half is caught in the platform, it does not matter, we by adjusting his position y value of 0.5, let it just on the platform:

Iv. How to add lights (light)

In the new version, each new project will automatically take a parallel light (directional), so here we don't have to add lights, directly modify the properties of the parallel light can be.
In front of the scene file preservation, the mention of good resource classification can help us better manage the game resources, here we gameobject management is the same, and in Gameobject, we do not have a folder, So we use an empty gameobject to replace the role of the folder:

(1), first create an empty game object, gameobject->create empty

(2), and then reset its Transform

(3), then change its name to Lighting

(4), finally we drag directional light into the Lighting

So we are equivalent to Lighting as a folder, all the light objects in this, easy to manage. Then, in order to make a clear distinction between the ball and the shadow, we are adding a new light source to illuminate the sphere, and we create a new parallel light (directional) so that we have two lights called directional light. , so that it is not easy to distinguish, so we modify the name of the light:

(1) First, we renamed the Directional light established by the system to Main Light

(2), and then renamed the Light We just built

(3), and then drag the Fill light into the Lighting (or we right-click on the Lighting on the->light->directional light, This will not require a drag and drop operation. The new object is already under lighting.

Then we select FillLight, first we set its lighting color to red, so it is easy to distinguish it from Main , and then adjust its Rotation property to -33,- 135,180:

With this modification, we can see a layer of red luster on the sphere.

How to add scripts to the game object (script) with the platform, with the ball, with the lights, the next step we should let the sphere can move up. One of the reasons unity is simple is that his approach is very much in keeping with the logic of our daily life, for example, we want the ball to move, so according to our normal thinking, what we're going to do is tell the ball, let him move., then the implementation of the conversion to unity is to add a script component to the sphere, and then in the script component to tell him how to move, let's take a look at the specific operation: (a), add a rigid body to the sphere (Rigidbody)

A rigid body can add physical effects to a sphere, allowing him to detect physical collisions, enabling us to pick up objects and collide walls later. There are two ways to add a rigid body to a sphere:

(1), through the menu bar Component,Physics,rigidbody add

(2), via the Add Component button at the bottom of the Inspector View

(ii) Adding a script to the sphere first, let's talk about the idea of resource management, and in order to make it easier for us to manage the resources of the game, we'll create a folder dedicated to all the script files:

To add a script to a sphere or to any gameobject, we first select Gameobject, where we select the sphere, which is our Player, and then click Add in the Inspector window. Component button to select New Scriptin the popup menu:

It then goes into the script name and language type selection, where we name the script Playercontroller, and then the language chooses C # (C Sharp):

After clicking Create and Add , we successfully add a script component to the sphere, and then we click the Pinion button in the top right corner of the script component to open the Script Editor by selecting Edit script from the pop-up menu:

(iii), editing scripts

As we can see, there are two methods, start and update, in this case we add a fixedupdate method that is only called when the physical effect is calculated, and then the code is as follows:

Here are a few things to note:

(1), speed in the first red box, variables of public type can be displayed and modified in the Inspector editor

(2), RG in the first red box, a variable of private type does not appear in the Inspector View

(3), the second red box we can initialize the Gameobject object in the Start () function, which can improve the efficiency of code execution

(4), the third red box, input, Vector3, Addforce, and Time.deltatime, can be viewed from the Unity User manual in the corresponding usage

Tips: In the process of adding a rigid component before adding a script component above, if you add the Scripts Script component and then add the Rigidbody rigid body component, the private rigidbody in the script scripts RG will prompt that player does not have rigidbody component, this time need to first remove the script in the Inspector view, add the Rigidbody component first, and then bind the script.

In this example, we can set the speed value to 500, which is a reasonable value, after this, we can run the game to see the effect!

Unity-2017.2 Official Example Tutorial Roll-a-ball (i)

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.