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

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/97b630a23234

Previous section Unity-2017.2 official Example Tutorial Roll-a-ball (a) we talked about:

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

If a friend who has run a game will find a problem, although we can control the sphere movement, but the game perspective does not seem to move with the sphere, the sphere will suddenly run to the camera can not see the place to go, then this section we will learn the following:

First, how to let the camera follow the ball movement

Second, make the fence to prevent the sphere from running out of the platform

Third, add a self-rotating animation block

Iv. making a prefab and using the global model

Five, let the sphere can pick up blocks

Vi. How to implement a simple scoring board UI (Text)

Vii. How to publish a game (Build)

First, let the camera follow the sphere to move

First we select our camera (Main camera), and then we see the following situation:

In, we can see that in SceneIn the lower right corner of the window, which is the red box in the picture, there is a small window that shows all the contents of the camera we have selected at this time, so that we can adjust the position of the camera, so we first drag the camera's position and angle to a suitable angle of view, in this case, We set the position Y and Z of the camera to 5 and 6 respectively, and then the X value of its rotation to 45, so that the camera's viewing angle becomes a 45° view angle:

After the angle adjustment, we want to let the camera follow the sphere to move, we first think of the method, but also the simplest way, is to turn the camera into a sub-object of the sphere, so that the sphere movement, the camera will follow the changes, we immediately do look:

Tips: Turning a camera into a sub-object of a sphere: Drag the main camera object onto the Player object

Then we run the game, we find that the camera is actually moving with the sphere, but it is not only the location of the ball changes, his angle will follow the sphere changes, so the camera will appear dizzy effect, this is not the effect we want.

Tips:unity, the Transform property of the child object is changed along with the parent object

We want the effect only need to follow the camera's position to move the ball, Angle does not need, so we can only use the foot to achieve this effect, first we add a script to the camera (you should remember how to add it, do not remember to go back to see the first section of the content, here will not repeat the introduction), we named the script Cameracontroller, scripting language selection C #。 Then we look at the script content:

(1) First, we define a public gameobject variable, which is used to tell the program which gameobject the camera will follow to move

(2), then we define a private Vector3 variable, which we mainly use to record the initial position of the camera, as an offset vector to update the camera's position.

(3), and then we in the Start method, the position value of the camera is assigned to the offset variable, which means that the initial position of the camera is recorded with the offset variable before starting

(4), finally, in the Lateupdate method, the position of the camera is changed to the position of the sphere plus the initial offset position

Tips: In fact, the script uses the Update method to achieve the same effect, but we use Lateupdate (last update) more efficient.

After writing the script, we save the script and then go back to the Unity editor interface, and we'll find a more player attribute in the camera's script component, such as:

This is the property of the public type that we just defined in the script.

Tips: In a Unity script, the properties of public are displayed in the editor

Then we drag the sphere into the player property, such as:

Next we run the game and we can see that the camera can now move normally with the sphere.

Second, make the fence to prevent the sphere from running out of the platform

After the camera can move with the sphere, we see another problem, if the sphere moves beyond the platform's boundaries, it will fall, which is not very friendly, so we then add a circle of walls to the platform to prevent the sphere from running out of the platform

We create a new cube object, and reset his Transform property will find that the cube and the sphere are blocked together, which makes it inconvenient for us to edit the cube:

To facilitate editing of the cube, we can temporarily not display the sphere, we select the sphere, Inspector The sphere below the tick, such as:

In this way, the sphere is displayed in our scene window. Next we can edit the properties of the cube, set its length to into, and move its position to the north edge of the platform, such as:

Then again, we make a wall to the south, west and east of the platform, such as:

Then run it and you can see that the sphere can't run out of the platform.

Tips: Remember to show the player when running

Third, the production of a rotating block has a platform, with a sphere, then we need a block, this block can let the ball collide and pick up, in order to make the block attractive, we let the block can automatically rotate, First we create a cube object (don't forget to create the two standard actions after Gameobject), then we name the cube pickup and set its rotation x, Y, z values to 45, then drag it to a location that you think is appropriate, such as this:

We then add an auto-rotation script to this block, the script name is rotator, and the script reads:

Inside we added a sentence, Time.deltatime is based on the change of time to change the rotation properties of the block, so as to achieve the effect of rotation, save the script, and then run, you can see the box itself to rotate up.

Iv. making a prefab and using the global model

In order to enrich the game, we have only one block is not enough, so we need to create a few more squares, before this, we need to make a prefab (prefabricated), to make a preform has the following benefits:

(1), one-time production, re-use, can be used in any project prefab

(2), once modified, all changes, after we modify the properties of the prefab, all the object properties that use the prefab will change accordingly

And the method of making prefab is very simple, we for resource management convenience, first create a prefabs folder under the Assets folder to hold the prefab file, and then create a new prefabs under the Prefab folder, renamed to Pickup, Drag pickup (the newly created cube) object to Pickup (prefab Resource), a prefab is created, and the steps are created in two ways:

First: Click to select the Prefabs folder under the Assets folder and click on the Assets->create->prefab in the menu bar.

Second: Click to select the Prefabs folder under Assets folder, right click->create->prefab

Then we can copy the Pickup object, and drag the new pickup to the appropriate position, you will find that the drag is not able to keep the Y axis unchanged, so in order to facilitate us to move the position of the pickup object, we first change the scene view to the y-axis mode:

Then change the local mode to global mode:

At this point we can easily adjust the position of the Pickup object, the final effect is as follows:

We can see that there are altogether 8 pickup blocks.

Five, let the ball pick up the block

Next we implement the function of the sphere pickup block, we know through the API that there is a ontriggerenter method can be used when the ball hit the object when the call (trigger), light has this method is not enough, we also need to determine what the sphere encountered, We can not touch anything to pick up, so we need to add a tag to the box that is picked up, and add the tag to the following method:

(1), because we have a total of 8 blocks in the scene, if one of the settings mark, it is a bit too much trouble, remember the above we mentioned the 2nd advantage of prefab? By the way, I just need to mark Prefab.

(2), first we select PickUp Prefab, and then in the Inspector view tag inside add a tag, we named PickUp

Then we can script it, we open the playercontroller script of the sphere, and add the following code:

First here the other variable represents the object that the sphere touches, in this method, I first use the IF statement to determine whether the tag encountered by the object is equal to "PickUp", if equal, I will encounter the object set to not activate (not shown)

Having done this, we can run the game and see the effect. We ran the game and found that the sphere collision box did not pick it up, which is why? The reason is that we did not set the block as a collision trigger (is Trigger), only set as a collision trigger, we can ontriggerenter method to effect, here we still give prefab set to is Trigger property, This property is then available for each block. After setting up, run the game, you can find that the sphere now hit the block can be picked up.

Six, make a score board

Well, our game is basically playable, but we also need to add a bit of excitement to the player, make a scoreboard to encourage players to pick up the block, then implement a scoreboard we need the following elements:

(1), use a variable to save the player's score

(2), and then display the score above the UI

First we modify the Playercontroller script as follows:

At the first red box, we define a count variable to hold the score, and then initialize the value of count to 0 in the start method, that is, we initialize the score to 0 at the beginning of the game, Finally, when we pick up the block, we add the count value to 1. Now we have a variable to record the score, but we haven't shown it to the player, so we need a UI to display the score, we create a text UI, and then we adjust the text position to the upper-left corner of the screen:

Next we modify Playercontrolleragain, as follows:

(1), in front of the code, we need to introduce Unityengine.ui, only then the code will recognize the Text type of the variable

(2), then we define the counttext variable to save the UI Text

(3), then we call the Setcounttext method in the Start method to update the contents of the Counttext

(4), and then we need to update the contents of Counttext each time the sphere touches the block.

(5) and finally the Setcounttext method we define.

Then we save the code and go back to the Unity Editor and drag the text into the Count text property inside the player:

By doing this, we can run the game and see the effect!

Vii. release of the game

The game is here and we're over. We can try to publish the game, Unity release game is very simple:

(1), first select File->build Settings

(2), we select the PC platform, then click Switch Platform

(3), we click the Add current button to add the present game scene

(4), click the Build button and select Save path to complete the release

Finally we find the saved path generated files, click on the exe file, you can run the game.

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

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.