游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3152|回复: 0

??Unity????AI

[复制链接]

1万

主题

1万

帖子

3万

积分

论坛元老

Rank: 8Rank: 8

积分
36572
发表于 2019-10-15 16:37:39 | 显示全部楼层 |阅读模式
??????????Synnaxium Studio????AI????????????????????????Radiant Blade???????????????

????Radiant Blade???????

????AI???

?????????????????AI?

??????????????????????AI??AI???????????????AI?????????????????????????????AI????????

???????????????????????????AI????????????AI??????????????????????

????????????????????????????????????????????????AI???????AI????????????????????????

????

???????????????????????????????????????????????????????????????????????

??????????????????????????????????????????????????????????????????AI????????????

???????????????AI???????????????????????????????????????????????AI????????????????

????????????????AI??????????????????????????????????????????AI?

????????????

???

?????????????????

1.jpg
??????

????????????????????????????????????????????????????????????????????????AI??????????

??????????????????????????????????????????????????????????AI?????????

??????AI???

2.jpg

???????AI???????????

??AI?????10%??????????

AI???????

????AI?????AI??????????????

???????AI??????????????????AI?????????????AI???????

????Unity

???Unity??????????????

  • ?????
  • ??Animator???
  • ?Asset Store???????????


???????????????????????????????????????????????????????????????????????????

????????Unity???Animator??????????????????????Animator????????????????????????????????????

Animator??????????????Radiant Blade????Unity Animator??????AI?

3.jpg

???????Asset Store?????????????????Animator????????


Animator

??????Animator?Unity???????????????????????

??

???Animator?????????????????????????????????

??????????????????????????Behaviours???????AI????????????????????????????????

?Shoot??????????????Shoot Behaviour???

4.png

?????????????????Shoot Behaviour???Animator?Animator????????????????????????????

??

???AI???????????????????????????

Animator?????????????????????????????????????????AI??????

5.png

????????Animator????????????????????????AI???????????????????????????

?????????behaviour_ended?behaviour_error???????????????????????????????????????

??

?????AI???????????????????AI????????????????????

???????????????????AI??????

6.jpg

?Unity?Animator???????????????????????????????????????????????????????????

????????Neutral?????????????????

7.gif

??????????????AI??????????????????????

???????AI??????AI?????????Neutral???????????????????

  • ?????????AI?????
  • ?????????AI?????????????
  • ??????AI??????AI???????????????
  • ?????????????????
  • ????????AI?AI???????
  • AI??????????????
  • AI???????


??????????????????????????????????????????????????????????????

????

????????????????????????????????????

??????????????????????????????????????????????????AI????????

??????

?????AI??????

??AI???

?Animator????????

?Animator????????????

??

??????????????

?????????????????

????????

????????

?????????

??????????

????????

?????????????????????API?

  1. public abstract class AbstractAIBehaviour : MonoBehaviour {

  2.     // ???????
  3.     [SerializeField]
  4.     protected CharController charController;

  5.     // ?????????Animator????????
  6.     abstract public int GetBehaviourHash();

  7.     // ??????????????
  8.     public event Action OnBehaviourEnded;

  9.     // ?????????????
  10.     public event Action OnBehaviourError;

  11.     // OnDisable()
  12.     // enable = true/false;
  13. }
复制代码

???????????????Unity?????????????????????????API?

?????????????????????GetBehaviourHash???Animator?????????????????????????????

????Shoot??????????Animator.StringToHash(Shoot)?

????????????????????????????????????

  1. /**
  2. *??????????????
  3. * ?????Animator?????AI????????
  4. * ???????????GetBehaviourHash????AbstractAIBehaviour???
  5. */
  6. public class BehaviourHashes {

  7.     //?????????????
  8.     static public readonly int OBJ_MOVETO_STATE = Animator.StringToHash("Obj MoveTo");

  9.     // ?????????????
  10.     static public readonly int IDLE_STATE = Animator.StringToHash("Idle");

  11.     // ???????????????
  12.     static public readonly int ROAM_STATE = Animator.StringToHash("Roam");
  13.    
  14.     // ...
  15. }
复制代码

??????AbstractAIBehaviour????????

  1.   // ?????????Animator????????
  2.     public override int GetBehaviourHash()
  3.     {
  4.         // State name in the Animator is Idle
  5.         // Animator???????Idle?
  6.         return BehaviourHashes.IDLE_STATE;
  7.     }
复制代码

???????????????????ROAM_STATE?????RoamBehaviour???

???????????????????????????????????Animator?????????????

???????????????????????????????AbstractAIBehaviour????

?????Animator

???AI????????????????????????????

?????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????

  1. /**
  2. * AIBehaviourController????AI?Animator??????
  3. */
  4. public class AIBehaviourController

  5.     /**
  6.      * ?????????
  7.      *
  8.      * ??????GetBehaviourHash???????
  9.      */
  10.     protected Dictionary<int, AbstractAIBehaviour> behaviours = new Dictionary<int, AbstractAIBehaviour>();

  11.     // AI?Animator
  12.     private Animator stateMachine;

  13.     // ????????????
  14.     private AbstractAIBehaviour currentBehaviour;   
  15.    
  16.     // ???????AI  Animator?????
  17.     public static readonly int BEHAVIOUR_ENDED = Animator.StringToHash("behaviour_ended");
  18.     public static readonly int BEHAVIOUR_ERROR = Animator.StringToHash("behaviour_error");

  19.     /**
  20.      * ???????????????
  21.      */
  22.     public void SetBehaviour(int behaviorHash)
  23.     {
  24.         // ?????????
  25.         if (currentBehaviour)
  26.             currentBehaviour.enabled = false;

  27.         try
  28.         {
  29.             // ??????
  30.             currentBehaviour = behaviours[behaviorHash];
  31.             currentBehaviour.enabled = true;
  32.         }
  33.         catch (KeyNotFoundException)
  34.         {
  35.             currentBehaviour = null;
  36.         }
  37.     }

  38.     void Awake()
  39.     {
  40.         stateMachine = GetComponent<Animator>();

  41.        // ???????
  42.         foreach (AbstractAIBehaviour behaviour in GetComponentsInChildren<AbstractAIBehaviour>())
  43.         {
  44.             // ????
  45.             behaviours.Add(behaviour.GetBehaviourHash(), behaviour);

  46.             // ????
  47.             behaviour.OnBehaviourEnded += OnBehaviourEnded;
  48.             behaviour.OnBehaviourError += OnBehaviourError;
  49.         }
  50.     }

  51.     /**
  52.      * ?????????AI?Animator
  53.      */
  54.     private void OnBehaviourEnded()
  55.     {
  56.         stateMachine.SetTrigger(BEHAVIOUR_ENDED);
  57.     }

  58.     /**
  59.      * ?????????AI?Animator
  60.      */
  61.     private void OnBehaviourError()
  62.     {
  63.         stateMachine.SetTrigger(BEHAVIOUR_ERROR);
  64.     }
  65. }
复制代码

?????????????????

  • ????????????
  • ???????????
  • ??????????????Animator?


?????????????Animator???????????????????StateMachineBehaviour?

????????Animator??????????????????Animator??????Animator????????

8.jpg

StateMachineBehaviour?????Animator????????????Animator????????????AIBehaviourController?

  1. /**
  2. * ?????AI?Animator?
  3. *
  4. * ?????????Animator???????
  5. */
  6. public class AIStateController : StateMachineBehaviour {
  7.    
  8.     /**
  9.      * ?Animator?????????AI????
  10.      */
  11.     override public void OnStateEnter(Animator animator, AnimatorStateInfo info, int layerIndex)
  12.     {
  13.         if (!animator.GetComponent<AIBehaviourController>().SetBehaviour(animatorStateInfo.shortNameHash))
  14.         {
  15.             // ???????????????????
  16.             // ??Animator????????
  17.             animator.Update(0f);
  18.         }
  19.     }
  20. }
复制代码

?????????????Unity????????Animator??????????????????????????????????

?????????????????????Update?????Animator?????

???????????????????????????????AI?Animator???????????????AI Animator???AIBehaviourController?

9.png

????????????????????????????????????????

????????AI?????????

10.png

??????

?????????????????????????????

???????????????????????????????????????AI????????

??????????????????????

????????Animator????????

???????????????

?????????????Unity?????

11.gif

??

???Unity?????AI????????????????????????????????AI??

???Synnaxium Studio  
???Unity????
????https://mp.weixin.qq.com/s/hkBmBAn7YMOnfjLygz-4Jw

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

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-10-29 12:33

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表