查看: 2034|回复: 7
打印 上一主题 下一主题

[提问] 角色的动作3.3---射击游戏系列教程之三

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2011-11-21 15:20:00 |只看该作者 |倒序浏览


           Go ahead and***n your game, is your character animating? If not have a look at my version of this
           

           project and see if there's something off in your code or unity editor. Maybe I've made a mistake here
           

           (goodness I hope not). If so let me know. But let's go through each section of code and I will explain
           

           what it is doing.
           

           void HandleAnimation () // handles all animation
           

           {
           

           FindAnimation();
           

           ProcessAnimation();
           

           }
           

           HandleAnimation is simply calling the FindAnimation then the ProcessAnimation function.
           

           void FindAnimation ()
           

           {
           

           if (inputMovement.magnitude > 0)
           

           {
           

           currentAnimation = animationWalk;
           

           } else {
           

           currentAnimation = animationStand;
           

           }
           

           }
           

           We are using an IF statement to see whether the inputMovement Vector's magnitude is greater than 0.
           

           Magnitude is the size of the Vector, if it is 0 the player is not inputting any movement, if the player is
         

           pressing a key because the Vector is being set to that, the magnitude of the Vector will also increase.
           

           And as such depending on what the player is doing we want to play the correct animation.
           

           I have added as many comments which have come to mind to the ProcessAnimation function, I shall
           

           try to elaborate a little more below:
           

           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;
           

           The first calculation on the variable 'animationTime' is subtracting the time since the last frame (or last
           

           time this function was called from this variable. If animationTime is less than 0, the frame number
           

           increments by a value of 1, meaning the next frame will be played.
           

           if (currentAnimation == animationStand)
           

           {
           

           frameNumber =
           

           Mathf.Clamp(frameNumber,standAnimationMin,standAnimationMax+1);
           

           if (frameNumber > standAnimationMax)
           

           {
           

           frameNumber = standAnimationMin;
           

           }
           

           }
           

           If we are playing the stand animation (ie the player is not giving any input on the keyboard) we are
           

           going to clamp the frameNumber value between 2 other values, the Clamp ins***ction does just that.
           

           For example, if frameNumber was equal to 3, and I went to clamp it between 5 and 10, the number
           

           would jump up to the nearest number within that range, in this example frameNumber would become 5.
           

           I do this because I don't want frameNumber to be outside of the frame number that it should be.
           

           Otherwise you might want to play a stand animation and it is half way through the walk animation. If
           

           it happens to be half way through the walk animation I want to clamp it back into the stand animation
           

           range. Next I check to see if the frameNumber has exceeded the maximum number of frames for the
           

           stand animation, if it has I set it back to the beginning and it will cycle once again.
           

           This effectively sets the variable frameNumber, to the frame number that should be displayed on the
           

           character.
           

           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;
         

           }s
           

           priteSheetCount.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
           

           This code I wont explain the detailed mathematics of. It simply finds out what frame number it is at,
           

           and offsets the texture accordingly. If the frame is number 5 then it wants to use the frame in the top
           

           right corner of our character image. If the frame is number 6, it wants to use the first frame on the
           

           second row etc. This code makes sure it aligns the texture on the character to make sure it accurately
           

           plays the correct frame.
           

           *** but what about the fact that our character doesn't rotate from the center of his body?
           

           Let's try a quick fix add a new variable:
           

           private Vector2 spriteSheetCount; // the X, Y position of the frame
           

           private Vector2 spriteSheetOffset; // the offset value of the X, Y coordinate for the texture
           

           public Vector2 spriteSheetOriginOffset;
           

           This is a variable you can set from your editor in Unity, that way if the offset is different for different
           

           sprite sheets you can adjust it accordingly. Great thing about Unity is that I can change these values
           

           while the game is***nning and see the changes right in front of me! Using trial and error I set an X
           

           value to -0.003 and a Y value to 0.045.
           

           Now let's add it to our code:
           

           spriteSheetOffset = new Vector2(1 - (spriteSheetCount.x/spriteSheetTotalRow),1 -
           

           (spriteSheetCount.y/spriteSheetTotalHigh)); // find the X and Y coordinate of the
           

           frame to display
           

           spriteSheetOffset += spriteSheetOriginOffset;
           

           renderer.material.SetTextureOffset ("
           
            _
           
           MainTex", spriteSheetOffset); // offset the
           

           texture to display the correct frame
           

           Here I am just adding the offset we specified to how much we are offsetting the texture.
           

           There is a small problem with this fix and that is a tiny shadow artifact which appears on the edge of
           

           the sprite. As another frame leaks onto the image. The solution I offered above is a quick one, if you
           

           wanted to try a more complicated option you can remove the mesh renderer and mesh filter component
           

           from your game object, and create a child game object to your player which only contains the mesh
           

           filter and mesh renderer, add your sprite to it. Then create a public GameObject variable in your player
           

           script, drag the child object onto the variable in the inspector which is on the parent object. And in
           

           your script change the following.
           

           yourGameObjectVariable.renderer.material.SetTextureOffset ("
           
            _
           
           MainTex", spriteSheetOffset);
           

           This is how I am doing it from now on, did the last method just to show you a potential work around.
           

           Feel free to see how I've set it up in the example project.
           

           Don't Forget to save your scene Goto > File > Save Scene.
           

           You're ready for Enemy AI!
         

           保存场景,下一章节将来实现敌人的AI功能。附件为本章节所用到的完整代码文件!出处:
           
            www.unity3d8.com
           

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-3-18 23:30:55 |只看该作者
其实楼主所说的这些,俺支很少用!
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

板凳
发表于 2012-5-25 23:24:20 |只看该作者
无聊时可以刷屏幕 灌水 也可以试试 帖子的标题究竟可以写多长
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-6-12 23:23:08 |只看该作者
加精、加亮滴铁子,尤其要多丁页丁页
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-8-11 00:01:10 |只看该作者
呵呵,真得不错哦!!
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-10-24 23:20:13 |只看该作者
我就看看,我不说话
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2013-2-15 23:31:01 |只看该作者
不错哦,顶一下......
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2013-2-28 23:31:36 |只看该作者
我看看就走,你们聊!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-29 21:58 , Processed in 0.156135 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部