Record: Writing Unity Scripts 5.0


Large-scale TV series in untiy (or other activities)

Preface

We have added background music to the scene before, but in our daily experience, we can find that there are many interesting animation effects in games or scenes. Today we will add an animation effect to our scene and modify it through scripts. Take control

Create animation

First create a folder to hold animation effects
Insert image description here

Unity Animation、Animator

Before using and writing content about animation, you need to first understand the animation components of Unity, which are Animation and Animator
In the old version of Unity, we only have the Animation component. After version 4.6, the Animator component was added. If we only control the playback of one animation, we use the Animaton component. If we need to convert many animations, we use the Animator component. The difference between them is that the Animator has an animation controller (commonly known as animation). State machine), it is very convenient to use it for animation switching, but the disadvantage is that it takes up more memory than the Animaton component

Common categories

Animator: Animation controller, an interface to control the Mecanim animation system, used to manage multiple animations;

Animation: used to play animations. A single Animation in the old version can also complete animation playback and switching, but state switching requires programmer code control. In the new version, the state management part is handed over to Animator;

AnimationClip: Animation clip clip, which stores animation based on key frames and is used by Animation to play animation;

AnimationState: Animation state, used to change the playback speed, weight, time, level, playback Mode, and mixing mode of a single animation;

AnimationEvent: animation event, used to trigger custom functions under certain conditions;

StateMachineBehaviour: Animation state machine manager extension class. After the script inherits this class, it is bound to a certain State on the Animator. When the state changes, the response function can be overloaded. Similar to trigger response function;

About the difference between the two

Compared with the old version of Animation, the new version of Animator adds animation state machine
Insert image description here
Compared with the old version of Animation, the new version of Animator adds skeletal Avatar (but it is not used now< /span>https://www.cnblogs.com/01zxs/p/9965115. htmllink
Insert image description here
For more details, you can read this article

Animator

Back to the topic, since Unity’s own animation control is very unbelievable, it is difficult to use in a word, and the old version is not commonly used anymore, so I won’t go into details here. If you want to know more, you can read this article or Video or something
linkhttp://t.csdnimg.cn/erxaa
So let’s talk about Animator< /span> and see the scene Try running it, click on the animator a> In the same way, continue to operate to achieve the effect in the picture and create transitions in the animator's actions to connect the animations Drag the blue triangle icon in the animation into the animator, here Just drag a few Open the animator Add the Animator component to control the animation Click on the object to operate (The familiar squares and balls are back) Then the line of blue text appears, which is the dragged object Drag the villain into the scene (just drag any one)
First find some animation effects, a villain does some actions
Insert image description here


Insert image description here



Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here
Insert image description here

Found that the villain performed an animation demonstration

Script

Use scripts to control animation playback

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class NewBehaviourScript1 : MonoBehaviour
{
    Animator am;
    // Start is called before the first frame update
    void Start()
    {
        am = GetComponent<Animator>();

        StartCoroutine(learnConroitine());//开启协程
        //StopCoroutine();
        //StopAllCoroutines();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            am.SetTrigger("zcc");//设置触发   //这是我的那个动画的名字
            //yield return new WaitForSeconds(5);
            //am.SetBool();//设置bool值
            // am.SetFloat();//设置浮点数
            //am.SetInteger();//设置整数值
        }
        //else
        //{
        //    yield return null;
        //}
    }

    //Unity中协程的使用,采用的是yield return
    IEnumerator learnConroitine()
    {
        yield return new WaitForSeconds(5); 
       yield return null;           //yield用于将方法切割开
    }
}

This script should make the animation play when pressing F on the keyboard

that's all.

Guess you like

Origin blog.csdn.net/m0_72471315/article/details/134254926