ãUnity7ããã¬ã¤ã¤ã¼ç¨ã®ã¹ã¯ãªãããè¨å®ãããï¼ããã¼ã°ã©ã¤ã¯9ã
Unity5.0.0f4 Personal(2015å¹´6æ)
ååã®ç¶ãã§ãã
Â
ä»åã¯ãã¬ã¤ã¤ã¼ã移åããããå å£ãå£ããããã¢ã¤ãã ãåãå復ãããã¨ãã£ãå¦çãä½æãã¦ããã¾ãã
Â
第6åç®ã§ä½ã£ãæ½è±¡ã¯ã©ã¹ã¯ãããããããã§ä½¿ããããã¨ã«ãªãã¾ãã
 ãç®æ¨ããã¬ã¤ã¤ã¼ç¨ã®ã¹ã¯ãªãããå®æããã
Â
2D Roguelike 9 of 14 : Player Script - YouTube
Â
â GameManagerã¹ã¯ãªããã«è¿½è¨
Â
ãã®è¨äºã§ä½æããGameManagerã¹ã¯ãªããã«è¿½è¨ãã¦ããã¾ãã以ä¸ã½ã¼ã¹ã§ãã
Â
Â
ã¾ãã¯ãã¬ã¤ã¤ã¼ã®ä½åã§ããplayerFoodPointsã100ã§ä½æãã¦ãã¾ãã
ã·ã³ã°ã«ãã³ã§ããGameManagerã«ä½åãä½æãããã¨ã«ããã次ã®ã¬ãã«ã«ç§»ãæã«Sceneã®èªã¿è¾¼ã¿ããã¦åSceneã®æ å ±ãç ´æ£ããã¨ãã¦ããä½åæ å ±ãä¿æãç¶ãã¦ããã¾ãã
Â
次ã«ãã¬ã¤ã¤ã¼ã®é çªãæµã®é çªãã表ãplayersTurnå¤æ°ãboolåã§ä½æãã¾ãã[HideInInspector]ã¨æ¸ãã¦ãããã¨ã«ãããä»ã¯ã©ã¹ãã使ç¨ãããã¨ã¯ã§ãããã©ãInspectorä¸ããã¯ç·¨éã§ããªãããã«ãã¦ãã¾ãã
Â
æå¾ã«GameOverã¡ã½ãããä½æãã¦ãã¾ããenabledãfalseã«ãããã¨ã§ãã²ã¼ã ä¸ããGameManagerãæ¶ãã¦ãã¾ãã
Â
â¡Playerã¹ã¯ãªããä½æ
ç¶ãã¦Playerã«ã¤ãããPlayerã¹ã¯ãªãããä½æãã¾ãã
Â
Playerã¹ã¯ãªããã§å®ç¾ããã¦ãããã¨ã¯ããã¬ã¤ã¤ã¼ã®ç§»åã»å å£ã¸ã®æ»æã»æµããã®æ»æã»Foodå復ã»Sodaå復ã»Exitã§æ¬¡ã®ã·ã¼ã³ã¸ç§»åã»ã²ã¼ã ãªã¼ãã¼å¤å®ã¨ããã¾ãã以ä¸ã½ã¼ã¹ã§ãã
Â
Â
MovingObjectã¯ã©ã¹ãç¶æ¿ãã¦ãããããbaseã使ããã¨ã§MovingObjectã¯ã©ã¹ã®ã¡ã³ãã¼å¤æ°ãã¡ã½ãããPlayerã¯ã©ã¹å ã§ä½¿ããã¨ãã§ãã¾ãã詳ããã¯ä»¥ä¸ã¨ãã»ã³ã¹ããåç §ä¸ããã
Â
Startã¡ã½ãã
Startã¡ã½ããã§ã¯ã¾ããPlayerChopã¨PlayerHitã¢ãã¡ã¼ã·ã§ã³ã使ãæã®ããã«Animatorã³ã³ãã¼ãã³ãã®ãã£ãã·ã¥ãè¡ãããã¤ã§ã使ããããã«ãã¦ãã¾ãã
Â
次ã«ãGameManagerã¹ã¯ãªããããplayerFoodPointsãèªã¿è¾¼ã¿ããã¬ã¤ã¤ã¼ã®ä½åãåå¾ãã¦ãã¾ãã
Â
æå¾ã«MovingObjectã¯ã©ã¹ã®Startã¡ã½ãããèªãã§ãã¾ããbaseã®ä½¿ãæ¹ã«ã¤ãã¦ã¯ä»¥ä¸ã¨ãã»ã³ã¹ããåç §ä¸ããã
Â
Â
Updateã¡ã½ãã
Updateã¡ã½ããã¯ãã¬ã¤ã¤ã¼ã®ç§»åãæ¸ãã¦ãããããplayerã®é çªã§ç¡ãå ´åã¯Updateã¡ã½ãããçµäºãããå¦çãå é ã«å ¥ãã¦ãã¾ãã
Â
ç¶ãã¦GetAxisRawã¡ã½ããã使ãããã¼ãã¼ãã®å³ãã¿ã³ãæ¼ããæã«horizontal=1ãå·¦ãã¿ã³æ¼ä¸ã§horizontal=-1ãä¸ãã¿ã³æ¼ä¸ã§vertical=1ãä¸ãã¿ã³æ¼ä¸ã§vertical=-1ã¨ããå¤ãåå¾ãã¦ãã¾ãã
GetAxisRawã¡ã½ããã¯1ã0ã¨ããããªãã¦ãã¾ãããGetAxisã¨ããã¡ã½ãããããããã¡ãã¯0ãã1ã¸åãã¦å°ããã¤æ°å¤ãå ç®ããã¦ããã¾ããã¡ãã£ã¨ãã¤å éãã¦ããåãã表ç¾ãããæã¯ãGetAxisã¡ã½ããã使ãã¨ããã§ããããä½è«ã§ããï¼
Â
次ã«horizontalã«ä½ãæ°å¤ãå ¥ã£ã¦ããæãverticalã¯å¿ ã0ã«ãªãããã«ãã¦ãã¾ããããã¯ä¸ä¸ã®ãã¿ã³ã¨å·¦å³ã®ãã¿ã³ãåæã«æ¼ããæãæãã«ç§»åãã¦ãã¾ãã®ãé²ãã¾ãã
Â
æå¾ã«ä¸ä¸å·¦å³ã©ããã®ãã¿ã³ãæ¼ãããæãAtemptMoveã¡ã½ããã®å¼ã³åºãããã¦ãã¾ãã
<Wall>ã¯ã¸ã§ããªãã¯æ©è½ã®åå¼æ°ã§ãWallã¹ã¯ãªããã®Wallã¯ã©ã¹ãæå®ãã¦ãã¾ãã詳ããã¯ä»¥ä¸ã¨ãã»ã³ã¹ããåç §ä¸ããã
Â
Â
åå¼æ°ã¨ãã¦Wallã¯ã©ã¹ãæå®ããå¼æ°ã¨ãã¦horizontalã¨verticalãã¤ã¾ã移åæ¹åã渡ãã¦ãã¾ããããã§ã¯AttemptMoveã¡ã½ãããè¦ã¦ããã¾ãããã
Â
AttemptMoveã¡ã½ãã
AttemptMoveã¡ã½ããã«ã¯override修飾åãã¤ãã¦ããã®ã§ãç¶æ¿æ©è½ã使ããã¨ãã§ãã¾ãã
Â
Â
ã¾ãã¯ç§»åããæã«foodããã¤ãã¹1ãã¦ãã¾ãããå¤æ°--ãã¨æ¸ããã¨ã§ã1ãã¤ãã¹ãããã¨ãæå³ãã¾ãã
Â
ç¶ãã¦baseã使ã£ã¦MovingObjectã®AttemptMoveã¡ã½ãããå¼ã³åºãã¦ãã¾ããåå¼æ°ã¯Wallãå¼æ°ã¯Updateã¡ã½ããã§æå®ããhorizontalã¨verticalã§ãã
Â
ç¶æ¿å ã®AttemptMoveã¡ã½ããããMoveã¡ã½ãããSmoothMovementã¡ã½ããã¨é²ã¿ãã¬ã¤ã¤ã¼ã®ç§»åãå®ç¾ããã¦ããã¾ãã詳ããã¯ç¬¬6åç®ã®è¨äºã«è©³ããæ¸ãã¦ããã¾ãã®ã§ããåç §ä¸ããã
Â
Â
å å£(Wall)ã«ã¶ã¤ãã£ãæã¯ç¶æ¿å ã®AttemptMoveã¡ã½ããããOnCantMoveã¡ã½ãããå¼ã³åºããWallãæ»æãã¾ãã(å¾è¿°)
Â
次ã«CheckIFGameOverã¡ã½ãããå®è¡ããã²ã¼ã ãªã¼ãã¼å¤å®ãè¡ãã¾ãã
Â
æå¾ã«playersTurnãfalseã«ãããã¨ã§ãæµå´ã«é çªãè²ã£ã¦ãã¾ãã
Â
OnCantMoveã¡ã½ãã
OnCantMoveã¡ã½ããã¯æ½è±¡ã¡ã½ãã(abstract)ã§ãããç¶æ¿å ã§ãããã®Playerã¯ã©ã¹ã§å¿ ãè¨è¿°ããªãã¦ã¯ãªãã¾ããã
Â
Â
 ã¸ã§ããªãã¯ã®åå¼æ°ã«ã¯ãWallåãå ã»ã©æå®ãã¾ããã移åå ã«Wallããã£ãå ´åã«ãã®OnCantMoveãå®è¡ããã¾ãã
Â
ã¾ãã¯æåã®è¡ã§ãWallã¹ã¯ãªããã®Wallã¯ã©ã¹ãhitWallå¤æ°ã«æå®ãã¦ãã¾ãã
Â
次ã«hitWall.DamageWallã¨è¨è¿°ããWallã¯ã©ã¹ã®DamageWallã¡ã½ãããå®è¡ãã¦ãã¾ããããã«ããWallã¹ã¯ãªããã®hpãæ¸ãããã¨ãã§ãã¾ãã
Â
æå¾ã«Animatorã«ã¦ä½æããTriggeråã®playerChopãtrueã«ããChopã¢ãã¡ã¼ã·ã§ã³ãå®è¡ãã¦ãã¾ãã
Â
以ä¸ã§ããã¬ã¤ã¤ã¼ã®ç§»åã«ã¤ãã¦ã¯ã²ã¨éã確èªã§ãã¾ããããã£ã¡ã«ãã£ãããã£ã¡ã«ãã£ããã§ããããç²ãæ§ã§ããã
Â
OnTriggerEnter2Dã¡ã½ãã
IsTriggerãtrueã§ãããExitããFoodããSodaãã«è§¦ããæã®ã¤ãã³ããè¨å®ãã¦ãã¾ãã
Â
ã¿ã°ãExitã ã£ãå ´åãRestartã¡ã½ãããå®è¡ãã¦ãã¾ãã
Invokeã¡ã½ããã¯ã第ï¼å¼æ°ã§æå®ããç§æ°ã ãã¡ã½ãããå®è¡ããã¿ã¤ãã³ã°ãé ããã¾ããä»åã§ããã¨1ç§é ããã¾ãã
Â
ã¿ã°ãFoodãSodaã ã£ãå ´åãä½åãå復ããããã®å¾åã¢ã¤ãã ãç¡å¹ã«ãã¦é表示ã«ãã¦ãã¾ãã
Â
Restartã¡ã½ãã
Exitã¾ã§å°éããæã«ã1ç§é ãã¦å®è¡ãããã¡ã½ããã§ãã
Â
mainã·ã¼ã³ãå度èªã¿è¾¼ããã¨ã«ãããã·ã³ã°ã«ãã³ã§ããGameManagerã§ä¿æãã¦ãããã¬ã¤ã¤ã¼ã®ä½åã¨ã¬ãã«ä»¥å¤ãåæç¶æ ã«æ»ãã次ã®ã¹ãã¼ã¸ã¸é²ã¿ã¾ãã
Â
OnDisableã¡ã½ãã
Restartã¡ã½ãããå®è¡ããæã«ãã®Playerã¹ã¯ãªãããä¸æ¦ç¡å¹(Disable)ç¶æ ã«ãªãã¾ããããã®æç´åã«å¼ã³åºãããã®ããã®OnDisableã¡ã½ããã§ãã
Â
ãã¬ã¤ã¤ã¼ã®ä½åãã·ã³ã°ã«ãã³ã§ããGameManagerã¸ä¿åãã¦ãã¾ãã
Â
LoseFoodã¡ã½ãã
LoseFoodã¡ã½ããã¯ãæµãã£ã©ã«æ»æãããæã«å¼ã³åºããã¾ãã
Â
ã¾ãã¯ãã¬ã¤ã¤ã¼Animatorã§ä½æããTriggeråã®playerHitãtrueã«ããã¢ãã¡ã¼ã·ã§ã³ããã¦ãã¾ãã
Â
次ã«å¼æ°lossåã ãä½åãæ¸ãããæå¾ã«CheckIfGameOverã¡ã½ãããå¼ã³åºããã²ã¼ã ãªã¼ãã¼å¤å®ãè¡ã£ã¦ãã¾ãã
Â
CheckIfGameOverã¡ã½ãã
Updateã¡ã½ãããLoseFoodã¡ã½ããã«ã¦ä½åãå°½ããæã«å¼ã³åºããã¾ãã
Â
ãã¬ã¤ã¤ã¼ã®ä½åã0以ä¸ã«ãªã£ãæãã·ã³ã°ã«ãã³ã§ããGameManagerã®GameOverã¡ã½ãããå®è¡ããã²ã¼ã ãªã¼ãã¼ã¨ãªãã¾ãã
Â
ç¶æ¿ã¨ã¸ã§ããªãã¯æ©è½ãé£ãããç解ããã®ã«ããªãæéãããã£ã¦ãã¾ãã¾ããã
ä»åå¦ãã ãã¨ã¯ä»å¾ãæ´»ç¨ãã¦ããããã¨æãã¾ãã
Â
ããã§ã¯ãä»åã¯ããã¾ã§ã§ãã
ãããã¨ããããã¾ããã
Â
ãUnityéçº7ãã¢ãã¡ã¼ã·ã§ã³ãä½ãããï¼ã2Dãã¼ã°ã©ã¤ã¯2ã
ãUnityéçº7ãFloorãã¢ã¤ãã ãä½æãããï¼ã2Dãã¼ã°ã©ã¤ã¯3ã
ãUnityéçº7ãBoardManagerãå¼ã³åºãGameManagerãä½æããã2Dãã¼ã°ã©ã¤ã¯5ã
ãUnityéçº7ãç ´å£å¯è½ãªå£(Wall)ãä½æãããï¼ããã¼ã°ã©ã¤ã¯7ã
ãUnityéçº7ããã¬ã¤ã¤ã¼ã®ã¢ãã¡ã¼ã¿ã¼ãè¨å®ãããï¼ããã¼ã°ã©ã¤ã¯8ã
ãUnityéçº7ããã¬ã¤ã¤ã¼ç¨ã®ã¹ã¯ãªãããè¨å®ãããï¼ããã¼ã°ã©ã¤ã¯9ã
ãUnityéçº7ãBGMã¨å¹æé³ãä»ããã2Dãã¼ã°ã©ã¤ã¯13ã
ãUnityéçº7ãã¿ããããã«ã«å¯¾å¿ããããï¼ã2Dãã¼ã°ã©ã¤ã¯14(Fin)ã
Â