解説
hitboxããã³hurtboxãšã¯äœã§ããïŒ ããã¯åãããšã§ã¯ãããŸãããïŒ
çãã¯ããªãã誰ã«è³ªåãããã«ãã£ãŠç°ãªããŸãããèšäºã§ã¯ããããããã¯ã¹ãšããŒãããã¯ã¹ã¯ãé©åãªæ Œéã²ãŒã ã®å Žåã®ããã«ãçšéã®ç°ãªã2ã€ã®ç°ãªãæŠå¿µã§ãããšèããŸãã
Hitboxã¯ãæ»æããããããå Žæã決å®ããé衚瀺ã®åè§åœ¢ïŒãŸãã¯çäœïŒã§ãã
Hurtboxãç®ã«èŠããªãé·æ¹åœ¢ïŒãŸãã¯çäœïŒã§ããããã¬ãŒã€ãŒãŸãã¯ãªããžã§ã¯ããHitboxã䜿çšããŠãããã§ããå Žæãå®çŸ©ããŸãã
Street Fighter IVã®ãã®ç»åã§ã¯ãèµ€ãããã¯ã¹ããããããã¯ã¹ã§ãç·ã®ããã¯ã¹ãããŒãããã¯ã¹ã§ã
ãããããã¯ã¹ãšããŒãããã¯ã¹ã®ãµã€ãºãšäœçœ®ã¯ãåçãããã¢ãã¡ãŒã·ã§ã³ãã¬ãŒã ã«äŸåããããšã«æ³šæããŠãã ããã
Killer Instinctã®Gifã ãããããã¯ã¹ã¯ã·ã§ãããã¬ãŒã ã«ã®ã¿è¡šç€ºãããå£ãšãšãã«ç§»åããããšã«æ³šæããŠãã ããã
Killer Instinctã®äŸã«ã¯ã Pushbox ïŒé»è²ã®é·æ¹åœ¢ãHurtboxã¯ç©ºã®ç·ã®é·æ¹åœ¢ïŒãšããå¥ã®ã¿ã€ãã®é·æ¹åœ¢ããããŸãã ããã·ã¥ããã¯ã¹ã¯ããã£ã©ã¯ã¿ãŒãç©ççã«å æããã¹ããŒã¹ã瀺ãé åã§ããããã£ã©ã¯ã¿ãŒãéãªããªãããã«ããŸãã
æŠéãšããŒãã¢ããã®ãžã£ã³ã«ã®ã»ãšãã©ã®ã²ãŒã ã§ã¯ãç°¡åã«ããããã«èæ ®ããªã2ã€ã®ã¿ã€ãã®é åããããŸãã
ã°ã©ããŸãã¯ã¹ããŒããã¯ã¹ã¯ããã£ã©ã¯ã¿ãŒãã€ããã ãæãããã§ããé åãå®çŸ©ãã ãããã¯ããã¯ã¹ã¯ãæ»ããã¿ã³ãæŒããæ»æããããã¬ãŒã€ãŒãåŸæ¹ã«ç§»åãã代ããã«æ»æããããã¯ãå§ããé åã決å®ããŸãã
èšèšã®èŠ³ç¹ãããããããã¹ãŠã®é åã¯éåžžã«éèŠã§ãã æ»æã®ãããããã¯ã¹ãšããŒãããã¯ã¹ã¯ãæ»æããã¡ãŒãžãäžãããã¬ãŒã ã®æ°ã ãã§ãªãããã®æ»æã®æ»è§ãšãã¬ã€ã€ãŒã®è匱æ§ã決å®ããŸãã
ã¹ããªãŒããã¡ã€ã¿ãŒã®äŸã§ããã®è¯ã説æã¯ãããªã§æ瀺ãããŸãã
ããã§ã¯ãçšèªãç解ããããäœæ¥ã«åãæãããŸãããã
äœã欲ãã
äœæ¥ããããšãªã¢ã®åã¿ã€ããèŠãŠããããããäœã欲ããããèšããŸãããïŒ
ããã·ã¥ããã¯ã¹ïŒã¿ããããã«ã¯2ã€ã®ããã·ã¥ããã¯ã¹ãå¿ èŠã§ãããäºãã«éãªãåãããšã¯ãããŸããïŒãã®ããããããã·ã¥ããã¯ã¹ãšãªã¢ããšåŒã°ãããããå¥ã®ãã£ã©ã¯ã¿ãŒãããã·ã¥ããŸãïŒã ããã·ã¥ããã¯ã¹ã¯ãä»ã®ããã·ã¥ããã¯ã¹ãšã®ã¿å¯Ÿè©±ããå¿ èŠããããŸãã
ããŒãããã¯ã¹ïŒããããèšé²ã§ããŸãããç©ççãªæå³ã§ã®è¡çªã¯å®è¡ããªãã§ãã ããã ããŒãããã¯ã¹ã¯ããããããã¯ã¹ãšã®ã¿å¯Ÿè©±ããå¿ èŠããããŸãã
HitboxïŒä»»æã®ãã¬ãŒã ã§Hurtboxã«ãªãŒããŒã¬ã€ãããã©ããã確èªã§ããã¯ãã§ãã Hurtboxãšã®ã¿å¯Ÿè©±ããå¿ èŠããããŸãã
Unityæšæºã³ã³ããŒãã³ãã®äœ¿çš
ãŸããããŸããŸãªã¿ã€ãã®é åãæšæºã®Unityã³ã³ããŒãã³ãã«ãã€ã³ãããããšãã§ããŸãã æãããªéžæã¯ã Colliderã§ãã
Pushboxã¯ãCollider plus RigidbodyãšããŠçŽæ¥å®è£ ã§ããŸãã ãªããžã§ã¯ããšã®è¡çªã«å¿çããä»ã®ããã·ã¥ããã¯ã¹ãšéãªããªãããã«ãå¿ èŠãªãšããã«åäœããŸãã
ïŒé©åãªRigidbodyèšå®ãé€ããŠïŒå¿é ããå¿ èŠãããã®ã¯ãã ä»ã®ããã·ã¥ããã¯ã¹ãšã®ã¿è¡çªããããšãã§ãã ãããããã£ã®å®è£ ã§ãã Unityç©çã·ã¹ãã ã«ç²ŸéããŠããå Žåã解決çã¯ã¬ã€ã€ãŒãšã¬ã€ã€ãŒè¡çªãããªãã¯ã¹ã䜿çšããããšã§ããããšãæ¢ã«ç¥ã£ãŠããŸãã ããããããããããã«ãPushboxãšããã¬ã€ã€ãŒãäœæããŠãªããžã§ã¯ãã«å²ãåœãŠã PushboxãPushboxãšã®ã¿è¡çªããããã«ã³ãªãžã§ã³ãããªãã¯ã¹ãæ§æã§ããŸãã
Hurtboxesã®å ŽåãisTriggerã䜿çšããŠColliderãååŸã§ããŸãã ãã®ããã圌ã¯ç©ççãªæå³ã§è¡çªãå®è¡ããã圌ã®ãšãªã¢ã«èœã¡ãä»ã®ã³ã©ã€ããŒã®ã¿ãç»é²ããããšãä¿èšŒããŸãã 圱é¿ãèšé²ããã«ã¯ã OnTriggerEnterã€ãã³ããå®è£ ããåããªããžã§ã¯ãã«ã¹ã¯ãªãããè¿œå ããå¿ èŠããããŸããå Žåã«ãã£ãŠã¯ãçä¿¡ã³ã©ã€ããŒã®ã¿ã°ããã§ãã¯ããŠãã€ãã³ããããªã¬ãŒããã³ã©ã€ããŒãå¿ èŠã§ããããšã確èªããã²ãŒã ã«å¿ èŠãªãã¡ãŒãžãšãã«ã¹ã®èšç®ãå®è¡ããå¿ èŠããããŸãã ããªãã¯ãããããã®ã¢ãããŒãã«ç²ŸéããŠããã§ãããã
ãŸããHurtboxã¬ã€ã€ãŒãšHitboxã¬ã€ã€ãŒãäœæããå¿ èŠããããŸããHurtboxãHitboxã®ã¿ãšè¡çªããããã«ãåã³Layer Collision Matrixã䜿çšããŸãã
- Rigidbodyã¯å¿ èŠãããŸããããè¿œå ãããã¹ãŠã®ããªã¬ãŒã¯ãRigidbodyãæ¢ã«æã£ãŠããPushboxãªããžã§ã¯ãã®åã«ãªããšä¿¡ããŠããããã§ãã ããã¯éèŠã§ãããªããªãããªãžããããã£ãŸãã¯ãã®èŠªãªããžã§ã¯ããæããªãã³ã©ã€ããŒã¯ãUnityãšã³ãžã³ã«ãã£ãŠStaticãšããŠå®çŸ©ããã ãããã移åããŠãã¢ã¯ã·ã§ã³ãçºçããªãããã§ãã
- ããã«ããããããHitboxãã¬ã€ã€ãŒãšæµã®Hitboxãåºå¥ããå¿ èŠããããŸãã Hurtboxã«ã€ããŠãåãããšãèšããŸãã ããã«ããããã¬ã€ã€ãŒã®ãããããã¯ã¹ã¯ããŒãããã¯ã¹ã®æµã«ãããããã§ãããæµã®ãããããã¯ã¹ã¯ãã¬ã€ã€ãŒã®ããŒãããã¯ã¹ã«ãããããã§ããŸããã å³æ¹ã«ãã¡ãŒãžãäžãããããããã§ã¯ãã¬ã€ã€ãŒãèªåã®ããŒãããã¯ã¹ããããã§ããªãããã«æ³šæããå¿ èŠããããŸãã
ãããããã¯ã¹ã¯å®è£ ãæãå°é£ã§ãã ç©ççãªè¡çªãé¿ããããã«ãisTriggerã§Colliderã䜿çšã§ããŸããå®éãHitboxã«å«ãŸããã³ã©ã€ããŒã¯ãããŸããã ãã®åé¡ã解決ããå¥ã®æ¹æ³ããããŸããHitboxã¯ãHurtboxã«ãå ¥ãããŸãïŒ ãŸãã¯ãªãŒããŒã¬ã€ããã§ãã¯ããŸã ïŒã ãšã«ãããColliderãå¿ èŠã§ããããããªããšã Unityã¯Hurtboxã§OnTriggerEnterãåŒã³åºããŸãã ã
Hurtboxã«ãã¡ãŒãžãäžããã«ã¯ãåããªããžã§ã¯ãã«ã¹ã¯ãªãããè¿œå ããŠãHurtboxãGetComponent <T>ã䜿çšããŠãã©ã®çšåºŠã®ãã¡ãŒãžãäžããå¿ èŠãããããèŠã€ããããããã«ããå¿ èŠããããŸãã ãããå¥ã®æ¹æ³ã§è¡ãããšãã§ããŸããäž¡æ¹ã®OnTriggerEnter Colliderã«å¯ŸããŠåŒã³åºãããŸãã ãŸãããããããã¯ã¹ãå¿ èŠãªãšãã«ã®ã¿ã¢ã¯ãã£ãã«ãããã¹ãŠã®ãã¬ãŒã ã§ã¯ãªãããã£ã©ã¯ã¿ãŒãæ»æããªããšãã«ã¢ã¯ãã£ãã«ããæ¹æ³ãèŠã€ããå¿ èŠããããŸãã ãããè¡ãã«ã¯ã ããã¥ã¡ã³ãã«ãããšã ããªã¬ãŒã€ãã³ããç¡å¹ãªMonoBehavioursã«éä¿¡ãããã³ãªãžã§ã³ãžã®å¿çã«ããã€ãã¢ãŒãå«ããããšãã§ãããããã¹ã¯ãªããããªãã«ããããšãã§ããŸãã
ã³ã©ã€ããŒãæå¹ãŸãã¯ç¡å¹ã«ããããã¹ã¯ãªããã«ããŒã«å€ãè¿œå ããŠãããããããã©ããã瀺ããŸãã
åé¡
- éå±€ïŒ OnTriggerEnterã«å¿çã§ããããã«ããã«ã¯ãColliderã䜿çšããŠåãªããžã§ã¯ãã«ã¹ã¯ãªãããäœæããå¿ èŠããããŸãã é åºã®ããã«ãã¹ãŠã®ã¹ã¯ãªããã1ãæã«ä¿æãããå Žåã¯ãä»ã®ãã¹ãŠã®ãªããžã§ã¯ãã«åŒã³åºããå§ä»»ããããã ãã«ã¹ã¯ãªãããäœæããå¿ èŠããããŸãã
- é床ïŒãã®ã¢ãããŒãã§ã¯ãHitboxã«ã¯å¿ èŠã®ãªã倧ããªæ©èœããããŸãã
- ã€ãã³ãïŒç§ãã¡ã®æ©èœã¯OnTriggerEnterã«åºã¥ããŠããŸãã Unityã€ãã³ãã®äœ¿çšã¯åé¡ã§ã¯ãªããããããŸããã ããããå°ãªããšããããã®å¿ èŠæ§ã«ã€ããŠèããçç±ããããŸãã ããã«ç¥ãã«ã¯ãããã調ã¹ã䟡å€ããããŸã ïŒ ãUnity APIãžã®é«äŸ¡ãªåŒã³åºãã®åé¿ãã»ã¯ã·ã§ã³ãåç § ïŒã
- èŠèŠçãªãã€ãºïŒç°ãªãæ»æã«ç°ãªããããããã¯ã¹ã䜿çšããå Žåãäžèšã®åé¡ãåã³çºçããã ãã§ãªãããšãã£ã¿ãŒãŠã£ã³ããŠã«ãèŠèŠçãªãã€ãºããããŸãã
- æè»æ§ãäœãïŒ ColliderãHitboxãšããŠäœ¿çšããå Žåãããšãã°ãBoxããSphereã«ã³ã©ã€ããŒã®åœ¢ç¶ãå€æŽããå Žåããã¹ãŠã®BoxColliderãæåã§åé€ããSphereColliderãè¿œå ããå¿ èŠããããŸãïŒãŸãã¯ããã®äœæ¥ãè¡ããšãã£ã¿ãŒã¹ã¯ãªãããèšè¿°ããŸãïŒ
ç§ãã¡ã¯ãã¹ãŠèªåã§äœããŸã
äžèšããããããç解ããããã«ãããã·ã¥ããã¯ã¹ãšããŒãããã¯ã¹ã¯ãæšæºã®Unityã³ã³ããŒãã³ãã«ãã£ãŠéåžžã«äŸ¿å©ã«å®è£ ãããŸãã
ããŒãããã¯ã¹ã«ã¯ãŸã äžèšã®åé¡ãããããããã®ããã€ãã解決ããŸãããæœè±¡åããå¿ èŠãããäž»èŠãªãšã³ãã£ãã£ã¯ãããããã¯ã¹ã§ãã
å€æ°ã®æ»æãšã³ã³ããå«ãæ Œéã²ãŒã ãäœæããå Žåããªããžã§ã¯ãå ã®ãã¹ãŠã®æ»æãé©åã«é åºä»ãããããããã«å¯ŸããŠHitboxã®è€æ°ã®çµã¿åãããäœæã§ããããã«ããå¿ èŠããããŸãã ãããè¡ãã«ã¯ãOnTriggerEnteråŒã³åºããã¢ã¯ãã£ããªæ»æã«å³å¯ã«å§ä»»ããããåæ§ã®ããšãè¡ãã¹ã¯ãªãããå¿ èŠã§ãã
æ»æããšã«åå¥ã®Hitboxãªããžã§ã¯ããäœæããå¿ èŠã¯ãããŸãããããã§ã¯ããµã€ãºãå€æŽããŠåããã®ã䜿çšã§ããŸãã
ãããããã¯ã¹
次ã®åé¡ã解決ããã«ã¯ãæ°ããã³ã³ããŒãã³ããå¿ èŠã§ãã
- Hitboxã®åäœããããã«ã¯ãä»»æã®ãã¬ãŒã ã§Hurtboxã®ãªãŒããŒã¬ã€ããã§ãã¯ã§ããå¿ èŠããããŸãã 圌ã¯Hurtbox-sãšã®ã¿å¯Ÿè©±ãã矩åããããŸãã
- ã·ãŒã³ãŠã£ã³ããŠã§èŠèŠçã«è¡šçŸããŸãã
- ã«ã¹ã¿ãã€ãºå¯èœãã€æè»ã§ããããšã
- çæ³çã«ã¯ãUnity APIã€ãã³ãã«äŸåããªãã§ãã ããã
- å¥ã®ãªããžã§ã¯ãã«ã¹ã¯ãªããã䜿çšããããã«ååã«ç¬ç«ããŠããã
- ç¹å®ã®æ»æã«å·çããªãã§ãã ããã ãããããã¯ã¹ã¯ãããã€ãã®ç°ãªãæ»æã«é©çšã§ããå¿ èŠããããŸãã
æ¯ãèã
æåã«ããšãªã¢ãã³ã©ã€ããŒã«éãªã£ãŠããããšãã©ã®ããã«ç¢ºèªããŸããïŒ çãã¯UnityEngine.Physicsã䜿çšããããšã§ã ã
ç©çåŠã«ã¯ããã®ã¿ã¹ã¯ãéæã§ããå€ãã®ã¡ãœããããããŸãã å¿ èŠãªãã©ãŒã ïŒBoxãSphereãCapsuleïŒãæå®ããå¿ èŠã«å¿ããŠããããããã³ã©ã€ããŒïŒããå ŽåïŒãé åãšããŠååŸãããããããã®ã³ã©ã€ããŒã§åããããã«é åãæž¡ããŸãã ããã«ã€ããŠã¯èããŸããããæåã®å Žåã¯æ°ããé åãå²ãåœãŠããã2çªç®ã®å Žåã¯æ¢åã®é åã«å ¥åããã ãã§ãã
é·æ¹åœ¢ã®é åãäœãã«åœãã£ãŠãããã©ããã確èªããããšããå§ããŸãããã ãã®ããã«ã OverlapBoxã䜿çšã§ããŸãã
ãã§ãã¯ãããé·æ¹åœ¢ã®å¯žæ³ãèšå®ããå¿ èŠããããŸãã ãããè¡ãã«ã¯ãé·æ¹åœ¢ã®äžå¿ããã®ååã®ãµã€ãºãå転ãããã³ãããããå¿ èŠãããã¬ã€ã€ãŒãå¿ èŠã§ãã ååã®ãµã€ãºã¯åæ¹åã®ãµã€ãºã®ååã§ããããšãã°ããµã€ãºïŒ2ã6ã8ïŒã®é·æ¹åœ¢ãããå Žåããã®ååã®ãµã€ãºã¯ïŒ1ã3ã4ïŒã«ãªããŸãã
äžå¿ãšããŠãGameObjectã®å€æäœçœ®ã䜿çšã§ããŸããå転ã«ã¯ãGameObjectã®å€æå転ã䜿çšããããäžè¬å€æ°ãè¿œå ããŠç¹å®ã®å€ãèšå®ã§ããŸãã
ååã®ãµã€ãºã¯Vector3ã§ãããããäžè¬çã«äœ¿çšããŠäœ¿çšããŸãã
ãããããã¬ã€ã€ãŒã®å Žåã LayerMaskã¿ã€ãã®ãããªãã¯ããããã£ãäœæããŸãã ããã«ãããã€ã³ã¹ãã¯ã¿ãŒã§ã¬ã€ã€ãŒãéžæã§ããŸãã
Collider[] colliders = Physics.OverlapBox(position, boxSize, rotation, mask); if (colliders.Length > 0) { Debug.Log("We hit something"); }
æ£ããèšå®ã®å Žåãããã³åŒã³åºãäžã«æ圱ãããé·æ¹åœ¢ã察å¿ãããã¹ã¯ã®ã³ã©ã€ããŒã«ã¹ãŒããŒã€ã³ããŒãºããããšãã³ã³ãœãŒã«ã«ã¡ãã»ãŒãžã衚瀺ãããã¯ãã§ãã
èŠèŠçãã¬ãŒã³ããŒã·ã§ã³
ããã¯ãã¹ãŠçŽ æŽãããã§ãã...æ©èœçã§ã¯ãããŸããã ã©ããã§å®çŸ©ãããé·æ¹åœ¢ãèŠãããšãã§ãããŸã§ãHitboxã®é©åãªãµã€ãºãšå Žæãæå®ããããšã¯éåžžã«å°é£ã§ãã
ããã§ã¯ãã²ãŒã èªäœã§ã¯ãªããã·ãŒã³ãŠã£ã³ããŠã«é·æ¹åœ¢ãã©ã®ããã«æç»ããŸããïŒ OnDrawGizmosã䜿çšããŸã ã
ããã¥ã¡ã³ããèšãããã«ã Gizmosã¯ã·ãŒã³ãŠã£ã³ããŠã§ã®èŠèŠçãªãããã°ãŸãã¯è£å©ãã«ãçšã«èšèšãããŠããŸã ã ãŸãã«å¿ èŠãªãã®ïŒ
ã®ãºã¢ã®è²ãšå€æãããªãã¯ã¹ãæäŸããŸãã ã€ãŸããå€æã®äœçœ®ãå転ãã¹ã±ãŒã«ã§ãããªãã¯ã¹ãäœæããã ãã§ãã
private void OnDrawGizmos() { Gizmos.color = Color.red; Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, transform.localScale); Gizmos.DrawCube(Vector3.zero, new Vector3(boxSize.x * 2, boxSize.y * 2, boxSize.z * 2)); // - }
å¿ èŠã«å¿ããŠã OnDrawGizmosSelectedã䜿çšããŠããªããžã§ã¯ããéžæãããšãã«ã®ã¿é·æ¹åœ¢ãæç»ã§ããŸãã
ã«ã¹ã¿ãã€ãºæ§ãšæè»æ§
ã«ã¹ã¿ãã€ãºæ§ã¯åºç¯ãªãããã¯ã§ãããäœæãããã²ãŒã ã®ã¿ã€ããšå¿ èŠãªæ©èœã«å€§ããäŸåããŸãã
ãã®å Žåããããããã¯ã¹ã®åœ¢ç¶ãšè²ããã°ããå€æŽã§ããŸãã Anima2DãŸãã¯äœããã®éªšæ Œã¢ãã¡ãŒã·ã§ã³ã䜿çšããå Žåãå¿ èŠã«ãªãå ŽåããããŸãã ãããããã¯ã¹ãããŒã³ã®ã¹ã±ãŒã«ã«åãããŠã¹ã±ãŒãªã³ã°ãããããã«ããŸãã
ãã©ãŒã ãå€æŽããã«ã¯ãããŒã«ããããã£ãè¿œå ããOverlapBoxãä»ã®ãã©ãŒã ïŒOverlapSphereãªã©ïŒã«å€æŽããã ãã§ã ã çãèšå®ããã«ã¯ããããªãã¯radiusããããã£ãè¿œå ããå¿ èŠããããŸãã æ°ãããã©ãŒã ãæç»ããã«ã¯ãOnDrawGizmosã€ãã³ããå€æŽããå¿ èŠãããããšãå¿ããªãã§ãã ããïŒãã®äŸã§ã¯ãããã¯DrawSphereã«ãªããŸã ïŒã
æ°ããã³ã³ããŒãã³ãã¯è¿œå ãããäœãåé€ãããè¡çªããã§ãã¯ãããšãã«éè€ãããã©ãŒã ãéžæããããŒã«å€ãäœæããã ãã§ããããšã«æ³šæããŠãã ããã ããã«ãããæ»æã«å¿ããŠïŒãŸãã¯åãæ»æã§ãïŒãããããã¯ã¹ã®åœ¢ç¶ãå€æŽã§ããŸãã
è²ã«é¢ããŠã¯ãéã¢ã¯ãã£ãã§ãããè¡çªããã§ãã¯ããããŸãã¯äœããšã®è¡çªãæ€åºãããšããæ¡ä»¶ã§ãHitboxã®è²ãå€æŽããå¿ èŠããããŸãã åŸã§ããžãã¯ã®ããã«ãããã®ç¶æ ãå¿ èŠã«ãªãã®ã§ãããããè¿œå ããŸãããã
Hitboxã®ããããã£ãšããŠè¿œå ããç¶æ ãš3ã€ã®è²ã®åæãäœæããŸãã
public enum ColliderState { Closed, Open, Colliding }
ã¯ã©ã¹ã¯æ¬¡ã®ããã«ãªããŸãã
public class Hitbox: MonoBehaviour { public LayerMask mask; public bool useSphere = false; public Vector3 hitboxSize = Vector3.one; public float radius = 0.5f; public Color inactiveColor; public Color collisionOpenColor; public Color collidingColor; private ColliderState _state; /* */ }
Gizmos.color = Color.red;
ãšããè¡ã眮ãæããããšã§ãã®ãºã¢ãæŽæ°ã§ã
Gizmos.color = Color.red;
æ°ããã¡ãœãããåŒã³åºãã«ã¯ïŒ
private void checkGizmoColor() { switch(_state) { case ColliderState.Closed: Gizmos.color = inactiveColor; break; case ColliderState.Open: Gizmos.color = collisionOpenColor; break; case ColliderState.Colliding: Gizmos.color = collidingColor; break; } }
ã§ã¯ãã©ãã§ç¶æ ãå€æŽããŸããïŒ æ¬¡ã®3ã€ã®èŠçŽ ãå¿ èŠã§ãã
- ãããããã¯ã¹ã«è¡çªãã§ãã¯ãéå§ããããæ瀺ããæ¹æ³
- 圌ã«ãããããã«èšãæ¹æ³
- è¡çªãã§ãã¯æ¹æ³
æåã®2ã€ã¯æããã§ãã
public void startCheckingCollision() { _state = ColliderState.Open; } public void stopCheckingCollision() { _state = ColliderState.Closed; }
Hitboxãã¢ã¯ãã£ãã«ãªã£ãã®ã§ãåãã¬ãŒã ã§ãã¢ã¯ãã£ããªãšãã«äœããšè¡çªãããã©ããã確èªããŸãã ããããããšã§ã次ã®é ç®ã«é²ã¿ãŸãã
Unity Events APIã®ç¬ç«
ãåç¥ãããããŸãããã Updateã䜿çšããŠåãã¬ãŒã ã§äœãããã§ãã¯ã§ããŸãïŒç°¡åã«ããããã«ããµã€ãºå€æŽã®ããã®ãã§ãã¯ã¯è¿œå ããŸããïŒã
private void Update() { if (_state == ColliderState.Closed) { return; } Collider[] colliders = Physics.OverlapBox(position, boxSize, rotation, mask); if (colliders.Length > 0) { _state = ColliderState.Colliding; // - } else { _state = ColliderState.Open; } }
ã芧ã®ãšãããçŸåšã®ç¶æ ããClosedãã®å Žåã«ã®ã¿æ»ããŸãã ããã¯ãHitboxãäœãã«ééããå Žåã§ãè¡çªããã§ãã¯ããããšãæå³ããŸããããã«ãããHitboxã¯æåã®ãããã ãã§ãªããè€æ°ã®ãªããžã§ã¯ããäžåºŠã«ãããã§ããŸãã ã²ãŒã ã§ã¯ãç°ãªãåŠçãå®è¡ã§ããŸãã
Updateã䜿çšããŸãããUnity Events APIã«äŸåããããããŸããïŒ è§£æ±ºçã¯ãç¬èªã®ãããªãã¯æŽæ°ã¡ãœãããäœæããããšã§ããããã¯hitboxUpdateãšåŒã°ãïŒãã®å 容ã¯Updateã¡ãœãããšåãã«ãªããŸãïŒãçŸåšã®æ»æã§äœ¿çšãããHitboxã§ã®ã¿åŒã³åºãããŸãã
æããã«ãéå±€ã®äžäœã®ããã€ãã®ãªããžã§ã¯ãã§UpdateïŒïŒãåŒã³åºãå¿ èŠããããŸãããããããååšãããšããçç±ã ãã§ãã¹ãŠã®Hitboxã§åžžã«äœ¿çšããå¿ èŠã¯ãããŸããã
å¥ã®ãªããžã§ã¯ãã§ã¹ã¯ãªããã䜿çšãããããããã¯ã¹
èŠããŠãããŠãã ãã-Colliderã䜿çšããéã®åé¡ã¯ãOnTriggerEnterãå®è£ ããããã«ãåãGameObjectå ã«ã¹ã¯ãªãããå¿ èŠã ã£ãããšã§ããïŒ ç¬èªã®ã¹ã¯ãªããã䜿çšããããããããããã®ã«è¿œå ã§ããããããœãªã¥ãŒã·ã§ã³ã¯éåžžã«æçœã§ãã
ãªããžã§ã¯ããããããã£ãšããŠè¿œå ããŠãHitboxãè¡çªãããšãã«ãªããžã§ã¯ãã®ã¡ãœãããåŒã³åºãããšãã§ããããã«ããŸãã
ãã®åé¡ã解決ããã«ã¯ãããŸããŸãªã¢ãããŒãã䜿çšã§ããŸãã
- ãããªãã¯GameObjectãè¿œå ããŠã SendMessageã䜿çšã§ããŸãïŒãã®ã¡ãœããã¯éåžžã«äœéã§ãïŒã
- Hitboxãè¡çªãããšãã«åŒã³åºãããã¡ãœããã䜿çšããŠãMonobehaviourã§ãåãããšãã§ããŸãã ãã®ã¡ãœããã«ã¯æ¬ ç¹ããããŸããHitboxã§ç°ãªãã¹ã¯ãªããã䜿çšããå Žåã¯ãããããã¹ãŠã®ããããã£ãè¿œå ããããåŒã³åºãããã¡ãœãããå«ãããŒã¹ã¹ã¯ãªããããç¶æ¿ããå¿ èŠããããŸãã
- åŒã³åºãã¡ãœããã䜿çšããŠã€ã³ã¿ãŒãã§ã€ã¹ãäœæããHitboxã䜿çšããåã¯ã©ã¹ã«å®è£ ã§ããŸãã
æ§é çãªèŠ³ç¹ãããç§ã«ãšã£ãŠæãããªéžæã¯ã€ã³ã¿ãŒãã§ã€ã¹ã§ãã Unityã®å¯äžã®åé¡ã¯ãããã©ã«ããšãã£ã¿ãŒãã€ã³ã¿ãŒãã§ã€ã¹ããããªãã¯ããããã£ãšããŠè¡šç€ºããªãããããšãã£ã¿ãŒã§ã€ã³ã¿ãŒãã§ã€ã¹ãå²ãåœãŠãããšãã§ããªãããšã§ãã 次ã®æ®µèœã§ã¯ããããé倧ãªåé¡ã§ã¯ãªãçç±ã説æããŸãã
ã€ã³ã¿ãŒãã§ãŒã¹ãäœæããŠé©çšããŸãããïŒ
public interface IHitboxResponder { void collisionedWith(Collider collider); }
Hitboxã«ããããã£ãšããŠè¿œå ããŸã...
public class Hitbox : MonoBehaviour { ... private IHitboxResponder _responder = null; ... /* */ }
ãŸããå¿ èŠã«å¿ããŠã1人ã®åçè ã®ä»£ããã«é åã䜿çšã§ããŸãã
åçè ã䜿çšããŸãããïŒ
public void hitboxUpdate() { if (_state == ColliderState.Closed) { return; } Collider[] colliders = Physics.OverlapBox(position, boxSize, rotation, mask); for (int i = 0; i < colliders.Length; i++) { Collider aCollider = colliders[i]; responder?.collisionedWith(aCollider); } _state = colliders.Length > 0 ? ColliderState.Colliding : ColliderState.Open; }
ãïŒãæŒç®åã«æ £ããŠããªãå Žåã¯ã ãã¡ãããèªã¿ãã ããã
ãããïŒ ãããã
_responder
ããããã£ãã©ã®ããã«èšå®ããŸãã ã»ãã¿ãŒãè¿œå ããŠã次ã®æ®µèœã§æ€èšããŠã¿ãŸãããã
public void useResponder(IHitboxResponder responder) { _responder = responder; }
ãããããã¯ã¹ã¯ãè€æ°ã®ç°ãªãæ»æã«é©çšå¯èœã§ããã1ã€ã®æ»æã«çµã³ä»ããããã¹ãã§ã¯ãããŸãã
ãã®ã»ã¯ã·ã§ã³ã§ã¯ããšãã£ã¿ãŒã䜿çšããŠHitboxRespondersãèšå®ã§ããªãããšãéèŠã§ããçç±ã説æããŸãã
æåã«ããããã®ãåçè ãã«ã€ããŠè©±ããŸãããã Hitboxã®å®è£ ãéžæããã¢ãããŒãã§ã¯ãåçè ã¯HitboxãColliderãšç«¶åããå Žåã«äœããå®è¡ããå¿ èŠãããã¯ã©ã¹ãã€ãŸãIHitboxResponderãå®è£ ããã¯ã©ã¹ã§ãã ããšãã°ãæ»æã¹ã¯ãªããã䜿çšã§ããŸããããããããã®ã«ãã¡ãŒãžãäžãããã®ã§ãã
ç¹å®ã®æ»æã«é¢é£ä»ãããããç¹°ãè¿ã䜿çšã§ããããã«ããããããšãã£ã¿ãŒã§åçè ãå²ãåœãŠãŠãäœãåŸãããŸããããã®å Žã§åçè ãå€æŽã§ããããã«ããããã§ãã
çŽæãšåãæã®ã¢ãããŒã«ããã®2çš®é¡ã®æ»æããããããããã«ãããããã¢ãã¡ãŒã·ã§ã³ã®ãã¬ãŒã ããã¡ãŒãžã®çšåºŠãªã©ã瀺ãç¬èªã®ã¹ã¯ãªããããããšããŸãã ãããã®æ»æã¯äž¡æ¹ãšãåãæ足ã§å®è¡ãããããã1ã€ã®Hitboxã䜿çšããŸãããã
public class Attack: Monobehaviour, IHitboxResponder { ... public int damage; public Hitbox hitbox; ... public void attack() { hitbox.setResponder(this); // } void collisionedWith(Collider collider) { Hurtbox hurtbox = collider.GetComponent<Hurtbox>(); hurtbox?.getHitBy(damage); } }
ãããïŒ åäœäžã®ãããããã¯ã¹ãäœæããŸããã äžèšã®ã³ãŒããããããããã«ã
getHitBy(int damage)
ã¡ãœããã
getHitBy(int damage)
è¿œå ããŸããã æ¹åã§ãããã©ããèŠãŠã¿ãŸãããã
ããŒãããã¯ã¹ã®æ©èœåŒ·å
çæ³çã«ã¯ãããŒãããã¯ã¹ã«ã€ããŠã¯ããããããã¯ã¹ãšã»ãŒåããã€ã³ããå®è£ ããå¿ èŠããããŸãã Colliderã«ã¯å¿ èŠãªæ©èœããããããããã¯ç°¡åã§ãã ãŸããColliderã䜿çšããå¿ èŠããããŸããããããªããšãPhysics.Overlap ...ãããããå ±åããŸããã
ã³ãŒããæ§é åããæ¹æ³ã«ãããOnTriggerEnterã䜿çšããå¿ èŠã¯ãªããGetComponentã䜿çšããŠã¹ã¯ãªãããååŸããŸãã
ããã«ãããã«ã¹ã¿ãã€ãºæ§ãšæè»æ§ãåŸãããŸãã Hitboxãšåãæè»æ§ãæäŸããã«ã¯ããã®å Žã§ã³ã©ã€ããŒãè¿œå ããã³åé€ããå¿ èŠããããã«ã¹ã¿ãã€ãºã®ããã«ããã®ç¶æ ã«å¿ããŠã³ã©ã€ããŒã«è²ãæç»ã§ããŸãã
public class Hurtbox : MonoBehaviour { public Collider collider; private ColliderState _state = ColliderState.Open; public bool getHitBy(int damage) { // - } private void OnDrawGizmos() { // , // , } }
ãªã³ã¶ãã©ã€ã§ã³ã©ã€ããŒãè¿œå ããã³åé€ããããšã¯ããããããã¯ã¹ã»ã©ç°¡åã§ã¯ãããŸããã ãã®ã¿ã¹ã¯ãéæããããã®æºè¶³ã®ããæ¹æ³ãèŠã€ãããŸããã§ããã Hitboxã§è¡ã£ãããã«ãããã€ãã®ç°ãªãColliderãã¹ã¯ãªããã«è¿œå ããããŒã«å€ã䜿çšããŠç®çã®Colliderãéžæã§ããŸãã ããã§ã®åé¡ã¯ãå¿ èŠãªãã¹ãŠã®Colliderãã³ã³ããŒãã³ããšããŠè¿œå ããå¿ èŠãããããšã§ãããã®çµæããšãã£ã¿ãŒãšãªããžã§ã¯ãã«åŒ·ãèŠèŠçãã€ãºãçºçããŸãã
å¥ã®ã¢ãããŒãã¯ãã³ãŒããä»ããŠã³ã³ããŒãã³ããè¿œå ããã³åé€ããããšã§ããããã®ãããªãœãªã¥ãŒã·ã§ã³ã¯å€ãã®äžèŠãªãŽããè¿œå ããããããããã»ã©æ£ç¢ºã§ã¯ãããŸããã
HurtboxãColliderããç¶æ¿ããã®ãçæ³çã§ããããã©ãŒã ã®ããžãã¯ã¯ãã¹ãŠå éšã§ãããçŸåšäœ¿çšããŠãããã©ãŒã ã®ã¿ãã¬ã³ããªã³ã°ã§ããŸãããããã®ã·ã¹ãã ãæãã©ããã«åäœãããããšãã§ããŸããã§ããã
次ã¯ïŒ
æçš¿ã§æ éã«æäœãç¹°ãè¿ããå ŽåãUnityã«ãããããã¯ã¹ãããŒãããã¯ã¹ãããã·ã¥ããã¯ã¹ãå®è£ ãããŸããã ããããããã«éèŠãªããšã¯ããããã®æœè±¡åãããã£ãããããããã®äžã«äœããæ§ç¯ãããšäœæ¥ãå€§å¹ ã«ç°¡çŽ åãããããšã§ãã
ããªãã®ã¹ã¯ãªããã€ã³ã¹ãã¯ã¿ãŒã¯æããä»æãããèŠããŸãããå¿é ããªãã§ãã ããã次ã®æçš¿ã§ãããã«ããŒããŸãïŒ
ãããåãããšãã§ããŸã...
ãã®ãããªãã®ã«ïŒ