While listening to arashi less live, while continuing to learn Unity.

1. Physic Material

The last time I did cube. The cube rolls forward when it touches the ground. That’s because of friction. To make things roll smoothly, we can remove friction.

  1. Create a New Pyysic Material in the Project Panel. The usage is consistent with Material. Just drag it onto Object. 2. You can then see the asset file in Box Collider. Double-click on the file or the file in the Project Panel to open the Inspector for the Physic Material. You can change its properties.
  2. Set both friction parameters to 0, run the game, and discover that the Cube no longer scrolls.

2. The variable

Why mention variables specifically. Because Unity allows you to see and change the value of a variable in C# Script in Inspector, you simply declare the variable public. Variables can then appear in Script component properties.

Rb, the RigidBody type declared earlier, is also a variable. We then assign the RigidBody of cube to this variable during subsequent concatenation.

By the way, for numeric variables/properties in Unity, hover over the property name to the left of the property line and see the mouse arrow change. It means you can drag it left and right to change the value. I’ll mention it because there’s no obvious hint.

3. The buttons

Update allows you to listen for keystroke events. Use input.getKey (“w”). Also, input events are better listened for in Update, not FixedUpdate. For example, listen for keystrokes in Update. Then assign the value to a Boolean variable, which is evaluated in FixedUpdate.

4. Differences between FixedUpdate and Update

Update calls are made once per frame. But the processing time for each frame is likely to be different. FixedUpdate is called at fixed intervals, and physical calculations are performed immediately after the FixedUpdate call. So any physical operations on RIgidBody should be done in FixedUpdate, because you want your code to change the physical state immediately after it runs.

  • So if you use FixedUpdate you don’t need to normalize with time. deltaTime. (But has its own FixedDeltaTime, is a fixed value. Of course, you can change FixedUpdate while the game is running to adjust its speed.)
  • Even using time. deltaTime in Update is not as good as using FixedUpdate. Because time.deltatime is, after all, the Time spent on the last frame, not this one. Maybe the distance traveled in a second is fixed, but the physical speed is unstable.

However, actions such as keystroke input must be placed in update. Why is that?

  • For example, you hit the jump button, and because it’s the jump button, you only hit it once. And then the moment you press the button, it’s just too late for FixedUpdate to execute, so your button is not being listened to, so you can’t jump, it’s not even a delay, it’s just not being listened to.
  • But if you put it in an Update, it will definitely be listened to.

5. Camera follow

If you want the camera to follow an object, you can drag the camera directly onto the object as its child. But that makes the camera rotate when the object rotates, which is not good. So we use scripts to implement:



We set the camera position to be equal to the cube position plus an offset value.

  • A Vector3 stores three float values.

6. The collision

Create an obstacle that will disable the player when it collides as follows:

PlayerMovement is actually the name of the Sript we created earlier (because the Script name automatically creates a class and then creates a type variable for that class), and then links it in Unity. Finally, turn off its enabled in c# to stop player action.

OnCollisionEnter is called when a collision occurs. One acceptable argument is the collision event. The collider in this event is the object that was hit.

Tags can be added in Unity. In the upper right corner of Inspector, there are some built-in tags, but here we add a new Obstacle tag.

6. Prefabs

Copying Objects one by one is fine when you want to create a group of the same Objects, but when you need to modify the Objects all at once it becomes a problem. The Prefab concept was born. This was created by dragging objects that you wanted to make Prefab into a Hierachy Panel directly into the Project Panel below. It becomes a Prefab, and you can drag the Prefab directly into the central view. You can create the same Objects.

  • If you make a change in the Prefab Inspector, all changes will affect all Objects created using the Prefab.
  • You can also modify individual objects without affecting other objects. To map the change to other Objects, select the Override property above the Object Inspector and select which changes to map to the other Objects.

7. The perspective

The thing in the upper right corner of the Scene view that shows the x,y, and z axis, you can click on the x,y, and Z axis to jump to that axis. Click on the center to toggle perspective.

  1. It’s isometric. :
    • You can see that the ground is parallel, and all the lines in front and behind are parallel, so it doesn’t follow the rules of perspective



2. This is the law of perspective (green line in picture)

In addition to the upper right corner of the screen, there is also an x, Y, and Z axis display when selecting objects (Which you have already seen and used many times). Drag the small square between any two axes of X, Y, and Z to move the two axes simultaneously. Holding down the middle square changes all the axes at once.

8. Layer

When your project is complex, sometimes the scene is just too much. When you left-click the box to select several Objects, you will always select something you don’t want to select, such as ground, grass, etc. However, it is too troublesome to choose in hierachy panel, because these things are not necessarily together. You can’t necessarily tell them apart in hierachy.

At this point you can use Layer to layer your object. We put these environment objects in a layer called environment, and make them unselectable with the mouse.

Click on the ground, and in the Layer above Inspector, you can see that Unity already has some prefabricated layer names, and you can see that all the objects we created have been placed in a layer called Default.

  • Here we select Add Layer to create a new Layer called Environment. (Note that although you Add Layer after selecting the ground, Unity doesn’t automatically bind it to it, so you have to click on the ground and select Environment in the Layer.)
  • Then we click Layer in the upper right corner of the screen (adjust the layout on the left) and make the Environment unselected (the one highlighted in red in the screenshot) :

  • The ground will no longer be selected when the left mouse button is pulled in the central view or directly clicked on the ground. Only in hierachy.

9. snap

In the row of tools above the central view, you can choose to display the grid. And then when YOU hold down the Control key and move the object, it attaches to the grid. Then a button on the right of the Grid tool sets the size of the snap, such as 1, and when you hold down Control, the object can only move one block at a time. You can also set it to 1/3 (you can hit 1/3 and Unity will calculate it automatically) so that the object can only move 1/3 space. It works when it’s aligned.

10. fog

If you can see things from a distance, it will affect your vision. Mix it up with something close by. You can add fog at this time. On the upper toolbar, select Window->Rendering->Lighting. You can drag it to the right of Inspector to stick to a TAB. In the sub-tab called Environment, select Fog. You can adjust the color of the fog (for example, to skybox color), the concentration of the fog, etc

11. Failure of collision detection of high-speed objects

Collision detection can fail if the object is moving too fast.

The solution is to change Collision Detection into Continuous in Obstacle Prefab’s RigidBody component. I’m going to change cube to Continuous Dynamic.

Then reduce Fixed Timestep in Project Setting->Time. Time. FixedDeltaTime. With this reduction, FixedUpdate will run faster, which means physical updates will be faster.

12. UI

We want to create a UI that shows the score earned in the center of the screen.

  1. Create a UI->Text object in Hierarchy. Hierarchy will create a Canvas -> Text Object. Canvas is where you put your UI Objects.
  2. Toggle sets the central view to 2D and then press F to automatically focus to the text you just created.
  3. In Inspector, set Vertical Overflow to Overflow. If the height is too high, the text disappears altogether.
  4. Resize the text. Select text Alignment (center, vertical, etc.).
  5. Next font, drag it directly into Asset. And then switch to this font. Text allows you to change the text string.
  6. Another problem is that you’re zooming in and out, and the font size stays the same. But we want to change it depending on the window size. Then we need to go into the Canvas Inspector and change the Canvas Scaler->UI Scale Mode to Scale With Screen Size. Then drag Canvas Scaler->Match to the far right (to Match Height).
  7. So now we have our UI styled.

Note: The Asset folder is too messy. You can create folders in it. The Asset folder itself also corresponds to the Asset folder in your project folder. When you import files, you simply copy files into that folder.

Next we want to update the text of the text in real time, so we need Script:

  1. Create a Script.
  2. Reference the player’s transform information in Script. I’ve talked about it before.
  3. The code is as follows:

13. Game Over

We want to be able to start over after the game is over, UI presentation and so on. We can use a Game Manager to do this.

  1. We create an empty object in Hierarchy and add a script named GameManager. As you can see Unity automatically recognizes this name and the icon displayed at asset becomes a gear. We reset the game inside with the following code:
  2. Then we’ll just run this script in the PlayerCollision we wrote earlier.
    • So the question is, how do we refer to this script, as we did before, because the script itself is going to create a new class, and we’re going to create a variable store?
    • Unfortunately, that doesn’t work. Because when we reset the game and the player is destroyed and rebuilt, the variables referenced before destruction are forgotten (of course, references to themselves or non-reference types are not), and variables referenced to this Object on other Objects are also lost.
  3. So we’re going to look for it in real time, as follows:
  4. Not only does the game need to be reset when a collision occurs, but it also needs to be reset when the Cube falls out:
    • Of course, the above is not necessaryGetComponentDirect use,rb.position.yJust for demonstration purposes.
  5. There is still a bug, when resetting the game, the light is gone. Because we changed the lighting Settings, but it didn’t generate. And now auto is off. So let’s go to the Lighting panel and Generate Lighting in Workflow Setting.
  6. Then we need to add a delay and wait a few seconds when the game is over before resetting it. The code looks like this:

14. Game wins

14.1 add the Trigger

  1. Create a cube, stretch it where you want to end the game, and win when the player touches it. But we’re going to turn off its Mesh Renderer so we can’t see it.
    • But the problem is, we can’t select it again, so we need to add an icon to select it. In Inspector, you can select icon in the upper left. However, the display may be too small or opaque. We find 3D ICONS at the top of the Gizmos list at the top of the scene view and drag the ICONS to the right to make them more visible.
  2. Turn on isTrigger, so it’s ignored by the physics engine, and the player will ignore it and go through it.
  3. We’ll create a new Script with an OnTriggerEnter function that will be called whenever an Object touches this trigger. This trigger should not overlap with other objects in case it is triggered at the beginning of the game.

14.2 add a panel

We want the screen to become a playthrough prompt when we win.

  1. Right-click on Hierarchy and create a NEW UI-> Panel. This will create a new panel under the Canvas.
  2. Go to the Panel Inspector and set the Source Image to None, which means we’re using a solid color for the background. Set the color. If you find the color translucent, you may have a lower opacity (Alpha, to be exact).
  3. Right-click on the panel and create a new Text, instead of a blank area, so that the text appears as a child under the panel, not horizontal.
  4. Adjust the style you want.
  5. Uncheck the top left of Inspector. You can see that the panel is no longer displayed on the screen. We need to enable it in Script.

14.3 Victory Script

  1. While this can be done in OnTriggerEnter, game wins should be done in GameManager. So we introduce the GameManager in the OnTriggerEnter script. We don’t need FindObjectOfType here because we won’t destroy and regenerate the trigger. Just use a variable reference.
  2. Then enable the panel we just built in GameManager.
// GameObject references the entire Object
public GameObject WinningPanel;
public void Win()
{
    / / enabled
    WinningPanel.SetActive(true);
}
Copy the code

14.4 the animation

It’s not fun to show Pane directly, it’s fun to animate it, so we’re going to animate it.

  1. First, open the Animation window and go to window->Animation->Animation in the upper toolbar. (Drag it to the bottom and attach it better)
  2. Creating an animation opens the explorer and selects the location to place it. Let’s create a new folder called Animation and create it inside.
  3. Then open the Project Panel and you’ll see two files inside. One is the Animation we created. Turn loop off because we don’t want it. The second one is the Animation Controller, which controls the states of the Animation.
  4. Back in the Animation window (with the UI Panel selected), you can see the Timeline. Click the red recording button on the left, the timeline turns red, and you can record all changes. Aniamtion is created using keyframes just like after Effects. Once you’re in recording state, all of your changes are going to be keyFrames. And then you do what you have to do.