当前位置:网站首页>Unity beginner 4 - frame animation and protagonist attack (2D)
Unity beginner 4 - frame animation and protagonist attack (2D)
2022-07-29 07:57:00 【Lin Fusheng】
This article comes from learning chutianbo Teacher's notes , link b standing
About the production of frame animation
About the animation of a character , We need to add a component to this role first Animator( Animation controller ).
Then we use ctrl+6 open Animation panel , Choose us Prefabs The next role , Click Create Animation Cilp, Put the new file animation on assets I created Animation In the folder .
Then we were in Characters Find different forms of material in the folder
Pictured above ,robot Actions are divided into idle,fixed,walk Three states , among walk The animation in is divided into four directions . We use Shift Press and hold to select the attribution down Drag four photos of into Animation You can make frames in the panel .
problem :
1. Action only left, not right 
Use Filp X Just reverse
2. The three symbols on the right side of the name respectively indicate that it is adjusted to the specified frame , Add keyframes , Join the event
3. Try not to make frame animation events lower than 0.10, Otherwise, the back of the hybrid tree tigger It is decided that bug
Use the blend tree to animate the character
After making the animation , We need to add behavioral processes to roles .
In the production animation when , He will automatically create a corresponding animator Click to enter
You will find that every animation we have done before is scattered in it , So according to the classification , We use the right button to create create stateNew Blend tree.
Because robot animation has orientation problems , So we are on the left paramemters Add parameter determination Move X, and Move Y, All are float type 
It's probably like this when it's finished
Remember here 
Change the mixed tree type to two control parameters , Then add your own animation
Finished the animation , Use parameters to control animation
Declare animated objects
Animator animator;
2. stay start Get animation examples in
animator = GetComponent();
3. stay FixedUpdate Add judgment in
if (vertical)
{
position.y = position.y + speed * Time.deltaTime*direction;
animator.SetFloat("Move X",0);
animator.SetFloat("Move Y", direction);
}
else
{
position.x = position.x + speed * Time.deltaTime * direction;
animator.SetFloat("Move X", direction);
animator.SetFloat("Move Y", 0);
}
rigidbodyroot.MovePosition(position);
About the protagonist's attack
Animation production is the same as above and will not be repeated , Here we talk about the main character shooting bullets
1. Add rigid bodies and collision bodies to the bullet preform , At the same time for bullets projecttile Prefabrication hanging script
Rigidbody2D rigidbodyproject;
// Start is called before the first frame update
void start()
{
rigidbodyproject = GetComponent<Rigidbody2D>();
}
public void launch(Vector2 direction,float force){
// Give the bullet a firing force
rigidbodyproject.AddForce(direction*force);
}
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log($" I met {collision.gameObject}");
// The bullet disappears when it hits the entity
Destroy(gameObject);
// When the bullet meets the robot , Repair robot
EnemyController2 emenyController2 = collision.collider.GetComponent<EnemyController2>();
if (emenyController2 != null)
{
emenyController2.Fix();
}
}
private void Update()
{
// If there is no collision body , Too far automatic destruction
if (transform.position.magnitude>100)
{
Destroy(gameObject);
}
}
2… Add
public GameObject projecttilePrefabs;// It means to obtain bullet prefabricated parts
3. Add the launch function for the protagonist
void Launch()
{
// Create an object at the specified location
GameObject projecttileObject = Instantiate(projecttilePrefabs, rigidbody2dRuby.position + Vector2.up * 0.5f, Quaternion.identity);
projecttile projecttile = projecttileObject.GetComponent<projecttile>();
projecttile.launch(lookDirection, 300);
}
Above Instantiate Indicates that the creation entity is unity One of the api The first parameter is the object , The second is the creation location , The third is rotation
,Quaternion.identity Indicates no rotation ( Here we can go back to the time when we first created the protagonist mobile , If freeze is not checked z Shaft appearance )
4. meet bug 233
We will find the first thing ,unity An error will be reported, indicating that the rigid body of the bullet preform is obtained as null, This is because , The following is unity Official explanation :
This is because when you create objects Unity Will not run Start, Instead, it starts running at the next frame . therefore , Call on the missile Launch when , Just instantiate (Instantiate), Do not call Start, therefore Rigidbody2d Still empty . To solve this problem , Please put Projectile Script void Start() The function is renamed to void Awake().
And Start Just the opposite , When you create an object ( call Instantiate when ) Will immediately call Awake, So in the call Launch Initialized correctly before Rigidbody2d.
You can go deep into this problem MonoBehavior Life cycle of
5. Finish this bug, Then we will continue to meet bug
We found that the missile was created , But the protagonist disappeared in an instant , So here we need to use layers , Put the protagonist and the prefabricated missile on different layers , At the same time EditProject Settingphysical 2D Uncheck these two layers ( That is, no collision )
The material used in this article comes from unity The store Ruby’s adventure
link unity Official website
边栏推荐
- Output 1234 three digits without repetition
- Excellent urban design ~ good! Design # visualization radio station will be broadcast soon
- [cryoelectron microscope] relation4.0 - subtomogram tutorial
- [deep learning] data preparation -pytorch custom image segmentation data set loading
- In an SQL file, a test table and data are defined above, and you can select* from the test table below
- The new generation of public chain attacks the "Impossible Triangle"
- You study, I reward, 21 day learning challenge | waiting for you to fight
- 《nlp入门+实战:第五章:使用pytorch中的API实现线性回归》
- [introduction to cryoelectron microscopy] Caltech open class course notes part 3:image formation
- UPC little C's King Canyon
猜你喜欢

Explanation and closing method of server 135, 137, 138, 139, 445 and other ports
![[paper reading] tomoalign: a novel approach to correcting sample motion and 3D CTF in cryoet](/img/3a/75c211f21758ca2d9bb1a40d739d80.png)
[paper reading] tomoalign: a novel approach to correcting sample motion and 3D CTF in cryoet

Compare three clock circuit schemes of single chip microcomputer

Sort out the two NFT pricing paradigms and four solutions on the market

NLP introduction + practice: Chapter 5: using the API in pytorch to realize linear regression

Solving linear programming problems based on MATLAB

Joseph Ring problem

The smallest positive number that a subset of an array cannot accumulate

207. Curriculum
![[experience] relevant configuration of remote connection to intranet server through springboard machine](/img/27/25301cce8a4e5fa0c91902c7b734fb.png)
[experience] relevant configuration of remote connection to intranet server through springboard machine
随机推荐
[paper reading | cryoelectron microscope] interpretation of the new subtomogram averaging method in relion 4.0
20 hacker artifacts
Volatile keyword parsing of C #
Matrix decomposition and gradient descent
flutter只要是数据,都会判空的
Convert source package to RPM package
IonIcons图标大全
Sort out the two NFT pricing paradigms and four solutions on the market
MySQL 45 talk | 07 line lock merits and demerits: how to reduce the impact of line lock on performance?
Keyboard processing in jetpack compose
Analyze the roadmap of 25 major DFI protocols and predict the seven major trends in the future of DFI
NLP introduction + practice: Chapter 5: using the API in pytorch to realize linear regression
Phased learning about the entry-level application of SQL Server statements - necessary for job hunting (I)
[密码学实验] 0x00 安装NTL库
C language data type
[cryoelectron microscope | paper reading] emclarity: software for high-resolution cryoelectron tomography and sub fault averaging
CentOS deploy PostgreSQL 13
Space shooting Lesson 17: game over (end)
What are the principles and methods of implementing functional automation testing?
Popular cow G