Unity beginner 4 - frame animation and protagonist attack (2d)

This article comes from the notes of learning chutianbo teacher, link to station b

About the production of frame animation

Regarding the animation production of a character, we need to add a component Animator (animation controller) to this character first. insert image description here
Then we use ctrl+6 to open the Animation panel, select our character under Prefabs, click Create Animation Cilp, and create a new file The animation is placed in the Animation folder we created under assets.
Afterwards, we found materials of different shapes in the Characters folder
insert image description here, as shown in the picture above. The robot action is divided into three states: idle, fixed, and walk, and the animation in walk is divided into four directions. We use Shift to hold down and select the four photos belonging to down and drag them into the Animation panel for frame making.

Questions:
1. The action is only left but not right.
insert image description here
Use Filp X to invert it.
2. The three symbols on the right of the name indicate to transfer to the specified frame, add a key frame, and add an event.
3. The frame animation event produced should not be lower than 0.10. Otherwise, there will be a bug in the tiger judgment of the mixed tree later

Animate characters using blend trees

After making the animation, we need to add a behavior flow to the character.
When making animation, it will automatically create a corresponding animator and click to enter. You
will find that every animation we made before is scattered in it, so according to the classification, we use the right button to create create state➡New Blend tree.
Because the animation of the robot has an orientation problem, we add parameters to the parameters on the left to determine that Move X and Move Y are both float types. It will look like
insert image description here
this after finishing .
Remember to
insert image description here
change the mixed tree type to two control parameters, and then You can add your own animation.
Here, I did not control the parameters of the animation above. The animation of the protagonist below
insert image description here
is four states: idle (standing), launch (shooting), moving (running), hit (hit)
Speed> At 0.1, idle➡Moving
Look X and Look Y are used to represent the orientation (there will be codes below to calculate the current orientation)
Lanuch and Hit are trigger types. When the condition is triggered, it will automatically jump back to the previous state;

//我的角色初始面向向下
 Vector2 lookDirection = new Vector2(0, -1);
    Vector2 move;
    void Update(){
 move = new Vector2(horizontal, vertical);
        //浮点数近似相等
        if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
        {
            lookDirection.Set(move.x, move.y);
            //使数值归一为向量
            lookDirection.Normalize();
        }
        animator.SetFloat("Look X", lookDirection.x);
        animator.SetFloat("Look Y", lookDirection.y);
        animator.SetFloat("Speed", move.magnitude);
        }

After the animation is done, use parameters to control the animation

Declare animation object
Animator animator;
2. Get animation instance in start
animator = GetComponent();
3. Add judgment in FixedUpdate

  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);
        ```
# 关于主角攻击
动画制作同上不再赘述,这里我们讲述一下关于主角射击发出子弹的事情
1.在子弹预制件中加入刚体和碰撞体,同时为子弹projecttile预制件挂上脚本
 ``
   Rigidbody2D rigidbodyproject;
    // Start is called before the first frame update
    void start()
    {
        rigidbodyproject = GetComponent<Rigidbody2D>();
    }
    public void launch(Vector2 direction,float force){
    //给子弹一个发射的力
        rigidbodyproject.AddForce(direction*force);
        }
   private void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log($"碰到了{collision.gameObject}");
        //子弹撞见实体时消失
        Destroy(gameObject);
        //当子弹遇见机器人时,修复机器人
        EnemyController2 emenyController2 = collision.collider.GetComponent<EnemyController2>();
        if (emenyController2 != null)
        {
            emenyController2.Fix();
        }
    }
    private void Update()
    {
        //如果没有碰到任何碰撞体,过远自动销毁
        if (transform.position.magnitude>100)
        {
            Destroy(gameObject);
        }
    }

2. Add public GameObject projecttilePrefabs to the protagonist code
;//Indicates to obtain bullet prefabs
3. Add launch function for the protagonist

   void Launch()
    {
        //在指定位置创建对象
        GameObject projecttileObject = Instantiate(projecttilePrefabs, rigidbody2dRuby.position + Vector2.up * 0.5f, Quaternion.identity);
        projecttile projecttile = projecttileObject.GetComponent<projecttile>();
        projecttile.launch(lookDirection, 300);
    }

The above-mentioned Instantiate means that creating an entity is an api parameter of unity. The first is the object, the second is the creation position, and the third is the rotation.
Quaternion.identity means no rotation (here we can go back to the initial creation of the protagonist’s movement, if Uncheck the frozen z-axis)
4. When encountering bug 233
, we will find the first thing. Unity will report an error indicating that the rigid body acquisition of the bullet prefab is null. This is because the following is the official explanation of Unity:
This is because in Unity will not run Start when you create an object, but will start running in the next frame. Therefore, when Launch is called on the missile, it is only instantiated (Instantiate), and Start is not called, so Rigidbody2d is still empty. To resolve this issue, rename the void Start() function in the Projectile script to void Awake().
In contrast to Start, Awake is called immediately when the object is created (when Instantiate is called), so Rigidbody2d is properly initialized before calling Launch.
For more details on this issue, you can take a look at the life cycle of MonoBehavior
5. After fixing this bug, we will continue to encounter bugs.
We found that the missile was created, but the protagonist disappeared in an instant, so we need to use layers here. The protagonist and the prefab missile are placed on different layers, and at the same time uncheck the two layers in Edit➡Project Setting➡physical 2D (that is, no collision)

The material used in this article comes from the unity store Ruby's adventure
link unity official website

Guess you like

Origin blog.csdn.net/jijikok/article/details/125949879