- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
- 纳金币
- -1
- 精华
- 11
|
前面两节教程中,我们实现了对角色的移动控制及摄像机跟随,不过我们发现角色并没有行走的动作,接下来我们来实现角色的各种动作,当然,完全是通过贴图实现的。
Got your mind wrapped around the last part? I know it can be really tricky and confusing sometimes
but I hope you're doing well!
This next part isn't too important to understand, I am animating a sprite sheet, if you're a Unity 3D user
you're more than likely going to be animating with a 3D model which is a lot more simple than
animating a sprite sheet and requires less code to accomplish. However I shall be explaining how to
animate a sprite sheet in this tutorial. Don't worry if you don't understand the ProcessAnimation()
function, see if you can understand the rest though as that will be very useful.First add the following code. And below I shall explain some of new variables we'll be using
让一个平面角色动起来有一定的难度,在unity3d中,相比让一个真正的角色模型动起来,后者要容易的多。下面逐步对实现过程进行讲解,其中要用到一个关键的函数 ProcessAnimation(),首先我们来看下面的代码,声明一些新的变量。uke原创,转载请注明出处:
www.unity3d8.com
// calculation variables (variables used for calculation)
private Vector3 tempVector;
private Vector3 tempVector2;
private int i;
// animation variables (variables used for processing aniamtion)
public float animationFrameRate = 11f; // how many frames to play per second
public float walkAnimationMin = 1; // the first frame of the walk animation
public float walkAnimationMax = 10; // the last frame of the walk animation
public float standAnimationMin = 11; // the first frame of the stand animation
public float standAnimationMax = 20; // the last frame of the stand animation
public float meleeAnimationMin = 22; // the first frame of the melee animation
public float meleeAnimationMax = 30; // the last frame of the melee animation
public float spriteSheetTotalRow = 5; // the total number of columns of the sprite sheet
public float spriteSheetTotalHigh = 4; // the total number of rows of the sprite sheet
private float frameNumber = 1; // the current frame being played,
private float animationStand = 0; // the ID of the stand animation
private float animationWalk = 1; // the ID of the walk animation
private float animationMelee = 2; // the ID of the melee animation
private float currentAnimation = 1; // the ID of the current animation being played
private float animationTime = 0f; // time to pass before playing next animation
private Vector2 spriteSheetCount; // the X, Y position of the frame
private Vector2 spriteSheetOffset; // the offset value of the X, Y coordinate for the texture
There are many variables here, I have added a comment to explain the use of each one. They are there
to create a lot of flexibility with the sprite sheet animation system I am using.
以上众多变量主要是来控制角色动画的状态,其实就是控制序列帧的播放,包括速率、起始帧,结束帧等等。
Notice I have defined an int variable 'i'. And int variable is an integer and is restricted to having no
decimal numbers. In this case I don't need decimals as I am either on frame number 1 or 2 etc. I have
made definitions such as 'animationWalk = 1;' The only purpose of this is for better code readability,
instead of typing:
if (currentAnimation == 1)
I can type:
if (currentAnimation == animationWalk)
So instead of having to guess what the number '1' means I know it means the walk animation now. It's
good practice to do that for code so you can understand what is going on when you look at a piece of
code you haven't seen in weeks.
以上部分主要对上面变量的一些具体说明。略过
接下来要在update()函数中添加一个重要的新函数HandleAnimation(),此函数即是负责角色序列帧的控制的。
Add the HandleAnimation function to your update function:
void Update () {
FindInput();
ProcessMovement();
HandleAnimation();
if (thisIsPlayer == true)
{
HandleCamera();
}
}
After your HandleCamera function add the following code:
|
|