当前位置:网站首页>Unity2d game let characters move - Part 1
Unity2d game let characters move - Part 1
2022-07-24 06:13:00 【It's really all right, duck】
After watching the video of Mr. Mako's little fox , In order to consolidate the practice , I'll find another material bag to practice , By the way, write a tutorial to make yourself more proficient , At the same time, when you forget, you can look back . in addition , I wrote this tutorial in great detail , Mengxin can also learn . What's wrong or questionable , Welcome to correct and consult .
First, import the material

Then we can browse the material package first , One is Assets, One is the test scenario demo, We mainly look at Asset What's in it ,background Inside is our background picture ,Items It's all kinds of things ,Main Character What we store is our role , Here are four characters , Let's just use the first character ,Other It's something for color matching ,Terrain It's our terrain related material ,Traps There are many traps , Used to increase gameplay . That's all for the material package , In fact, the main purpose of this section is to make the characters move , So not many things are used , One is the role , Or is it terrain material .
Let's first put all the pictures of the character Pixels Per Unit( Per unit pixel ) Such as 32, Because the material is 32 Pixels *32 Pixel

Then create an empty component (Hierarchy Bar right button ->Create Empty) Name it player, Dexter transform Remember reset( Reset ) once


stay player Add a component called sprite Renderer( Sprite renderer ), This is the model used to add our characters

Put the character's idle Drag the pose of the first frame of to the one just now sprite Render Inside Sprite

So we create our characters , And you can Scene See our role in the column

Then we simply set up the map
On the left hierachy Create a view tilemap,tilemap yes unity A plug-in for , It's very convenient for us to 2D Game editing work

Name it platforms

Then we go from Window->2D->Tile Palette Open up our tilemap The editing interface of

Creat New Palette Create a new panel , Name it Map, The position can be placed by yourself , But for better management in the later stage, I created a folder for such things , stay project Bar right button creat You can create new Folder, We can create one here Map Folder for our tilemap
In the material package Terrain There are two in it png picture , These two are the basic materials we compiled , The first one seems to have no segmentation , So we use the second , Remember to put it on the right Inspector Change the pixels there to 16

Then drag the picture directly to Tile Palette, Choose the location we created Map Folder

go back to Hierarchy, Click on platforms, At this time, we can edit our terrain , go back to Tile Palette, Let's first add a simple platform , Choose... Directly Tile Palette The box inside is directly dragged to Scene Inside

So our first platform is ready
We also need to give platforms Add a collider
We click platforms, Click... On the right Add Component( Add the component ) Add one Collider Components , Search for tilemap collider 2D, Just choose .

So we add a collision body to our terrain , This collider It can be used to detect whether there is a collision between objects , It can also be used as a trigger . Here we can simply think of adding collider Then the terrain has entities , Characters don't go straight through , And it can also respond to the corresponding collision event , For example, a character will trigger an event after touching the terrain , This event can be used to write animation of character jumping .
Since the terrain can be added collider, Then our characters can also add collider, We also add one to the character collider, What we add to the character is a circular collider , There is also a box collider called Box Collider 2D, We don't need this , Considering that the material has no squatting action and the box collision body is easy to get stuck with the terrain .

And then we click Edit Collider Edit this circular collider , Adjust its size to a suitable position


After we click Run, we can find that the character has stood on the platform
Then we need to move our characters , The role must have a force to move , And also respond to the trigger of our keys , This force is ours Rigidbody 2D, This component can add x Direction and y Directional force , In this way, the character can move , So we add this component to the role

But remember in the constraints( constraint ) There lock z Axis , Don't let z The shaft moves , because z If the axis moves, the character will roll

There is also the trigger of the key ,Edit->Project Settings->Input Manager, So we enter the key setting

Pull open Axed We can add or change the keys we want , Like this Horizontal, This is x Key position in axis direction ,a and d perhaps left and right,Vertical Namely y Key position in axis direction

It has already been bound for us , We don't need to make any changes
In order to make the character move, we have to add animation to the character
Create a folder called Animation To store animation resources
Right click in this folder ->create->Animation Create an animation named Idle

Remember to check in the attribute bar Loop, Let's have Idle Animation loop

And then put this Idle Dragged to us player, We will find an extra one named player Of Animator Controller

At the same time player Of Inspector There is one more column Animator, We found that Animator Controller It has been automatically created and added for us , Is this very convenient

And then we passed window->Animation, open Animation and Animator

choice player, stay Animation There is only one column Idle Animation , We give this Idle Add animation sequence , We said that above , The character's animation is Main Character The folder Mask Dude Inside , choice Idle Drag all the pictures inside to Animation Inside the fence

take Samples Change to 15, You can play the animation and try ,Samples The larger the size, the faster the playback speed
Empathy , We create a run Animation , choice player, stay Animation bar , spot idle Next you can create a new animation

Name this new animation run, And then put run Drag the animation sequence of ,samples Change it to 20 Just go

We have not only opened Animation bar , And opened Animator bar ,Animator What is the column for ,Animator It is used to switch animation , Open us Animator You can find two more States , One idle One is run, Let's right click idle,make transition Connected to the run, Again run It's the same thing , In this way, we can add some conditions to switch the two animations


stay Parameters( Parameters ) Click the plus sign there to add a variable as the switch between animations
Let's add a bool Variable representation run state

So we idle and run The connection between them can set the conditions when they switch , Let's click idle toward run Arrows , We can set when to start idle Switch to run, Remember to put the property bar on the right Has Exit Time Uncheck the , And the settings Inside Transition Duration Set to 0, In the future, the connection between all animations should be set like this

And then down there Conditions You can add conditions , We can judge run When true, from idle The state switches to run state

from run State to idle The status setting is similar to the above

By analogy , Let's create two more animations , And set the conditions for switching between them
Namely jump( Take off ) Animation and fall( whereabouts ) Animation , I won't explain it one by one here , Here the relevant operations are posted , But remember to go Animation In the folder jump and fall Two animated loop time Uncheck the , Playing these two animations once is enough



Run->jump and idle->jump The conditions are the same , All are jump by true

Jump->fall Is the condition of jump by false,fall by true

Fall To idle Is the condition of fall by false

In this way, we have finished the preparatory work besides the code .
Then let's start coding .
link :unity2D The game makes the characters move - Next _ It's really okay duck's blog -CSDN Blog
边栏推荐
- Detailed explanation of KMP code distribution
- Dameng database_ Common user management commands
- 【数据库系统原理】第五章 代数和逻辑查询语言:包、扩展操作符、关系逻辑、关系代数与Datalog
- QT char to qstring hexadecimal and char to hexadecimal integer
- Openpose Unity 插件部署教程
- HoloLens2开发:使用MRTK并且模拟眼动追踪
- Installation of tensorflow and pytorch frames and CUDA pit records
- PDF Text merge
- [principles of database system] Chapter 4 advanced database model: Unified Modeling Language UML, object definition language ODL
- Iotp2pgate two IOT devices point-to-point communication fast implementation scheme
猜你喜欢

LSTM neural network

HoloLens 2 中文开发文档 MRTK v2

Common features of ES6

Day3 jvm+ sorting summary

论文阅读-Endmember-Guided Unmixing Network (EGU-Net) 端元指导型高光谱解混网络

Learning rate optimization strategy

MySQL download and installation environment settings

Synergy LAN realizes multi host shared keyboard and mouse (AMD, arm)

JUC concurrent programming foundation (9) -- thread pool

JUC并发编程基础(8)--读写锁
随机推荐
data normalization
使用Keras实现 基于注意力机制(Attention)的 LSTM 时间序列预测
Lua Foundation
Bat batch script, running multiple files at the same time, batch commands executed in sequence, and xshell script.
Oserror: [winerror 127] the specified program cannot be found. Error loading “caffe2_detectron_ops.dll“ or one of its dependencies
day4-jvm
Vscode multiline comments always expand automatically
Unity基础知识及一些基本API的使用
Find the number with the most occurrences in the array
Day-7 JVM end
HoloLens 2 开发:开发环境部署
什么是单调栈
JUC并发编程基础(9)--线程池
头歌 平台作业
day1-jvm+leetcode
餐饮数据统计分析---泰迪云课程大作业
STM32 standard peripheral Library (Standard Library) official website download method, with 2021 latest standard firmware library download link
HoloLens 2 中文开发文档 MRTK v2
HoloLens 2 开发101:创建首个HoloLens 2应用程序
JUC concurrent programming foundation (9) -- thread pool