There are two types of animation components: old and new
Legacy Component: Animation
We can simply make an animation by ourselves
Animation is to change the value of the object. We can add properties to declare changes.
After adding, we are given two frames, what the first frame looks like, and what the second frame changes to, so that the animation is generated (the time length can be changed by the scroll wheel to increase the frame time)
Use scripts to play the animations in the animation list
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
//模仿动画片段
//GetComponent<Animation>().Play();
//从动画列表里找寻属于改名字的动画片段
GetComponent<Animation>().Play("right");
}
}
}
New component: Animator
New version of the animation component, no animation clip, but a controller
How to add a controller
The animation created in the new version is roughly the same as above, but it should be noted that when creating a new version of the animation, the object hanging on the new animation component should be selected, so that the created animation will be inconsistent with the original animation
new animation
old animation
We created two animations, but in only one controller, where are the animations found
Click on the controller, a bullet box will pop up, and the animation is in it
This box contains our animation, we generally call this box "animation state". (There are multiple animation states in a controller, each animation state contains an animation file and the settings of the animation file)
Screenplay:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimatorTest : MonoBehaviour
{
private Animator animator;
// Start is called before the first frame update
void Start()
{
//获得动画器组件
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
//按下鼠标左键动画切换成newRight
animator.Play("newRight");
}
}
}
Generally speaking, we will not switch animations in this way. This is just a demonstration. Generally, we use transitions to switch animations.