转自 : http://www.unitymanual.com/thread-1906-1-1.html
动画系统非常的灵活强大,动画系统支持动画融合、混合、叠加动画,行走循环的时间同步,动画层,控制动画的各个方面,以及支持基于物理的布娃娃系统和程序动画。这里做个笔记文章,记录整个从0开始使用Unity的动画系统。下载官网的CharacterAnimation例子里面带有3个动画模型,以及这三个模型的简单调用的例题。unity3d教程手册
1
2
3
4
5
6
7
|
function Start ()
{
animation.Stop();
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
// Makes a character contains a Run and Idle animation
// fade between them when the player wants to move
// 让一个角色包含Run和Idle动画,并且在玩家想移动的时候在他们之间淡入淡出。
function Update () {
if
(Mathf.Abs(Input.GetAxis(
"Vertical"
)) > 0.1)
animation.CrossFade(
"Run"
);
else
animation.CrossFade(
"Idle"
);
}
|
1
2
3
|
animation.wrapMode = WrapMode.Loop;
animation[
"jump"
].wrapMode = WrapMode.Clamp;
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
function Start ()
{
// Set all animations to loop
animation.wrapMode = WrapMode.Loop;
// except shooting
animation[
"shoot"
].wrapMode = WrapMode.Once;
// Put idle and walk into lower layers (The default layer is always 0)
// This will do two things
// - Since shoot and idle/walk are in different layers they will not affect
// each other's playback when calling CrossFade.
// - Since shoot is in a higher layer, the animation will replace idle/walk
// animations when faded in.
animation[
"shoot"
].layer = 1;
// Stop animations that are already playing
//(In case user forgot to disable play automatically)
animation.Stop();
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
function Update () {
if
(Mathf.Abs(Input.GetAxis(
"Vertical"
)) > 0.1 || Mathf.Abs(Input.GetAxis(
"Horizontal"
)) > 0.1)
animation.CrossFade(
"run"
);
else
animation.CrossFade(
"idle"
);
// Shoot
if
(Input.GetButtonDown (
"Fire1"
))
animation.CrossFade(
"shoot"
);
}
|
测试一下控制角色,这样在上次人物动作的基础上让角色可以在场景里面跑起来。首先为角色增加一个CharacterController。然后设定一些变量控制角色的速度。
01
02
03
04
05
06
07
08
09
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
|
var speed = 3.0;
var rotatationSpeed = 200.0;
private
var curSpeed = 0.0;
function Update () {
// Rotate around y-axis
var newRotation = Input.GetAxis(
"Horizontal"
) * rotatationSpeed;
transform.Rotate(0, newRotation * Time.deltaTime, 0);
// Calculate speed
var newSpeed = Input.GetAxis(
"Vertical"
) * speed;
if
(Input.GetKey(
"left shift"
))
newSpeed *= 1.5;
// Move the controller
var controller : CharacterController = GetComponent (CharacterController);
var forward = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward * newSpeed);
}
|
1
2
3
4
5
6
7
|
animation.AddClip(animation[“shoot”].clip, “shootUpperBody”);
animation[“shootUpperBody”].AddMixingTransform(transform.Find(“mover/roothandle/spine1”));
animation[“shootUpperBody”].AddMixingTransform(transform.Find(“mover/gun”));
|
1
|
animation.CrossFadeQueued(
"shootUpperBody"
, 0.3, QueueMode.PlayNow);
|
01
02
03
04
05
06
07
08
09
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
49
50
|
function Update () {
if
(Mathf.Abs(Input.GetAxis(
"Vertical"
)) > 0.1 || Mathf.Abs(Input.GetAxis(
"Horizontal"
)) > 0.1)
{
animation.CrossFade(
"run"
);
animation[
"run"
].speed = Mathf.Sign(Input.GetAxis(
"Vertical"
));
}
else
animation.CrossFade(
"idle"
);
// Play the cross fade animation
if
(Input.GetButtonDown (
"Jump"
))
{
animation.CrossFade(
"jump"
, 0.3);
}
// Shoot
if
(Input.GetButtonDown (
"Fire1"
))
{
// We are running so play it only on the upper body
if
(animation[
"run"
].weight > 0.5)
animation.CrossFadeQueued(
"shootUpperBody"
, 0.3, QueueMode.PlayNow);
// We are in idle so play it on the fully body
else
animation.CrossFadeQueued(
"shoot"
, 0.3, QueueMode.PlayNow);
}
}
|