12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 2417|回复: 11
打印 上一主题 下一主题

增加敌人并添加AI 4.1---射击游戏系列教程之四

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

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

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

          在本章节中,我们给场景中增加敌人,并赋予其AI,在前面的几节中,角色的控制及运动已构建完成,接下来我们需要做的就是在此基础上,创建敌人prefab并增加AI部分,使其能发现并攻击玩家角色。
         

          We're ready to start coding our enemy AI. Now that our movement and animation framework is
         

          already in place, all we really need to is fill our FindAIinput function with code and add a new enemy
         

          prefab to our game.
         

          创建一个新的prefab,将其命名为:EnemyPrefab,在层级列表中,选择Player并复制,将复制的物体重命名为enemy,在属性面板中,将新物体的tag改为untagged,这是为了将其与Player分别开来。现在我们有了一个全新的角色敌人。
         

          Firstly prefabs. Hopefully you have browsed through the Unity tutorials enough to understand what a
         

          prefab is. Let's create a new prefab, right click in our project window and select (create – prefab) and
         

          name it something like EnemyPrefab.
         

          Now duplicate your player object, click it and press CTRL-D, name it to something like 'enemy'. Make
         

          sure you change the enemies 'tag' to 'untagged' in the inspector when you highlight it otherwise you'll
         

          just be creating another player. Move your enemy to another location away from your player, run your
         

          game and you have a brand new character object.
         

          此外将角色的碰撞体半径设为0.45是比较合适的,将敌人的碰撞体半径设为0.25,看来我们的角色要比敌人雄壮一些。现在我们需要创建材质更换贴图并将其赋予“enemy ”敌人物体,省事的办法是直接复制角色的材质,将此新材质重命名为"EnemyMaterial",同理,其动作也是通过播放一张贴图序列来实现的,所以我们将此材质的贴图更换为EnemySpriteSheet.png,下面设置相关的参数,首先要关闭EnemySpriteSheet.png图片的Generate Mip Maps选项,然后将贴图的XYtiling更改为-0.2和-0.167.
         

          At this point feel free to play around with your Capsule Collider component on your player and enemy,
         

          I found a radius of 0.45 to be good. Now your characters will collide with more correct collision.
         

          Now you can create a new material, by duplicating the player material and naming it EnemyMaterial.
         

          And giving it the texture EnemySpriteSheet.png. Just remember to turn off Generate Mip Maps for the
         

          texture otherwise your enemy character might look blurry. Add your EnemyMaterial to your enemies
         

          MeshRenderer Material, and give it an X tiling of -0.2 and a Y tiling of -0.167.
         

          Set the radius of your enemies Capsule Collider to 0.25.
         

          更改下面的两个变量值,在层级列表中,选中“enemy ”,并拖动到项目面板中我们前面创建的“EnemyPrefab”上,OK,敌人模板创建完成,这样你就可以使用大量的敌人而不必一个一个的去创建。
         

          Also change your public enemies animation variables as follows:
         

          animationFrameRate = 15;
         

          spriteSheetTotalHigh = 6;
         

          Now click and drag your enemy object from your Hierarchy to your EnemyPrefab in your project
         

          window. And voila you have your prefab! Now whatever you do make sure your enemy objects are
         

          always connected to your prefab, let them go and any changes you make to your prefab, global variable
         

          values etc, wont follow through consistently to each prefab and it can get very annoying if you placed
         

          lots of enemies.
         

          Any changes you make to your enemies later on, either make them to the prefab itself, or if you make
         

          the changes to one of the objects in your scene, but want the changes to be made to all your objects in
         

          your scene, drag your object again onto your prefab and the changes you made on that one object will
         

          be applied to your prefab and all objects using that prefab.
         

          Run your game and your Alien enemy should be standing there if all has worked well and good.
         

          Now to make your enemy run towards the player!
         

          接下来我们添加一段代码,使敌人朝着玩家跑去,
         

          Add the following to FindAIinput:
         

          void FindAIinput ()
         

          {
         

          inputMovement = objPlayer.transform.position - transform.position;
         

          inputRotation = inputMovement; // face the direction we are moving
         

          }
         

          It's that simple! What wonderful framework we've put in place. The direction we need to move to is
         

          the Vector from our position 'transform.position' to the player's position ' objPlayer.transform.position'.
         

          Time to implement melee attacks.
         

          Remember back in our ProcessAnimation function how we commented out a few lines involving a
         

          'meleeAttackState' variable? Uncomment that code and add the following variable to your script:
         

          //input variables (variables used to process and handle input)
         

          private Vector3 inputRotation;
         

          private Vector3 inputMovement;
         

          private bool meleeAttackState;
         

          Now make the following changes to your FindAIinput function:
         

          void FindAIinput ()
         

          {
         

          inputMovement = objPlayer.transform.position -
         

          transform.position;
         

          inputRotation = inputMovement; // face the direction we are
         

          moving, towards the player
         

          meleeAttackState = false;
         

          if
         

          ( Vector3.Distance(objPlayer.transform.position,transform.position) < 1.2f )
         

          {
         

          meleeAttackState = true;
         

          inputMovement = Vector3.zero;
         

          if (currentAnimation != animationMelee) { frameNumber
         

          = meleeAnimationMin + 1; }
         

          }
         

          }
         

          敌人物体的AI,其实就是各种条件及状态的判断,首先要设定一个感应范围,并监测敌人物体的当前帧,使其动作保持完整的循环。
         

          Firstly we are setting meleeAttackState to false so that we don't melee if we are too far away from the
         

          player. Next we are checking to see our distance from the Player, if we are within 1.2 units (the f is
         

          there because the function Vector3.Distance returns a floating point number. We then set
         

          meleeAttackState to true. We set each x,y and z value of inputMovement to 0. And if we are not
         

          playing the melee animation we set the current frame to be at the beginning of the animation. If we
         

          didn't set it to play at the beginning it might start half way through the animation. We didn't need to do
         

          it for cyclic animations because they would just loop but this animation needs to play from start to
         

          finish. We don't need the +1 added to frameNumber, I just noticed on my pc the animation plays better
         

          starting a little bit into it.
         

          Now make the following changes to your FindAnimation function:
         

          void FindAnimation ()
         

          {
         

          if (meleeAttackState == true || currentAnimation == animationMelee)
         

          {
         

          currentAnimation = animationMelee;
         

          return;
         

          }if (
         

          inputMovement.magnitude >
         

          0)
         

          {
         

          currentAnimation = animationWalk;
         

          } else {
         

          currentAnimation = animationStand;
         

          }
         

          }
         

          We check to see if meleeAttackState is true and if it is we will begin playing the melee animation, we
         

          also check to see if we have not yet finished playing the melee animation, even if meleeAttackState is
         

          set to false we will still play the melee animation if we haven't reached the end of it yet.
         

          We add the return command to stop the currentAnimation variable being set below.
         

          And now the code we uncommented before:
         

          if (currentAnimation == animationMelee)
         

          {
         

          frameNumber =
         

          Mathf.Clamp(frameNumber,meleeAnimationMin,meleeAnimationMax+1);
         

          if (frameNumber > meleeAnimationMax)
         

          {
         

          if (meleeAttackState == true)
         

          {
         

          frameNumber = meleeAnimationMin;
         

          } else {
         

          currentAnimation = animationWalk;
         

          frameNumber = walkAnimationMin;
         

          }
         

          }
         

          }
         

          此部分添加的代码并不是很多,可以直接拷贝添加我们的脚本文件中,当然,手动输入更有助于理解其作用。保存场景,此部分结束。
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

沙发
发表于 2012-1-26 23:18:44 |只看该作者
春节即将来到,我用祝福捻制成的绒线,烛光下为您织起一件红色的毛衣:前身是平安,后身是幸福,吉祥是厚厚的肩,如意戴在袖子里,领子蕴藏着体贴,口袋把快乐盛满,穿在身上让暖和包裹着您,让我的心陪伴您度过新年。
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

板凳
发表于 2012-2-13 23:24:27 |只看该作者
谢谢楼主,真是太实用了
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

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

地板
发表于 2012-2-21 23:31:57 |只看该作者
真是不错啊
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

5#
发表于 2012-3-13 23:24:36 |只看该作者
“再次路过……”我造一个-----特别路过
回复

使用道具 举报

797

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
5568
精华
0

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

6#
发表于 2012-3-16 19:43:02 |只看该作者

   
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

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

7#
发表于 2012-3-30 23:21:36 |只看该作者
我也来支持下
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

8#
发表于 2012-8-30 00:50:39 |只看该作者
无聊时可以刷屏幕 灌水 也可以试试 帖子的标题究竟可以写多长
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

9#
发表于 2012-8-30 00:55:39 |只看该作者
你们都躲开,我来顶
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

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

10#
发表于 2012-8-30 00:56:18 |只看该作者
心中有爱,爱咋咋地
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

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

GMT+8, 2025-7-28 13:45 , Processed in 0.296100 second(s), 29 queries .

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

© 2008-2019 Narkii Inc.

回顶部