Unity 动画系统初探(1)

     Unity的动画系统非常的灵活强大,动画系统支持动画融合、混合、叠加动画,行走循环的时间同步,动画层,控制动画的各个方面,以及支持基于物理的布娃娃系统和程序动画。这里做个笔记文章,记录整个从0开始使用unity的动画系统。(感谢Unity圣殿里面的相关翻译)

 

下载官网的CharacterAnimation例子里面带有3个动画模型,以及这三个模型的简单调用的例题。

这里例题中包含三个基本的数据模型,Goober是一个简单的小丑,Robot则是一个机器人战士,Soldier是一个人类战士。我的目标是最后灵活控制整个3.2版本里面自带的美国大兵的控制,使他可以在任意地形上产生真实的“脚踏实地”的行为动画效果。

在自带的动画模型里,除了模型Robot以外,还包含了很多@XXX结尾的动作,我们调用的时候,只需要按照@后面的动作名称调用即可。

1)新建一个场景,创景地形,并且将Robot模型拖到场景中。

      这个时候的场景中是没有任何代码的,直接运行场景,你就能看到这个模型在运行“idle”动作。

     看来Unity不但自己加载了模型,而且识别出了动作,自动调用了待机动作。为了防止unity自己的动作影响到我们的代码操作,在开始的时候我们就把默认动作关闭。创建一个JS脚本,加入如下代码:

function Start () 
{ 
  animation.Stop();
}

To play a simple animation use Animation.Play
To cross-fade between animations use Animation.CrossFade

在动画播放方式上,Unity有两种方法,一种是Play(),这种方式的动画没有任何混合,直接播放动作,这样的动作看起来比较生硬,而且在Unit用的官方文档里建议个别动画可以使用Play()方式播放,那似乎是不建议使用Play来播放动画;另外一种是Animation.CrossFade。这是一种再带混合的动画播放模式,他可以让动画逐渐变化,而不是生硬的切换。

This is where animation blending comes in. In Unity you can have an arbitrary amount of animations playing on the same character. All animations are blended or added together to generate the final animation.

这就是动画融合的用武之地。在Unity同一个角色可以拥有任意数量的动画。所有动画被融合或添加在一起,来产生最终动画。 


// 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");
}

将上面这段代码拷贝到JS脚本中,我们按方向键就可以看到刚刚的Robot在"Run"和"Idle"两个动画柔和切换的动作变化。

In today's games, animation blending is an essential feature to ensure that characters have smooth animations. Animators create separate animations, e.g. a walk cycle, run cycle, idle animation or shoot animation. At any point in time in your game you need to be able to transition from the idle animation into the walk cycle and vice versa. Of course you don't want any sudden jumps in the motion, you want the animation to smoothly transition.

如今的游戏,Animation Blending是确保角色平滑动画的一项重要功能。动画师创建的分段动画,如:行走循环、跑步循环、 空闲动画或射击动画。在游戏的任何时间点都有可能从空闲转换到走动,反之亦然。当然你不希望两个不同的动作之间突然跳转,想要动画平滑过渡。


To change how animations wrap (Loop, Once, PingPong) change the  WrapMode of the respective AnimationClips in their import settings, or  use AnimationState.wrapMode to change it at runtime.
AnimationState can be used to change the layer of an animation, modify playback speed, and for direct control over blending and mixing. 

通过animation还可以设定动画的运行方式,可以animation.WrapMode方式来修改是否循环播放。

animation.wrapMode = WrapMode.Loop;
animation["jump"].wrapMode = WrapMode.Clamp;

 

动画层可以控制玩家的动画播放优先级,具体的用法似乎还挺丰富的,从文档上看,最起码的用途是可以在其他的动画还在播放的时候,优先播放而不影响底层级的动画。比如当层级都是1,在跑动的时候,点击射击按钮,则没有反应会一直奔跑。如果设定射击的动作层级为1,其他都为0的时候,跑动的时候点射击,角色就会执行射击动作。

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();
}

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"); 
}

今天就到这里吧,动画的混合好像很难呢~~明天搞搞~
 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值