# unity-2d 动画
# 1. 切割原画
- 通过组件 editor 打开编辑界面
- 通过切割将整张图切割成我们需要的大小,并导入到 assets 文件夹
# 2. 创建动画
创建动画文件
挑选适合的图片
将挑选好的图片拖入动画中
并设置适合的帧率,至此动画初步完成不同的行为控制不同的动画
例如这里 通过双向行为控制角色是否在移动来添加不同的动画
同时在对象身上添加判定条件
此时 再进入程序内row 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49public class player : MonoBehaviour
{
private Rigidbody2D rb;
[private Animator animator; ]
[private float moveSpeed; ]
[private float jumpforce; ]
private float xinput;
// Start is called before the first frame update|
void Start()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponentInChildren<Animator>();
}
// Update is called once per frame
void Update()
{
Movement();
CheckInput();
AnimatorController();
}
private void CheckInput()
{
xinput = Input.GetAxisRaw("Horizontal");
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
Debug.Log("jump");
}
}
private void Movement()
{
rb.velocity = new Vector2(xinput * moveSpeed, rb.velocity.y);
}
private void Jump()
{
rb.velocity = new Vector2(rb.velocity.y, jumpforce);
}
private void AnimatorController()
{
bool moving = rb.velocity.x != 0;
animator.SetBool("moving", moving);
}
}
通过[SerializeField] private Animator animator;
创建对象animator = GetComponentInChildren<Animator>();
获取到子类中的动画moving = rb.velocity.x != 0;animator.SetBool("moving", moving);
并通过判定向量来控制 moving 的变化