2D project experience summary
- Preface
- map drawing
- player movement
- Implementation of multi-stage attacks for players
- Use of 2D animation
- Collider
- Various status determinations of enemies
- Camera related uses
- Sound effects
- Mobile screen control
- Game pause function
Preface
This article will not be very detailed, but will briefly record the experience of using it at that time.
map drawing
Sprite Editor
Used to cut pictures and cut out the pictures that need to be used in the atlas.
Notes on overlaying (Knowledge points related to SortingLayer)
The lower the Sorting Layer is, the later the rendering order is.
For the same Layer, the larger the Order in Layer is, the later the rendering order is.
How to use Tile Palette
First open the Tile Paltette under the WIndows panel.
Then Create New Palette
and drag the cut album in.
Then click the brush, select the picture, and then draw in the Scene panel.
Animated Tiles (animated pictures (also called tiles or tiles))
Create Animated Tiles in the Project panel, and then select the number of pictures to be animated. In the picture, the waterfall is four grids, so add four grids, and then fill in the waterfall pictures in the corresponding order. A total of three Animated Tiles will be created, and then they will be Drag them into the Tile Palette and select the pictures they draw to be the moving waterfall.
player movement
Three components are needed to realize player movement, rigid body, collision body, and unity input system.
Rigid body mainly realizes the physical characteristics of the player, such as movement and jumping.
The collision body is mainly responsible for interacting with the terrain and where to move.
The input system is responsible for receiving the player's instructions.
Flip of player direction
One is to change the Scale value in the corresponding direction, and the other is to change the Filp of the Sprite Renderer. Both methods are acceptable, depending on personal preference.
rigid body
It mainly realizes the physical properties of the object. There is a point to note here, which is to lock the rotation of the Z axis. Because the character operated by the player is actually a picture, it is likely to rotate around the Z-axis when it encounters a bump in a corner, so the Z-axis needs to be locked.
important point
The player's force in the up and down directions generally uses the value that comes with the rigid body. Because the player is affected by gravity by default, changing it to 0 will cause the falling speed to be very slow.
Collider
Collisions between the player and the terrain are triggered using colliders. You can adjust the shape and size of the collider yourself. Characters generally use ellipses.
Input system InputSystem (new version or old version)
old version
The old version is to obtain the value entered in the InputSystem, and then perform corresponding operations based on the obtained value.
New version: Added visualization tools.
For example, I want to add a jump event to the player character.
Or maybe I want to monitor the player's up, down, left, and right key movements.
Get the corresponding data and then give it to the rigid body to perform the corresponding movement or jumping operation.
player movement
Just change the speed of the rigid body.
player jump
There is something to note when the player jumps. If there are no restrictions, the player can keep jumping until he jumps out of the screen (crazy bird). Therefore we need to add some restrictions to players' jumping.
Implementation method 1: Real-time detection of collisions under the feet.
Detect collisions under the feet in real time, and set the jumpable state when touching the ground, otherwise jumping is prohibited.
Implementation method 2: Register through events
If an event has a beginning, then it will have an end, so we can set a number of jumps that can be -1 when the start of a jump is detected. This method can also achieve advanced continuous jumps by eating props (such as the I wanner game). Each time you jump, it is judged whether there are still jumps left, and if not, jumping is prohibited.
Implementation of multi-stage attacks for players
Animation implementation method
The implementation is very simple, as shown in the figure below:
The Has Exit Time parameter is used here.
First, it is determined that the conditions for entering the next attack are the same. The player is in an attack state and Attack is triggered.
Here is the Exit Time. Pressing attack before part 0.9 will set the player to the attack state and trigger Attack. Otherwise, enter Exit and exit the current attack. Player status will be reset when exiting.
Damage determination method
Triggered via collider.
You need to use a polygonal collision body here, view the character's attack animation frame by frame, and then draw a collision body that is the same as the weapon in the frame when the character attacks, and set this collision body to True, and false for the remaining frames. . Then add a script to the weapon to obtain the colliding enemy and cause damage and attack effects to him.
Use of 2D animation
Common properties
Has Exit Time
This attribute is often used, which means whether switching from the current action to the next action requires the current action to finish playing. Uncheck it to directly enter the next action.
When it is the last animation in the tree or secondary menu, if there is no condition, this attribute must be checked, otherwise the last animation will be directly skipped by Unity.
Make frame animation
Insert the corresponding picture into the Animation panel. Multiple items can be changed in the same frame, such as the picture and the alpha value of the picture. As shown in the figure below, the animation of the protagonist being attacked is also flashed.
Animation tree (BlendTree) (usually used for switching between jumping state or running and walking state)
That is, animation trees can be used when a series of animations are controlled by one value.
Here we take the player's jumping distance, and the jumping actions are divided into: jumping, upward acceleration, hovering in the air, downward acceleration to the ground, etc. They can all be controlled by the player's vertical speed.
Secondary animation menu (State Machine)
It is to add a new animation panel to the current animation panel, which is equivalent to adding another class to the current class. There are separate judgment conditions inside. Unlike the animation tree, the values inside can also be judged individually.
important point
Some animations require changing the collision body at the same time
For example, in the squatting animation, when the player squats, the collision area is actually smaller, so the area and offset of the collision body need to be changed (the collision body does not support anchor points, so the entire collision area is zoomed in and out)
Collider
Common properties
Layer Overrides
You can choose which layer levels to interact with and which ones not to interact with. You can also control the priority of the interaction. The larger the number, the higher the priority (for situations where there are multiple colliders on an object).
The use of multiple colliders (both colliders and triggers) (for one object to have different interaction effects with other objects)
For example, an enemy needs to collide with the terrain, but it is also a trigger for the player character (causing knockback and damage when colliding). There is a very embarrassing point at this point. If isTriger is checked, it will not interact with the terrain and will fall directly. If it is not checked, it cannot interact with the player to trigger damage and knockback, so only two colliders need to be used. That's it, one as a collider and one as a trigger.
Various status determinations of enemies
hit the wall
Add a collider in front of the enemy and try to detect whether it collides with the wall. If it collides with the wall, it will turn around.
Whether to fall off a cliff
Add a collider at the front and bottom of the enemy to detect whether there is a road below the enemy. If there is no road, return.
Camera related uses
camera following
Using the cinemaachine component, select the 2D camera
The camera range is locked and the display area is specified.
Add Add Extension, select Confinder2D, and then select a collider in the map to set the range for it. Note that this collider must be used as a trigger, otherwise it may block the protagonist's way.
Using a camera to create a sense of attack jitter
Add the Inpulse lister, add a related script to the game, and set the relevant parameters.
Sound effects
Audio Mixer
Classify sound effects so that one unified control of all volumes can be achieved, and individual sound effects can control themselves separately.
Mobile screen control
Just add a picture and then add an On-Screen Stick to achieve it. This is used in conjunction with Unity's new input system.
If there is an error, just change this parameter.
Game pause function
Controlled by Time.timeScale, 1 means normal and 0 means paused. This value can also control the speed of the game, depending on personal needs.