Although learning to speak Unity and C# was probably the easiest of all the things I had to learn. But that’s a relative concept, and it doesn’t mean that learning these things is an easy thing in and of itself. Many people can’t claim to be proficient in Unity or C# after seven or eight years of learning. To say that they are simple is to say that they are simple in comparison with painting and music, for the latter two may take a lifetime to master.

Anyway, I don’t need to be proficient in Unity. I just need to be able to put my ideas into practice. For now, my goal is just to get through the Brackeys tutorial and the Unity documentation.

1. Install

1.1 How to Escape Unity in China

It has been heard that China Unity does all the bad things. So I found this website before I installed it. Domain names are interesting.

1.2 installation

  1. Unity Hub: Ask me if I want to switch to beta Downloaders. It looks a lot better and looks like Unreal has learned a lot.
  2. Unity ontology download: Check out the release notes on the official website. The beta version has more features than the stable version, but that’s not to say it’s unstable. Alpha is the version that is unstable and buggy. So I downloaded the beta.
  3. Additional download options are as follows:

1.3 Download Failure

Crazy download failure, and download failure needs to start from 0 to download again, absolutely. It took two hours to download, and I don’t expect it to get any better today.

2. Start

Create a 3d empty project. Then you’ll see the Unity interface. Long like this:

2.1 UI Introduction

2.1.1 hierachy

The panel on the left shows the hierachy of the current project.

  1. You can see that by default there is something called sampleScene, which as the name implies is a scene (scene in Unity is a level).
  2. And then the two that can be expanded below that are called objects. One for the main camera and one for the directional light.

2.1.2 inspector

  1. When you click on any object, something is displayed on the right.
  2. These things are also hierachy, the things that can be expanded are called components. . Component can make an Object do all sorts of things. The things in the expansion are called Properties.

2.1.3 project

  1. The long one down here is the Project Panel. Inside is the game asset, which you can drag directly onto the screen from here. It was shown in hierachy.

The upper right corner can change the view display, traditional art ability now.

2.2 Creating a Ground

  1. Create a new 3D cube in hierachy view.

Select an object in the central view and press F to automatically focus on that object.

  1. The location where the item appears is not where we want it to be, so after selecting cube, right-click the Transform Component in inspector and hit Reset. This resets the position and zoom of the item.
  2. There are several tabs at the top of the center screen that represent different views, and when you select The game, it becomes what the game actually looks like. These view tabs can also be dragged to change the layout. For example, drag the game to the right so you can see what the game screen looks like at the same time. Let’s do that.

4. Change the transform to make it longer and wider. It becomes a floor. You can change the name of an object in the upper left of inspector. I’ll call it ground.

2.3 Creating a Role

  1. Still use cubes as game characters. Reset after creation. You see, we reset the ground before, and we reset the character now. It’s much easier to adjust the distance between the two.

2.3 the material

The character is now as white as the ground and is indistinguishable. So use material to change the appearance of the character.

  1. Material is the asset that you want to create in the Project Panel.
  2. Material also has a number of properties that can be changed in Inspector.
  3. Drag material onto object to use. Properties changed in material can be displayed on the screen in real time.

2.4 Adding Physics

  1. Drag the character up a little bit. Click Play at the top of the center. The cube does not fall when nothing happens. Because we didn’t do anything. We didn’t add anything.
  2. So we need to make this object interact with the Unity physics system.
  3. We saw earlier that Component makes an Object do all sorts of things. So we need to give object a new Component.
  4. Let’s add the RigidBody component so our cube can fall down. Then we rotate it to make it more interesting to fall. (QWERTY shortcut can quickly change the drag, position, rotate, zoom and other tools)

2.5 component

Take a closer look at Cube components. Except the RigidBody we added. There are many others.

  1. A transform is a basic property of every object.
  2. Mesh Rendered. Render object, if deselected. The item will not appear on the screen. But it still exists, it just doesn’t show.
  3. The Box starts. As the name implies, collision-related components. Items have no collision effect when unchecked. In the presence of gravity, they fall in defiance of the ground. Touching other objects does not affect each other. The Box Collider and RigidBody work together to provide realistic physics.
  4. Mesh Filter. There’s nothing to say, just one property, change shape. If we change it to a sphere. Notice that we still have a Box Collider inside our component. So the collision volume is still Box. I can delete the Box Collider and replace it with a Sphere Collider.

Properties can still be changed while in the Play state. However, all changes are undone when you exit the play state.

2.6 skybox

Skybox can be changed in camera. (Changed in the Clear Flags property)

2.7 Maximize View shortcut key

Press Shift + Space to magnify the view over which the mouse is located. Similar to the Adobe family of ~ keys.

3. Initial contact with C#

Not new to it, a little grammar, but only a little.

3.1 move the Script

Add code to an Object using Script in Unity.

Script is also a component.So Add it with the Add Component. We add a Script component to our cube.

After adding it, you can see a Script file appear in the Project Panel. This script can naturally be placed on other objects as well.

You can see that we now have a Script component on our cube. Double-click the file name in the property below the component to open the code editor (mostly VS) to edit it. (Or double-click on the file in the Project Panel)

3.1.1 the Start ()

The start function (or method? The code in) will be executed before the game runs, as can be seen from the comments. Use debug.log () to print. It looks a lot like console.log(), but actually when you go back to Unity and run the game. You can see that it is in the lower Console Panel (the TAB next to the Project Panel) that you want to print the item.

3.1.2 Linking other Components

This time we will create a new variable above start of type RigidBody, obviously to control RigidBody. I’m going to arbitrarily call the variable rb.



Back in Unity, you can see that the Script component has an extra RB property. However, the value is null (None).



We want this RB variable to be linked to RigidBody on our Cube to achieve control over RigidBody. To do this, simply drag the RigidBody component on cube onto None. (Unity has type checking and you can’t drag other components in)

Then you can control RB in VS. AddForce(0, 300, 500) will cause our objects to fly forward (assuming y and Z axes are up and forward).

3.1.3 the Update () and Time. DeltaTime

We want to keep the item moving, so we can use Update, we can apply a force to the item in Update. Since the Update is performed once every frame of rendering, the goal of continuous movement is achieved.

But this is actually buggy, because Update is everyframeRun it once. However, we all know that frame rates vary from computer to computer. And even the frame rate of the same computer is unstable. At high frame rates, the cube is experiencing more force per second, and at low frame rates the cube is experiencing less force per second.

Here’s the solution:Here,Time.deltaTimeWhat is it? It actually refers to the elapsed time of the last render frame (in seconds). So it’s a balancing act.

Roughly speaking: Frames per second * seconds per frame = 1 or, to be exact, in a second: time spent on the first frame * 600 + time spent on the second frame * 600 +... + time spent on last frame * 600 = (time spent on first frame + time spent on second frame +... + time spent on last frame) * 600 = 1 * 600Copy the code

So the force on cube per second is fixed regardless of the frame rate. That’s why we changed from 3 to 600, because that number now represents the force per second, not the force per frame. That is, in Update, multiplying a number by time.deltatime changes the meaning of the number from changes per frame to changes per second.

3.1.5 FixedUpdate ()

There is no problem with the previous code. But it’s not Unity style. Unity expects you to perform physical operations in FixedUpdate, not Update. Simply change Update to FixedUpdate.