Unity5 Mecanimã®StateMachineBehaviourã¨æ¯ãã
Unity5ã§å¯ã§ãããããããMecanimï¼ã¢ãã¡ã¼ã·ã§ã³ç®¡çã·ã¹ãã ï¼ã«æ°ããStateMachineBehaviourã追å ããã¾ããã
ãã®æ©è½ã¯Mecanimã®ã¹ãã¼ã管çæ©è½ã®åã¹ãã¼ãã«æ¯ãèããè¨å®ã§ããæ©è½ã§ããåã¹ãã¼ãæ¯ã«æ¯ãã¾ããè¨å®ãããã¨ã§ãã³ã³ãã¼ãã³ãå´ããç¾å¨ã®ã¹ãã¼ãã確èªããã¨ãã£ãäºãããåãããããã«ãªãã¾ãã
ç®æ¬¡
- ç®æ¬¡
- 使ãæ¹
- 使ã£ã¦éãã§ã¿ã
- ä¸æºå
- æ¯ãèãã®ç»é²
- ã¢ãã¡ã¼ã·ã§ã³ã®è¨å®
使ãæ¹
StateMachineBehaviourãç¶æ¿ããã¯ã©ã¹ãç¨æããAddBehaviourãã¿ã³ã使ã£ã¦æ¯ãèããç»é²ããã ãã§ããStateMachineBehaviourã§ã¯å¹¾ã¤ãã®ã³ã¼ã«ããã¯ãããã¾ãã
- OnStateEnterï¼ã¹ãã¼ãã«å ¥ã£ãæã«å¼ã°ãã
- OnStateUpdateï¼æ¯ãã¬ã¼ã å¼ã°ãã
- OnStateExitï¼ã¹ãã¼ãããæããæã«å¼ã°ãã
- OnStateMoveï¼ãªãã¸ã§ã¯ãã®åº§æ¨ãåããç©ã¯ã³ã³ã§å¼ã¶
- OnStateIKï¼IKã使ãå¦çã¯ã³ã³ã§å¼ã¶
使ã£ã¦éãã§ã¿ã
å®éã«ä½¿ã£ã¦ç°¡åãªã²ã¼ã ãä½æãã¦ã¿ã¾ããã
ãã¬ã¤ã¤ã¼ã¯å·¦ã®ã¬ã¤ã³ãï¼éçï¼ã§ãå³ã®ç½ã奴ãå¾ããåãã¦ããéã«è¿ã¥ãã¨ãã£ãæãã®å 容ã§ããè¦ããã«ãã ãã¾ããããããã ãã§ãã
ä¸æºå
 ã¾ããã£ã©ã¯ã¿ã¼ã管çããAnimatorControllerã®ãã¼ã¹ã¨ãªããã®ãç¨æãã¾ããéçã¨ç½ããã¤ã®ï¼ç¨®é¡ã§ããã¢ãã¡ã¼ã·ã§ã³ãæ£ãã°ãã®ãå«ã§ããä½ãã®ã¯ç©ºã®ã¢ãã¡ã¼ã·ã§ã³ãªã®ã§ãAnimatorã«å ¥ãã¡ããã¾ãã
Unityã®AnimatorControllerã«AnimationClipãå èµãã - ãã©ã·ã¥ã¼ã«ããã°
ã¹ãã¼ãã¯ãããªæãã«ãªãã¾ããä¸ã移åããéçæ°ãä¸ãç½ããã¤ã®ã¹ãã¼ãã§ãã
æ£ç´ãã¹ãã¼ãä½ãããPlayãFadeã§ãã¬ã³ããããæ¹ãè¯ãæ°ããã¾ãããä»åã¯ã試ãã§ãã
æ¯ãèãã®ç»é²
Walkã¨Idleããã¼å ¥åã§åãæ¿ããæ¯ãèããä½ãã¾ããæ¯ãèãã追å ãããã¹ãã¼ãé¸æãã¦AddBehaviourãã¿ã³ãã¯ãªãã¯ã»ã¹ã¯ãªããåãè¨å®ããã¨ãèªåçã«ã¹ã¯ãªãããAssetsç´ä¸è¾ºãã«ä½ããã¾ãã
ã¨ãããããã¹ãã¼ãé¸ææã¯ãã£ã©ã¯ã¿ã¼ãæ©ããããæ¯ãèãã¨ããæå®ã®ãã¿ã³ãæ¼ãã¦ããéã¯æå®ã®Boolãã©ã¡ã¼ã¿ãenableã«ãããæ¯ãèããä½æãã¾ããã¡ãªã¿ã«animator.GetComponentã§ã¢ãã¡ã¼ã¿ãæã¤ãªãã¸ã§ã¯ãã¸ã®ã¢ã¯ã»ã¹ãå¯è½ãªã®ã§ãã¹ãã¼ãéã®å ±éå¤æ°ä¿ãããããã©ã¡ã¼ã¿å¼ã£ããè²ã åºæ¥ã¾ãã
BooleanSwitchByKeyã¯éçç¨ã®Idleã¨Walkã®ã¹ãã¼ãã«è¨å®ãã¦ããã¾ããã¾ãããã¼å¤å®ã¯SpaceKeyãå¤æ´ããå¤ã¯IsWalkï¼boolï¼ã¨ãã¦ããã¾ãã
Walkã¯æ©ãæ¯ãèããªã®ã§Walkã¹ãã¼ãã«è¨å®ãã¾ãã
ã¤ãã§ã«ãè¦çãã§ãã¯ãè¡ãã¾ããè¦çãã§ãã¯ã¯åç´ã«ãè¦ããã¦ãæã«åãããæ»ã¬ããªã®ã§ãç½ããã¤ã®ã¹ãã¼ããLookã®æã«éçã®Walkã¹ãã¼ããå ¥ã£ããæ»ã¬ãã¨ãã¾ããå®è£ ã¯ãâ対象ã®Animatorã®æã¤StateåãLookã®å ´åãDeadTriggerãå ¥ããâæ¯ãèããWalkã«å ¥ããããããªæãã§ãã
ããã§é¢åãããã®ããStateMachineBehaviourã¯ã·ã¼ã³ã«ãããªãã¸ã§ã¯ãã¸ã®åç §ãã¨ãã£ã¿å´ã§è¨å®ã§ããªãäºã§ã*1ããªã®ã§ãã¹ã¯ãªããå´ã§åç §å ãåå¾ãã¾ãã
ä¸ãWalkã¹ãã¼ããä¸ãIdleã¹ãã¼ãã«ã¤ãã¦ãæ¯ãèãã§ãã
ç½ããã¤ã®æåã¯ãæ£ç´é¢åãããã®ã§ï¼ç§æ¯ã«ã¹ãã¼ããã¹ã£ããããããã«ãã¾ããè¦ããã«HasExitTimeã§ãµã¯ãã¨ãã¾ãã
ã§ããå®éã¯ãæ°ç§çµã£ããæ¯ãè¿ããã¨ãã£ãæãã®æ¯ãèããè¨å®ããã®ãè¯ãããããã¾ããã²ã¼ã çã«ã¯ã
 ããããã°ãGameObject.Find("Canvas/Enemy");ãã£ã¦ã§ãããã§ãããç¥ãã¾ããã§ããã
ã¢ãã¡ã¼ã·ã§ã³ã®è¨å®
å¾ã¯ä½æããAnimatorControllerã®ãæ©ãããã¢ã¤ãã«ããæ»ã¬ããæ¯ãåããã«ã¢ãã¡ã¼ã·ã§ã³ãå²ãå½ã¦ã¾ããä¸ã§ãã£ã¦ãããããªAnimatorã®ã»ããã¢ãããç¹°ãæ¿ããã®ã¯é¦¬é¹¿ãããã®ã§ã1åä½ã£ã¦AnimatorOverrideControllerã§ã¢ãã¡ã¼ã·ã§ã³ãå·®ãæ¿ããããã«ãã¾ãã
ä½æããã«ã¯Projectãã©ã¦ã¶>Create>AnimatorOverrideController ãé¸æãã¾ããå¾ã¯è©²å½ã®Animator Controllerã«å«ã¾ããClipä¸è¦§ã表示ãããã®ã§ãä¸æ¸ããããã¯ãªãããç»é²ãã¦ããã¾ãã
ã¡ãªã¿ã«ã¬ã¤ã¤ã¼åãã¦ã¹ãã¼ãã ã使ãâ¦ã¿ãããªã®ãOKã ã¨æãã¾ãããã ãã¹ãã¼ããã·ã³ã¨ãã¦ä½¿ããªãæ£ç´ãã£ã¨è¯ãã¢ã»ãããããããããªãããªã¨æãã¾ããã±ã¼ã¹ãã¤ã±ã¼ã¹ã§ã
*1:Animatorã¯åå©ç¨ãããå¯è½æ§ããããã