ºÇ½ª¹¹¿·¡§ orika_ex_miyako 2020ǯ08·î23Æü(Æü) 03:39:20ÍúÎò
https://gyazo.com/cdaaa65ee46cd48b59e97d170a8fbc05
https://gyazo.com/176e966356e8c57db0dcdc088787535c
¿·¤·¤¤¥¹¥¯¥ê¥×¥È¡ÖChangeColorOnHit¡×¤òºîÀ®¤·¤Þ¤¹¡£
Assetes -> Script ¥Õ¥©¥ë¥ÀÆâ¤Ç±¦¥¯¥ê¥Ã¥¯¤ò¤·¤Æ¡¢create -> C# script¤òÁªÂò¤·¤Þ¤¹¡£
¥½¡¼¥¹¥³¡¼¥É¤Ï²¼µ¤ÎÄ̤ê¤Ç¤¹¡£
using UnityEngine; public class ChangeColorOnHit : MonoBehaviour { //Hierarchy¤«¤é¥«¥é¡¼¤ò¥»¥Ã¥È¤¹¤ë¤¿¤á¤ËÇÛÎó¤òÍÑ°Õ¤¹¤ë public Color[] blockColors; /// <summary> /// ¥Ö¥í¥Ã¥¯¤Î¿§¤òÊѹ¹¤¹¤ë¥á¥½¥Ã¥É¡£ÇÛÎó¤ÎSize¤Ç»ØÄꤷ¤¿¿ô¤¬¡¢¿§¤ÎÊѤï¤ë½çÈÖ¤ÈÆâÍƤˤʤë /// </summary> /// <param name="index">¥Ö¥í¥Ã¥¯¤Î»Ä¤ê¤ÎHP</param> /// <returns></returns> public Color ChangeMaterialColor(int index) { // index¤Ï [Block¤Îhp - 1]¤ÎÃͤòÍøÍѤ¹¤ë index--; // ¥Ö¥í¥Ã¥¯¤Î¿§¤òÇÛÎó¤ËÅÐÏ¿¤µ¤ì¤Æ¤¤¤ëIndex¤Î¿§¤ËÊѹ¹¤¹¤ë return blockColors[index]; } }
¡¡¾åµ¤ÎChangeColorOnHit¥¹¥¯¥ê¥×¥È¤òMain¥·¡¼¥ó¤ÎMaster¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Ë¥¢¥¿¥Ã¥Á¤·¤Þ¤¹¡£
Master¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Î¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤ò³Îǧ¤·¤Æ¡¢ChangeColorOnHit¥¹¥¯¥ê¥×¥È¤¬¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤ë¤«³Îǧ¤·¤Þ¤¹¡£
¡¡Ìµ»ö¤Ë¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤ë¤È¡¢BlockColors ¤È¤¤¤¦Color·¿¤ÎÇÛÎó¤Î¥¢¥µ¥¤¥ó¾ðÊó¤¬É½¼¨¤µ¤ì¤Æ¤¤¤Þ¤¹¤Î¤Ç¡¢¤³¤Á¤é¤ÎSize¤ò¡¢Block¤Î»ý¤ÄÂѵ×ÎϤÎÆâ¤ÇºÇ¤â¹â¤¤ÃͤÈƱ¤¸¿ô»ú¤ËÀßÄꤷ¤Þ¤¹¡£
Î㤨¤ÐBox¤ÎHp¤¬1¡¢HardBox¤ÎHp¤¬3¤Î¾ì¹ç¤Ë¤Ï¡¢¤³¤ÎSize¤ò3¤ËÀßÄꤷ¤Þ¤¹¡£¤½¤¦¤¹¤ë¤ÈElement¾ðÊó¤¬Äɲ䵤ì¤Þ¤¹¡£
¡¡Size¤ò»ØÄꤹ¤ë¤È²èÁü¤Î¤è¤¦¤Ë¿§¤òÀßÄꤹ¤ë¹àÌܤ¬É½¼¨¤µ¤ì¤Þ¤¹¡£¡Ê½é´üÃͤϤ¹¤Ù¤Æ¹õ¿§¡Ë¡¡³ÆElement¤Ë¤Ï¹¥¤¤Ê¿§¤òÅÐÏ¿¤·¤Æ¤¯¤À¤µ¤¤¡£
¤³¤Î½èÍý¤Ç¤Ï¡¢Element¤ÎÃͤȳÆBlock¤ÎInitHp(Âѵ×ÎÏ)¤ÎÃͤȤò¥ê¥ó¥¯¤µ¤»¤Æ¡¢¥Ö¥í¥Ã¥¯¤Ë¥Ü¡¼¥ë¤¬Åö¤¿¤ëÅ٤˿§¤¬ÊѲ½¤¹¤ë¤è¤¦¤Ë¤·¤Æ¤¤¤Þ¤¹¡£
¡¡
¡¡
³¤¤¤Æ¡¢Destroyer.cs¤Î²¼µ¤Î¾ì½ê¤ËÊѹ¹¤ò²Ã¤¨¤Þ¤¹¡£
public class Destroyer : MonoBehaviour { ¡¡¡¡(Ãæά) ///* Àë¸À¥Õ¥£¡¼¥ë¥É¤Ë2¤ÄÊÑ¿ô¤òÄɲà */// public Renderer rend; ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡// ¥Ö¥í¥Ã¥¯¤Î¿§¤ÎÊѹ¹ÍÑ private ChangeColorOnHit changeColorOnHit; // ¿§¤òÊѹ¹¤¹¤ë¥¯¥é¥¹¤Î¾ðÊó¤òÂåÆþ¤¹¤ëÊÑ¿ô void Start() { //Box¤ÎÂѵ×ÎϤò½é´ü²½(¸½ºßÃÍ¡áºÇÂçÃͤˤ¹¤ë) this.currentHp = initHp; ///* ¤³¤³¤«¤éÄɲà */// // masterObj¤Î»ý¤Ã¤Æ¤¤¤ëChangeColorOnHit¥³¥ó¥Ý¡¼¥Í¥ó¥È¤ò¼èÆÀ¤·¤ÆÊÑ¿ô¤ËÂåÆþ¤·¤Æ»²¾ÈÍøÍѤǤ¤ë¤è¤¦¤Ë¤¹¤ë changeColorOnHit = masterObj.GetComponent<ChangeColorOnHit>(); // ¥Ö¥í¥Ã¥¯¤Î¿§¤ò»Ä¤ê¤ÎÂѵ×ÎϤ˹ç¤ï¤»¤ÆÊѹ¹¤¹¤ë(ChangeColorOnHit¥¯¥é¥¹¤ÎChangeMaterialColor¥á¥½¥Ã¥É¤ÎÌá¤êÃͤȤ·¤ÆColor¤ò¤â¤é¤¦) rend.material.color = changeColorOnHit.ChangeMaterialColor(currentHp); ///* ¤³¤³¤Þ¤Ç */// } private void OnCollisionEnter(Collision collision) { (Ãæά) // ¤Þ¤À²õ¤ì¤Ê¤¤¾ì¹ç else { // ¥Ü¡¼¥ë¤òÄ·¤ÍÊÖ¤¹SE¤òºÆÀ¸ AudioSource.PlayClipAtPoint(hitSE, transform.position); // ¥Ö¥í¥Ã¥¯¤Î¿§¤òÊѹ¹ // <= Äɲà ChangeBlockColor(); // <= Äɲà } } ///* ¤³¤³¤«¤é¥á¥½¥Ã¥É¤ò£±¤ÄÄɲà */// ¡¡ /// <summary> /// ¥Ö¥í¥Ã¥¯¤Î¿§¤ò»Ä¤ê¤ÎÂѵ×ÎϤ˹ç¤ï¤»¤ÆÊѹ¹¤¹¤ë /// ChangeColorOnHit¥¯¥é¥¹¤ÎChangeMaterialColor¥á¥½¥Ã¥É¤Ë¸½ºß¤ÎÂѵ×ÎϤÎÃͤòÅϤ·¡¢Ìá¤êÃͤȤ·¤ÆColor(¿§¤Î¾ðÊó)¤ò¤â¤é¤¦ /// </summary> private void ChangeBlockColor() { rend.material.color = changeColorOnHit.ChangeMaterialColor(currentHp); } ///* ¤³¤³¤Þ¤Ç */// }
¡¡¥¹¥¯¥ê¥×¥È¤ò½¤Àµ¤·¤Þ¤·¤¿¤é¡¢Box¥×¥ì¥Õ¥¡¥Ö¤ÈHardBox¥×¥ì¥Õ¥¡¥Ö¤Î¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤ò³Îǧ¤·¤Þ¤¹¡£¡Ê¾¤Ë¤âBox¤Î¥×¥ì¥Õ¥¡¥Ö¤òºîÀ®¤·¤Æ¤¤¤ë¾ì¹ç¤Ë¤Ï¤½¤ì¤é¤â¡Ë
Rend ¤È¤¤¤¦ÊÑ¿ô¤¬¥¢¥µ¥¤¥ó¾ðÊó¤È¤·¤ÆÄɲ䵤ì¤Æ¤¤¤Þ¤¹¤Î¤Ç¡¢¤½¤Á¤é¤ËBox¥×¥ì¥Õ¥¡¥Ö¼«¿È¤Î»ý¤Ä MeshRenderer ¥³¥ó¥Ý¡¼¥Í¥ó¥È¤ò¥¢¥µ¥¤¥ó¤·¤Þ¤¹¡£
¤³¤ì¤ò³ÆBox¤Î¥×¥ì¥Õ¥¡¥Ö¤ÇƱ¤¸¤è¤¦¤ËÀßÄꤷ¤Æ¤¯¤À¤µ¤¤¡£
https://gyazo.com/b85f8daf6267f3343f7c804cc8b351a6
¡¡°Ê¾å¤Ç½àÈ÷¤Ï´°Î»¤Ç¤¹¡£¥²¡¼¥à¤ò¼Â¹Ô¤·¤Æ¤ß¤Æ¤¯¤À¤µ¤¤¡£¥¹¥¿¡¼¥È»þ¤Ë³Æ¥Ö¥í¥Ã¥¯¤Î¿§¤¬¼«Ê¬¤ÎÂѵ×ÎϤÎÃͤ˹ç¤ï¤»¤ÆÊѲ½¤·¡¢¤½¤Î¸å¡¢¥Ü¡¼¥ë¤¬Åö¤¿¤ëÅ٤˿§¤¬ÊѲ½¤¹¤ì¤ÐÀ®¸ù¤Ç¤¹¡£
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- Áí¹ç
¥³¥á¥ó¥È¤ò¤«¤¯