Unity Core 6 - Animation

1. Animation window

 ​ Open the Animation window through Window  --> Animation Animation -->

The Animation window is mainly used to  create and modify animations inside Unity  . All objects in the scene can be animated through the Animation window.

​ Principle:

​ When making an animation: record the variable changes of the script mounted on the object at a fixed point in time

​ When playing animation: Change the data recorded during animation production at a fixed time point to produce animation effects

​ Animation timeline:

​ Each animation file has its own life cycle, from the beginning to the end of the animation

​ We can edit each animation life cycle change on the animation timeline

Frames in the animation:

​ Assuming that the frame rate of an animation is 60 frames per second, it means that the animation will have up to 60 chances of change in 1 second

​ The interval between each frame is 1s / 60 ≈ 16.67 milliseconds

​ That is to say, we can change the state of the object every 16.67 milliseconds at the fastest

​ Key frame: the state of the animation at a certain time node on the timeline

  1. Preview Mode (Switch)
  2. Recording mode (switch), preview mode is enabled by default after opening
  3. Back to the beginning of the animation
  4. Previous keyframe (shortcut key: ",")
  5. Play animation (shortcut key: "Alt" + ",")
  6. Next keyframe (shortcut key: ".")
  7. Return to the end of the animation (shortcut key: "Alt" + ".")
  8. What frame is currently in
  9. filter by selection
  10. add keyframe
  11. add event
  12. keyframe mode
  13. curve mode

2. Create and edit animation

(1) Create animation

  1. Select the object you want to animate in the scene

  2. In the Animation window click Create

  3. Select the location where the animation files will be saved

When saving an animation file, Unity will help us do the following

  1. Create an  Animator  Controller (animation controller or animation state machine) resource (new animation system)

  2. Add the newly created animation file to the Animator Controller

  3. Add Animator component for animation object

  4. Animator Controller file created for Animator component association

(2) Edit animation

​ After creating the Animation file, click the corresponding game object to edit the animation interface. Switch the Animation file that needs to be edited here

​ Here you can add change attributes, or you can add any script variables you want to change in the animation

​ You can also directly select an object to modify its properties in recording mode, and Unity will automatically record such changes

(3) Curve setting

​ Click Curves to enter the curve settings. Right-click a sequence frame, the following parameters will appear:

  1. Delete Keys: Delete keyframes
  2. Edit Keys: Edit keyframes
  3. Clamped Auto: tangent mode, automatically set tangent
  4. Auto: The old tangent mode, similar to Clamped Auto, is mainly used for old animation systems, and it is recommended not to use it, unless it is an old animation
  5. Free Smooth: Freely set the tangent
  6. Flat: Tangents are set to be horizontal
  7. Broken: Separately control the left and right curve tangents
  8. Left / Right / Both Tangent: Tangent mode
    • Free: set freely
    • Linear: straight line
    • Constant: constant switching
    • Weighted: weight switching

(4) Animation file interface parameters

​ Click the Animation file and see the following parameters in the Inspector panel:

  1. Loop Time: Whether to play in a loop
  2. Loop Pose: Whether the starting point and the ending point are seamlessly connected
  3. Cycle Offset: The cycle offset when the cycle animation starts at other times

3. The old animation system

(1) Old animation system

​ There are two animation systems in Unity

  • New: Mecanim animation system - mainly use the Animator component to control animation
  • Old: Animation animation system - mainly use the Animation component to control animation (versions before Unity4 may be used)

​ At present, the animations we create for objects in the Animation window will be dominated by the new animation system

​ The old animation system will only be used if there are special needs or for some simple animations

​ The old animation system is mainly used to deal with old version projects and some simple self-made animations, new projects are not recommended for everyone to use

​ Key component: Animation

​ Note: Add the Animation component to the object before creating the animation and then make the animation. The animation produced at this time is different from the previous animation format. Unity will not automatically generate the animation state machine Animator when creating the animation.

​ At the same time, the parameters displayed in the created animation file are also different from the previous ones

  • Default: read settings get higher default repeat mode
  • Once: Stop playing once, and the playback ends
  • Loop: Loop playback from beginning to end
  • PingPong: Play from beginning to end, and then from end to beginning, in a continuous loop
  • ClampForever: After the playback ends, it will stay at the last frame, and will always play the last frame, the state will not stop, and the animation will always be played. The performance is the same as Once

(2) Parameter introduction

  1. Animation: The animation played by default
  2. Animations: All animations that this animation component can control
  3. Play Automatically: Whether to automatically play the default animation at the beginning
  4. Animate Physics: Whether the animation interacts with physics
  5. Culling Type: Decide when not to play the animation
    • Always Animate: always play
    • Based On Renderers: Culling based on the default animation pose

(3) Code control old animation system

private Animation animation;

void Update()
{
    // 1.播放动画
    animation.Play("1");
    animation.Play("2");

    // 2.淡入播放,自动产生过渡效果
    //   当你要播放的动画的开始状态 和当前的状态 不一样时 
    //   就会产生过渡效果
    animation.CrossFade("3");

    // 3.前一个播完再播放下一个
    animation.PlayQueued("2");
    animation.CrossFadeQueued("2");

    // 4.停止播放所有动画
    animation.Stop();

    // 5.是否在播放某个动画
    if (animation.IsPlaying("1")) { }

    // 6.播放模式设置
    animation.wrapMode = WrapMode.Loop;

    // 7.其它(了解即可,新动画系统中会详细讲解)
    //   层级和权重以及混合(老动画系统需要通过代码来达到动画的遮罩、融合等效果)
    // 设置层级
    animation["1"].layer = 1;
    // 设置权重
    animation["1"].weight = 1; 
    // 混合模式 叠加还是混合
    animation["1"].blendMode = AnimationBlendMode.Additive;
    // 设置混组相关骨骼信息
    animation[""].AddMixingTransform();
}

(4) Animation events

​ Animation events are mainly used for processing. When the animation plays to a certain moment, you want to trigger some logic, such as damage detection, firing bullets, special effects playback, etc.

​ Select Function in the Inspector panel to add to the event. The Function here needs to be mounted on the corresponding object by writing a method in the script

4. Animator window

(1) Finite-state machine (FSM)

Also known as finite state automaton, referred to as state machine, it is a mathematical model that represents a finite number of states and behaviors such as transitions and actions between these states

​Limited: Indicates that it is limited, not infinite

​ State: Refers to all states owned

​ For example:

​ Suppose we humans can do many actions, that is, there are many states, including standing, walking, running, attacking, defending, sleeping, etc.

We switch between these states every day, and these states are many but limited. When certain conditions are met, we will switch between these states, and this switching may happen at any time

​ Many functional systems in game development are finite state machines, the most typical state machine system

​ Action system - switch an action when a certain condition is met, and the action is limited

​ AI (artificial intelligence) system - switch a state when a certain condition is met, and the state is limited

So the state machine is an essential concept in game development

(2) Animation state machine window

​ Two ways to create an animation state machine:

  • Created automatically when creating an animation for an object in the scene, the name is the same as the object name
  • Manually create animation state machine files

  1. Layers: animation layer tab, add more layers to the animation, high-level animation playback will cover low-level animations

  2. Parameters: Parameters tab, add parameters to control state switching for the animation state machine

  3. Grid layout area:

    Mainly used for switching relationship before editing state

    • Each rectangle in the window represents a state
    • Each arrow in the window represents a switching condition

    Default three rectangles:

    • Green Entry rectangle: enter the state machine process
    • Red Exit rectangle: Exit the state machine process
    • Any State: Any state, representing any state in the state machine

    Artificially adding rectangles:

    • Orange rectangle: the default state animation at the beginning, connected with Entry to indicate the animation played at the beginning
    • Gray rectangle: a certain action state added by oneself

​ Add animation:

  • Auto-Add - Animations are automatically added to the state machine after animating an object
  • Manually add 1 - drag the animation file into the state machine (note: there will be warnings when dragging old animations)
  • Manually add 2 - right-click to create a state, and then associate animation

​ Add a switch connection:

​ Select a state, right-click to select Make Transition to add a connection, and then click the state you want to connect

​ Add switching conditions:

​ Click the parameter tab on the left panel, you can add 4 types of switching conditions here

​ Set switching conditions:

​ Click the connection to add a switching condition in the Conditions in the Inspector panel. When multiple conditions exist, the operation will be judged:

5. Animator

(1) Parameter introduction

  1. Controller: corresponding animation controller (state machine)
  2. Avatar: corresponding avatar configuration information (3D model related)
  3. Aplly Root Motion: Whether to enable animation displacement update
  4. Update Mode: update mode, generally not modified
    • Normal: Normal update
    • Animate Physics: Physics update
    • Unscaled Time: Not affected by time scaling
  5. Culling Mode: Cropping and culling mode
    • Always Animate: always play
    • Cull Update Transforms: When the camera does not render the object, stop the writing of position and IK
    • Cull Completely: When the camera is not rendering objects, the entire animation is completely disabled

(2) Code control

​ We use code to control state machine switching mainly using the API provided to us by Animator

​ We know that there are four switching conditions: int, float, bool, trigger, so the corresponding API is also related to these four types

private Animator animator;

// 1.通过状态机条件切换动画
animator.SetFloat("条件名", 1.2f);
animator.SetInteger("条件名", 5);
animator.SetBool("条件名", true);
animator.SetTrigger("条件名");

animator.GetFloat("条件名");
animator.GetInteger("条件名");
animator.GetBool("条件名");

// 2.直接切换动画 除非特殊情况 不然一般不使用
animator.Play("状态名");

Guess you like

Origin blog.csdn.net/weixin_53163894/article/details/131307001