Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right
Article directory
1. Create folder Scenes in the project
3. Commonly used components in the game
2. The collider component Collider
1. Create a script for the character
2. Simple mobile implementation
foreword
This tutorial teaches you how to use Unity to create an RPG game to achieve the most basic character movement, combat, backpack management, UI construction, plot and dialogue.
Project resources and source code:
Link: https://pan.baidu.com/s/101oY6xXZ1HUET7If2FD0gw?pwd=48q5
Extraction code: 48q5
All resource packages used in the project have been added
Before that, open the Unity user manual in the upper right corner. Reasonable use of the manual can make learning easier.
1. Import resource pack
First create a folder Resources in the project to store the downloaded resource pack for later use.
Click Window->Package Manager in the upper left corner
Here you can check the packages subscribed in the store and download them
Most of this project uses free resource packs downloaded from the Internet, here is a download site recommended
Aigei_Sound Effects_3D Model_Video Material_Free Download(aigei.com)
Two, the scene
1. Create folder Scenes in the project
Right click in the folder->Create->Scene to create an empty scene
2. Terrain
Right-click in the Hierarchy column to create a new terrain Terrain
The component Terrain on the right can adjust the terrain size and other information
Click the second icon, the six items in the selection column are
点击Paint Texture->Edit Terrain Layers->Create Layer
Find the texture in the resource and add it Add a normal map to the Normal Map
Adjustable brush size and sensitivity in Brusher
Apply directly in the Scene
The third column Paint Trees is the same, add objects
It is recommended to adjust the brush size to 1, the density to the lowest, and plant trees one by one. Planting trees in a large area may cause lag
Check Random in Tree Height to achieve random tree height effect
Left click to plant trees in Scene
The same is true for Paint Details, so I won’t repeat them here.
3. Commonly used components in the game
1. Transform component
Find the required model in the resource and drag it directly into the scene
Transform component in the game object property bar
Position: the coordinates of the object, Rotation: Euler angle, Scale: scaling
After selecting the object in the scene, you can use the shortcut key qwer to adjust the hand, object movement, angle adjustment, and zoom adjustment respectively.
2. The collider component Collider
Create a new folder Role to store game character resources Import resource packs and add game characters to the scene
The Collider on the right is the collision body component, and the Capsule Collider is the capsule collision body
Edit Collider can adjust the size of the collision body
Is Trigger is a trigger. After checking, the rigid body will not directly collide with the collider object, but directly penetrate the collider.
Trigger information detection uses the following 3 functions:
MonoBehaviour.OnTriggerEnter(Collider collider), fires when the trigger is entered.
MonoBehaviour.OnTriggerExit(Collider collider), fired when the trigger is exited.
MonoBehaviour.OnTriggerStay(Collider collider), fires when stay in the trigger
For example: in a role-playing game, when the player walks to a place, a BOSS event will occur, which can be realized by using a trigger.
3. Rigidbody
properties are
Mass Gravity
Drag Friction
Angular Drag Angular Friction (Air Resistance)
Use Gravity Applicable Gravity
Is Kinematic Whether it has a physical engine effect (its own attribute is invalid)
Interpolate makes the trajectory smoother, and the performance overhead increases from top to bottom
Collsion Detection:
1 .Discrete Normal judgment of colliders
2.Continuous Judgment of fast-moving objects (high performance overhead)
3.Contiuous Dynamic Dynamic judgment of fast-moving objects is more commonly used.
Constrints lock rigid body position and angle
4. Collision
1: Conditions for collision
1: To generate a collision, both sides must have a collider.
2: One side of the movement must have a rigid body, and it does not matter whether the other side has a rigid body or not.
Note: If there is no rigid body on the moving side, it will collide with a static rigid body, which is equivalent to not being installed.
Two: Two ways of contact
1: Collision collision, which causes a physical collision, and the OnCollision event can be executed when the collision occurs.
2: Trigger triggers, cancels all physical collisions, and executes the OnTrigger event when triggered.
4. Introduction to script
In unity, most operations are implemented by scripts carried by game objects.
1. Create a script for the character
After selecting a game object, you can add scripts to the character in the property bar
Click Add Component
Enter twice after entering the script name
open script
Awake: Called once immediately when the object is loaded, often used for initialization before the game starts.
OnEnable: Called whenever the scripting object is enabled.
Start: Called 1 time when the script object is enabled, executed later than Awake.
Update: Called once per frame.
2. Simple mobile implementation
(1)Vector
1. Vector represents the meaning of vector and vector, including size and direction;
Vector3 can be derived from the name to represent a three-dimensional vector, including x, y, z three components. Generally, the position, scale, rotation and other attributes under transform can be changed by setting the value of Vector3 to change its corresponding position and size.
(2) Achieve mobile
Idea: Get the transform script of the current object, and move forward to increase the X in the position.
After obtaining the keyboard input W, X increases by 0.01 every frame.
void Update()
{
if(Input.GetKey(KeyCode.W))//获取输入
{
transform.position += new Vector3(0.01f,0,0);//transform为当前脚本所搭载物体的transform组件,
}
}
Summarize
This chapter introduces the basic operation of the Unity engine and the initial understanding of the script. See the next chapter for the development of the complete game.