当前位置:网站首页>[unity entry program] make my first little game
[unity entry program] make my first little game
2022-07-25 07:33:00 【flashinggg】
Catalog
1 Import playground Project resources
3.2 adjustment Game View background
3.3 Inspector -> rename & change Tag
About Unity The label of (Tag)
5 Add obstacles and collisions
add to ModifyHealth Script components
add to 2D Rigid body components
5.3 Add collision -> The spacecraft
6.2 Realize the operation of reducing blood volume
Learned from Unity course
my Unity edition
2021.3.6f1c1
1 Import playground Project resources
Get into assetstoreUnity Asset Store - The Best Assets for Game Making
Search for Unity playground preservation , Again from Unity Import Assert that will do .
palyground The project is an official introductory video for novices , Preset a encapsulated development environment , Let's get started , To get familiar with the game concept and production process , It's easier to operate !
2 Create a new scene
Assets -> Scene Right click -> Create -> Scene, Create a new scene .
Click the name to rename , Name it PlaneAsteroid

Double click this Scene object , On the right side of the Hierarchy The window will show the current scene object .

By default, the engine will also create : Player camera Main Camera And directional light Directional Light Two objects .
3 Create player ships
In this “ The spaceship ” In the small game of , Players operate spaceships , Therefore, the player object of this game is the spaceship !
We will use the imported playground The resources in the project are ok .
Asesets -> Images -> Spaceships, Choose your favorite spacecraft , Add to scene , There are two ways to add to a scene :
- Drag directly to Scene In the view , This method can directly implement objects stay Scene Positioning in
- Drag onto Hierarchy Window , This method can be implemented in Create a parent-child relationship while joining

3.1 Resize position
The initial spacecraft was too big , stay Game Click Run in the view , Is this

So we need to shrink it a little , Exit the running state -> Choose SpaceshipRed object -> R key , Zoom .
meanwhile , If you want to zoom accurately , On the right side Inspector window -> Transform Under the hierarchy Scale, Enter a value .
It can also be opened Scale Lock button , Realization XYZ Three way proportional scaling , What I choose here is to zoom to 0.2.

You can stay here again Game View run view , The size is more comfortable .

3.2 adjustment Game View background
In the course UP The background color of the game view is different from mine , I also want to make it solid , I found it on the player's camera Main Camera Of Inspector Change in .

Main Camera -> Inspector -> Camera -> Clear Flags&Background, You can see the initial sky box , This is changed into Solid Color And set the color to black .


3.3 Inspector -> rename & change Tag
According to the tutorial , stay Inspector Change the name of the spacecraft in the window -> ship, stay Hierarchy The name in the window will also be automatically synchronized .

About Unity The label of (Tag)

unity official : label - Unity manual
- stay Unity in ,Tag Is a reference word assigned to one or more game objects , Play the role of identification 、 The role of distinction ;
- The same kind of model , It can be set as a unified label ;
- You can find objects by labels ;
The rest, such as how the code is written , later , Here, give our spacecraft an object first Player Of Tag.

4 The ship moves
Direct use playground Written in the project Move Script will do , Click on the one on the right Inspector Look at the code .

So how to assign the script to our spacecraft ? There are two ways :
- A way for laymen —— hold Move Drag script to ship Of Inspector In the window ;
- Compare the suggested way —— from ship Of Inspcetor Window Add Component add to
After adding , You can see that there is one more attribute Rigidbody 2D And a Move With Arrows, about 2D Rigid body components Rigidbody 2D I have made a preliminary study summary :Unity Study -2D rigid body Rigidbody 2D
the other one Move With Arrows It is a component created by programming to control game objects through the keyboard .

5 Add obstacles and collisions
5.1 Add obstacles
Asesets -> Images -> Asteroids, Choose asteroids to join Scene in , And size it .

5.2 Add collision -> obstacle
add to ModifyHealth Script components
playground A script component for changing blood volume has been packaged for us in the project .
Asserts -> Scripts -> Attributes -> ModifyHealthAttribute
adopt Add Component Add this script component to the obstacle just now , When joining, we need to choose :

This component needs to add a 2D Colliding body (Collider 2D) To achieve collision , Here you need to choose the type of collision , Because of our obstacles —— Asteroids are irregular objects , Here you can choose polygon.
About 2D Colliding body , You can see another blog I introduced :Unity Study -2D Colliding body Collider 2D_flashinggg The blog of
You can see , This encapsulated component contains :
- Modify Health Components —— Realize the operation of blood loss after the aircraft touches it

Polygon Collider 2D 2D Colliding body —— To achieve collision detection , Both planes and meteorites should be collision bodies , Therefore, we need to add a collider component to the meteorite , Be careful , This cannot be a trigger , So we need to get rid of Is Trigger Check box ;

add to 2D Rigid body components
We also want to achieve the effect of a little movement after the meteorite is collided , Therefore, we need to give a meteorite Rigidbody 2D Of 2D Rigid body components , And properly adjust the parameter value of the rigid body , Do you , Simulate the real effect .

5.3 Add collision -> The spacecraft
It's mentioned above , To achieve collision, both must be collision bodies , Therefore, we also need to add one to the spacecraft Polygon Coliider 2D Components .

6 Add blood reducing effect
6.1 Add user interface

playground The project also encapsulates the components of such a user interface , Search for interface find UserInterface Drag the component into the... On the right Hierarchy Just a window .

6.2 Realize the operation of reducing blood volume
Blood volume control is accomplished by the following two parts
- Obstacles have Modify Health—— Change your HP after collision

- The ship's own HP , Need to add a Health System Component to set the initial health


After all the above , When the mobile spacecraft collides with an obstacle meteorite , The upper left corner will reduce HP .
7 Add background
Still use the drawings provided by the project ,images -> Backgrounds, hold Space Drag in Hierarchy Just go .
However, the size of the drawing and the background drawing need to be modified 、 The plane 、 Layer order of obstacles , Avoid obstructions .
7.1 Adjust the layer order
The layer order is in the Sprite Renderer Revision in China , my unity2021.3 Version can be modified directly layer In order to adjust :
- BG_Space -> 0
- ship&Asteroid2 -> 1
We can avoid the background blocking our spacecraft and obstacles .
7.2 Adjust the background size
Here the size is set to

Final effect :

8 Add precast units
Because the game has more than one obstacle , Multiple obstacles will also be added , But a series of properties of obstacles should be the same , And that's where it comes in unity Prefabricated parts in , Namely A reusable resource template .
About the creation and use of prefabricated parts :Unity Study - Preforms Prefab_flashinggg The blog of
After adding several obstacles , Adjusted the size and style , The result is shown in Fig. :

9 Add stars to get points
This is the last part of the game , If you need to join, you can ” eat “ And ” eat “ The function of getting scores after .
9.1 Join the stars
Project Window search star, And drag in Hierarchy window

It needs to be revised Star Layer order ,Order in Layer Change to 1 that will do .

9.2 Add the score component
It's still Star Of Add Component -> Search for Collectable

After joining, you can find , Synchronization requires the addition of an impactor assembly , choice polygon Just go .
Be careful , there Polygon Collider 2D Of Is Trigger You need to check , Here we can reach “ Touch the stars and eat ” The effect of .

Collectable The above content in the script is the implementation : After the plane hit the stars -> score++ -> Stars disappear , The effect of , Specific preparation will be carried out after C# Learning from .
10 Grouping is easy to manage
The operation part is over ! Pretty simple , Finally, you can group objects of the same kind , Easy to manage .
It's the end of the first Unity The practice of , Continue to work hard !

边栏推荐
- How does uxdb extract hours, minutes and seconds from date values?
- J1 common DOS commands (P25)
- Ask the bosses: MySQL CDC stores configuration data, and Kafka has history
- Gan series of confrontation generation network -- Gan principle and small case of handwritten digit generation
- JS note 17: the whole process of jest project configuration of typescript project
- Configuring WAPI certificate security policy for Huawei wireless devices
- toolbar的使用
- JS cannot get content disposition in headers
- Luo min's backwater battle in qudian
- Learn no when playing 9 | enterprise knowledge management is so simple because it uses
猜你喜欢

J1 常用的DOS命令(P25)

Oracle19采用自动内存管理,AWR报告显示SGA、PGA设置的过小了?

集群聊天服务器:项目问题汇总

深度学习之快速实现数据集增强的方法

【论文笔记】Progressive Layered Extraction (PLE): A Novel Multi-Task Learning (MTL) Model for Personalized

轮询、中断、DMA和通道

Line generation (matrix ')

华为无线设备配置WPA2-802.1X-AES安全策略

BOM概述

Configuring WAPI certificate security policy for Huawei wireless devices
随机推荐
Problems during nanodet training: modulenotfounderror: no module named 'nanodet' solution
[notes] search rotation sort array
QT6 with vs Code: compiling source code and basic configuration
Growth path - InfoQ video experience notes [easy to understand]
转行学什么成为了一大部分人的难题,那么为什么很多人学习软件测试呢?
Oracle19 adopts automatic memory management. The AWR report shows that the settings of SGA and PGA are too small?
9 best engineering construction project management systems
【Unity入门计划】基本概念-2D刚体Rigidbody 2D
12 combination methods and risk interpretation of database architecture optimization (books available)
【论文笔记】EFFICIENT CNN ARCHITECTURE DESIGN GUIDED BY VISUALIZATION
Flinkcdc2.0 uses flinksql to collect MySQL
YUV player
【ES6】函数的参数、Symbol数据类型、迭代器与生成器
[cloud native] the ribbon is no longer used at the bottom of openfeign, which started in 2020.0.x
QT学习日记20——飞机大战项目
JS cannot get content disposition in headers
Learn no when playing 10. Is enterprise knowledge management too boring? Use it to solve!
做好项目管理的10个关键点和5大措施
Million level element optimization: real-time vector tile service based on PG and PostGIS
Elasticserach里delete_by_query的机制是什么?