- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
 
- 纳金币
- -1
- 精华
- 11
|
下面我们就要声明新增加的HandleAnimation () 函数了。看下面代码可以发现,此函数只是起到一个调用,真正起作用的是其所调用的FindAnimation(),ProcessAnimation() 这两个函数,搞清楚这个函数和用途,也就明白此章节所讲的了。FindAnimation()这个函数其实是检测角色是处于什么状态,是在移动呢还是停 止,紧接着ProcessAnimation()这个函数根据角色当前的状态是行走还是停止来处理图片序列的播放等。
void HandleAnimation () // handles all animation
{
FindAnimation();
ProcessAnimation();
}
void FindAnimation ()
{
if (inputMovement.magnitude > 0)
{
currentAnimation = animationWalk;
} else {
currentAnimation = animationStand;
}
}
void ProcessAnimation ()
{
animationTime -= Time.deltaTime; // animationTime -= Time.deltaTime; subtract
the number of seconds passed since the last frame, if the game is***nning at 30 frames per second the
variable will subtract by 0.033 of a second (1/30)
if (animationTime <= 0)
{
frameNumber += 1;
// one play animations (play from start to finish)
if (currentAnimation == animationMelee)
{
frameNumber =
Mathf.Clamp(frameNumber,meleeAnimationMin,meleeAnimationMax+1);
if (frameNumber > meleeAnimationMax)
{
/* if (meleeAttackState
== ***e) // this has been commented out until we add enemies that will attack with their evil alien
claws
{
frameNumber =
meleeAnimationMin;
} else {
currentFrame =
frameStand;
frameNumber =
standAnimationMin;
}*/
}
}
// cyclic animations (cycle through the animation)
if (currentAnimation == animationStand)
{
frameNumber =
Mathf.Clamp(frameNumber,standAnimationMin,standAnimationMax+1);
if (frameNumber > standAnimationMax)
{
frameNumber = standAnimationMin;
}
}
if (currentAnimation == animationWalk)
{
frameNumber =
Mathf.Clamp(frameNumber,walkAnimationMin,walkAnimationMax+1);
if (frameNumber > walkAnimationMax)
{
frameNumber = walkAnimationMin;
}
}
animationTime += (1/animationFrameRate); // if the
animationFrameRate is 11, 1/11 is one eleventh of a second, that is the time we are waiting before we
play the next frame.
}
spriteSheetCount.y = 0;
for (i=(int)frameNumber; i > 5; i-=5) // find the number of frames down the
animation is and set the y coordinate accordingly
{
spriteSheetCount.y += 1;
}
spriteSheetCount.x = i - 1; // find the X coordinate of the frame to play
spriteSheetOffset = new Vector2(1 - (spriteSheetCount.x/spriteSheetTotalRow),1 -
(spriteSheetCount.y/spriteSheetTotalHigh)); // find the X and Y coordinate of the frame to display
renderer.material.SetTextureOffset ("
_
MainTex", spriteSheetOffset); // offset
the texture to display the correct frame
}
到目前为止,如果一切顺利的话,切换到游戏模式,角色已可以正常的行走了,接下来的部分是对上面新增加的代码的一个分段讲解....当然,你想省事的话,直接将本节教程最后的附件中的AIscript.cs文件下载,直接复制代码即可!
出处:
www.unity3d8.com
|
|