ãUnity7ããã£ã©ã¯ã¿ã¼ãåããããã®æ½è±¡ã¯ã©ã¹ä½æã2Dãã¼ã°ã©ã¤ã¯6ã
Unity5.0.0f4 Personal(2015å¹´6æ)
ååã®ç¶ãã§ãã
ä»åã¯ç§»åãããªãã¸ã§ã¯ããã¤ã¾ãPlayerãEnemy1ãEnemy2ã®3ãªãã¸ã§ã¯ãã«ä»ããã¹ã¯ãªããã®ãç¶æ¿ã¯ã©ã¹ãæ¸ããMovingObjectã¹ã¯ãªãããä½æãã¦ããã¾ãã
Â
ç¶æ¿ã«ã¤ãã¦ã¯ä»¥ä¸ã¨ãã»ã³ã¹ããåç §ä¸ããã
Â
 ãç®æ¨ããã£ã©ã¯ã¿ã¼ãåããããã®æ½è±¡ã¯ã©ã¹ãä½æããï¼
Â
2D Roguelike 6 of 14 : Moving Objects - YouTube
Â
â æ½è±¡ã¯ã©ã¹ã®MovingObjectã¹ã¯ãªããä½æ
æ©éã½ã¼ã¹ãè¦ã¦ã¿ã¾ãã
Â
Â
Startã¡ã½ãã
ã¾ãã¯Startã¡ã½ããã«ã¦GetComponentãå ã«è¡ããBoxCollider2Dã¨Rigidbody2Dãã¹ã¯ãªããå ã§ä½¿ãåããããã«ãã¦ãã¾ãã
Â
ã¾ããSmoothMovementã¡ã½ããã§ä½¿ãinverseMoveTimeãå ã«è¨ç®ãã¦ããã¾ãã
Â
Â
MovingObjectã¹ã¯ãªããã¯AttemptMoveã¡ã½ããâMoveã¡ã½ããâSmoothMovementã¡ã½ããã®é çªã§é²ãã§ãããããªã®ã§ããã®é çªéãã«è¦ã¦ããã¾ãããã
Â
Â
AttemptMoveã¡ã½ãã
AttemptMoveã¡ã½ããã®ä¿®é£¾åã¨ãã¦virtualãä»ãã¦ãã¾ããããã¯ç¶æ¿ãæå³ãããµãã¯ã©ã¹ã«ååã®ã¡ã½ãããããå ´åãã¹ã¼ãã¼ã¯ã©ã¹ã§ãããã®ã¹ã¯ãªããã®AttemptMoveã¡ã½ããã使ããã¨ããæå³ã§ãã
使ãéã«ã¯ãbase.ã¹ã¼ãã¼ã¯ã©ã¹ã®ã¡ã½ããåãã¨ããå½¢ã§ä½¿ãã¾ãã
Â
ã¾ããã¡ã½ããåã®å³ã«<T>ã¨ããæåãããã¾ãã
ããã¯ã¸ã§ããªãã¯ã¨ããC#ã®æ©è½ã§ãåãã¯åã決ãã¦ãããå¾ããåãæå®ããã¨ããæ©è½ã§ããæå®ã¯ãµãã¯ã©ã¹ã§è¡ã£ã¦ããã®ã§ä»ã¯ã¾ã ã©ããªåãæå®ãããã®ãã¯åããã¾ããã
ããã¤ã¡ã¼ã¸ããããããã«å ã«è¨ã£ã¦ããã¾ãã¨ãPlayerã移åããã¹ã¯ãªããã®å ´åã¯WallåããEnemyã移åããã¹ã¯ãªããã®å ´åã¯Playeråããã®<T>ã«å ¥ã£ã¦ãã¾ãã
Â
ã¡ã½ããå ã§ã¯ã¾ããRaycastHit2Dåã®å¤æ°hitãä½æãã¦ãã¾ãã
RaycastHit2Dã¯ãLinecastã«ãã£ã¦åå¾ãããªãã¸ã§ã¯ãæ å ±ãä¿åãã¦ããç¨ã®åã§ããLinecastã¯Moveã¡ã½ããã«ã¦åºã¦ãã¾ãã
Â
ç¶ãã¦å¼æ°ã«xDir(移åå x)yDir(移åå y)ãå¤æ°hitãæå®ãã¦Moveã¡ã½ãããå¼ã³åºãã¦ãã¾ãã
hitã®å·¦ã«outã¨ããæåãã¤ãã¦ãã¾ãããããã¯å¤æ°æ å ±ãã¡ã½ãããã¾ããã§ä¿æããã¨ããæå³ã§ããä»åã§ããã¨ãMoveã¡ã½ããå ã§åãåã£ããªãã¸ã§ã¯ãæ å ±ã¯AttemptMoveã¡ã½ããå ã§ã使ããã¨ãããã¨ã§ãã
Â
Moveã¡ã½ããã¯boolåã®æ»ãå¤ãããã®ã§ãboolåã®canMoveå¤æ°ã§çµæãåãåãã¾ããtrueã®æã¯ç§»åæåãfalseã®å ´åã¯ç§»å失æã§ãã
Â
ç¶ããè¦ã¦ã¿ã¾ããããhitã«ãªãã¸ã§ã¯ããä½ãå ¥ã£ã¦ããªãå ´åã¯ããã§ã¡ã½ããçµäºã§ãã
Â
hitã«ä½ããªãã¸ã§ã¯ããå ¥ã£ã¦ããå ´åã¯ã¸ã§ããªãã¯ã§æå®ãããåï¼ãã¬ã¤ã¤ã¼ãªãWallåãEnemyãªãPlayeråï¼ã§GetComponentãè¡ããªãã¸ã§ã¯ãã使ããããã«ãã¾ãã
Â
canMoveãfalseï¼ç§»å失æï¼ã§ãhitComponentã«æå®ã®åã®ãªãã¸ã§ã¯ããå ¥ã£ã¦ããå ´åãhitComponentãå¼æ°ã«OnCantMoveã¡ã½ãããå¼ã³åºãã¾ãã
Â
Moveã¡ã½ãã
Moveã¡ã½ããã§ã¯ã¾ãç¾å¨å°ã¨ç§»åå ãæ±ãã¦ãã¾ãã
Â
ç¶ãã¦Linecastã¨ããã¡ã½ãããå®è¡ãã¦ãç¾å¨å°ããç®çå°ã¾ã§ã®éã«ãblockingLayerã¨ããã¬ã¤ã¤ã¼ãæå®ããã¦ãããªãã¸ã§ã¯ãããã£ãå ´åããã®ãªãã¸ã§ã¯ãæ å ±ãåå¾ããå¦çãè¡ã£ã¦ãã¾ãã
Â
Linecastã¡ã½ããå®è¡ã®åã§boxCollideræ å ±ãOffã«ãå®è¡å¾ã«Onã«ãã¦ããã®ã¯ãèªåèªèº«ï¼PlayerãEnemyï¼ãLinecastã«å¼ã£ããã£ã¦ãã¾ãã®ãé²ãããã§ãã
Â
次ã®è¡ã§ã¯ãé害ç©ãä½ãç¡ãã£ãå ´åã«ç®çå°æ å ±(end)ãå¼æ°ã«ãSmoothMovementã¡ã½ãããå¼ã³åºãã¦ãã¾ããããã¦ã移åãæåããã¨ããæå³ã§trueãAttemptMoveã¡ã½ããã¸è¿ãã¦ãã¾ãã
é害ç©ããã£ãå ´åã¯ç§»åã«å¤±æããã¨ããæå³ã®falseãè¿ãã¦ãã¾ãã
Â
SmoothMovementã¡ã½ãã
ç¶ãã¦SmoothMovementã¡ã½ããã§ãã
Â
ã¾ãã¯ï¼ãã¯ãã«éã®è¿æ¥åº¦åãã表ãå¤sqrRemainingDistanceãæ±ãã¦ãã¾ãã
Â
ï¼ãã¯ãã«éã®æ¬æ¥ã®è·é¢ãåå¾ããæã¯magnitudeã¨ããé¢æ°ã使ãã®ã§ãããmagnitudeã¯è¨ç®ãè¤éã§å¦çãéããmagnitudeãç´¯ä¹ããå¤ãæ±ããsqrMagnitudeã®ã»ããæ ¼æ®µã«å¦çã軽ããè¿æ¥åº¦åãããã§ãã¯ãããªãåé¡ãç¡ããããä»åã¯sqrMagnitudeã使ç¨ãã¦ãã¾ãã
Â
ç¶ãã¦whileæã§ã«ã¼ãã«å ¥ã£ã¦ãã¾ãã
ã«ã¼ããæããæ¡ä»¶ã¯Epsilon(ã»ã¨ãã©0ã«è¿ãæ°å¤)ãããsqrRemainingDistanceãå°ãããªã£ãæã§ããã«ã¼ãå ã§ç¾å¨å°ã¨ç®çå°ã¯ã©ãã©ãã¨è¿ã¥ãã¦ããã¾ãã
Â
whileææåã®è¡ã«ã¦ç§»åénewPositionãæ±ããMoveTowardsã¡ã½ããã使ã£ã¦ãã¾ãã
MoveTowardsã¡ã½ããã¯ç¬¬ä¸å¼æ°ã¨ç¬¬äºå¼æ°ã®éã§ãinverseMoveTime(ããã©ã«ãã¯10) * Time.deltaTime(ç´è¿1ãã¬ã¼ã ã«ããã£ãæé)åã ã移åãã¦ãã¾ãã1ç§50ãã¬ã¼ã ã ã£ãå ´åã¯10Ã(1÷50)=0.2ãã¤ã¾ã1ãã¬ã¼ã ã«0.2ãã¤é²ãæãã§ãã
Â
次ã«ãæ±ãã移åéåã ãMovePositionã¡ã½ããã«ã¦ç§»åãã¾ãã
Â
æå¾ã«ç§»åå¾ã®ç¾å¨å°ã§ãå度2ç¹éã®è¿æ¥åº¦åããæ±ãã1ãã¬ã¼ã å¾ ã£ã¦ããwhileæã®å é ã«æ»ã£ã¦ããæãã§ãã
Â
1ãã¬ã¼ã ãã¨ã«ã¡ãã£ã¨ãã¤ç®çå°ã«è¿ã¥ãã¦ãããç®çå°ã«å°éããã移åãçµäºããã¨ããã¢ãã¡ã¼ã·ã§ã³ããããã§å®ç¾ããã¾ããã
Â
OnCantMoveã¡ã½ãã
æå¾ã«AttemptMoveã¡ã½ããããå¼ã³åºããã¦ããOnCantMoveã¡ã½ãããè¦ã¦ããã¾ãããã
Â
ãã®ã¡ã½ããã«ã¯abstract修飾åãã¤ãã¦ãã¾ããããã¯æ½è±¡ã¡ã½ããã¨è¨ããã¡ã½ããã®ä¸èº«ã¯ãµãã¯ã©ã¹ã«ã¦æ¸ã決ã¾ããããã¾ãããªã®ã§ãOnCantMoveã®è§£èª¬ã«ã¤ãã¦ã¯å¾è¿°è´ãã¾ãã
Â
<T>ã¯AttemptMoveã¡ã½ããã¨åæ§ãã¸ã§ããªãã¯æ©è½ã表ãã¾ãããã¡ãããµãã¯ã©ã¹å´ã§æå®ããã¾ãã®ã§ãå¾è¿°è´ãã¾ãã
Â
Â
ä»åã¯æ½è±¡ã¯ã©ã¹ã®èª¬æã§ãã£ãããå®éã«ãã£ã©ã¯ã¿ã¼ãåããã¦ç¢ºèªã¨ããããã«ã¯ããã¾ããã§ãããããªãã¨ãªããã£ã©ã¯ã¿ã¼ãåããæããã¤ã¡ã¼ã¸ãã¦é ããããä»å¾ã¹ã ã¼ãºã«é²ããããã¨æãã¾ãã
Â
ããã§ã¯ä»åã¯ããã¾ã§ã§ãã
ãããã¨ããããã¾ããã
Â
ã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)ã
Â