Unity¤Ë´ØÏ¢¤¹¤ëµ­»ö¤Ç¤¹

BOX¤ÎÂѵ×ÎϤò»ë³ÐŪ¤Ëʬ¤«¤ë¤è¤¦¤Ë¤¹¤ë¤¿¤á¤Ë¡¢ÃƤ¬Åö¤¿¤ë¤´¤È¤Ë¿§¤òÊѤ¨¤ë¤è¤¦¤Ë¤·¤Þ¤¹¡£

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

¡¡°Ê¾å¤Ç½àÈ÷¤Ï´°Î»¤Ç¤¹¡£¥²¡¼¥à¤ò¼Â¹Ô¤·¤Æ¤ß¤Æ¤¯¤À¤µ¤¤¡£¥¹¥¿¡¼¥È»þ¤Ë³Æ¥Ö¥í¥Ã¥¯¤Î¿§¤¬¼«Ê¬¤ÎÂѵ×ÎϤÎÃͤ˹ç¤ï¤»¤ÆÊѲ½¤·¡¢¤½¤Î¸å¡¢¥Ü¡¼¥ë¤¬Åö¤¿¤ëÅ٤˿§¤¬ÊѲ½¤¹¤ì¤ÐÀ®¸ù¤Ç¤¹¡£

¥³¥á¥ó¥È¤ò¤«¤¯


¡Öhttp://¡×¤ò´Þ¤àÅê¹Æ¤Ï¶Ø»ß¤µ¤ì¤Æ¤¤¤Þ¤¹¡£

ÍøÍѵ¬Ìó¤ò¤´³Îǧ¤Î¤¦¤¨¤´µ­Æþ²¼¤µ¤¤

Menu


´ðÁÃ

µ»½Ñ/Ãμ±(¼ÂÁõÎã)

3D¥¢¥¯¥·¥ç¥ó¥²¡¼¥à

2D¤ª¤Ï¤¸¤­¥²¡¼¥à(ȯŸÊÔ)

2D¶¯À©²£¥¹¥¯¥í¡¼¥ë¥¢¥¯¥·¥ç¥ó(ȯŸÊÔ)

2D¥¿¥Ã¥×¥·¥å¡¼¥Æ¥£¥ó¥°(³ÈÄ¥ÊÔ)

¥ì¡¼¥¹¥²¡¼¥à(È´¿è)

2DÊüÃÖ¥²¡¼¥à(ȯŸÊÔ)

3D¥ì¡¼¥ë¥¬¥ó¥·¥å¡¼¥Æ¥£¥ó¥°(±þÍÑÊÔ)

3Dæ½Ð¥²¡¼¥à(È´¿è)

2D¥ê¥¢¥ë¥¿¥¤¥à¥¹¥È¥é¥Æ¥¸¡¼

3D¥¿¥Ã¥×¥¢¥¯¥·¥ç¥ó(NavMeshAgent »ÈÍÑ)

2D¥È¥Ã¥×¥Ó¥å¡¼¥¢¥¯¥·¥ç¥ó(¥«¥¨¥ë¤Î°Ù¤Ë¡Á¡¢¥Ü¥³¥¹¥«¥¦¥©¡¼¥ºÉ÷)

VideoPlayer ¥¤¥Ù¥ó¥ÈϢư¤Î¼ÂÁõÎã

VideoPlayer ¥ê¥¹¥ÈÆ⤫¤é¥à¡¼¥Ó¡¼ºÆÀ¸¤Î¼ÂÁõÎã(ȯŸ)

AR ²èÁüÉÕ¤­¥ª¥Ö¥¸¥§¥¯¥ÈÀ¸À®¤Î¼ÂÁõÎã

AR ¥ê¥¹¥ÈÆ⤫¤éÀ¸À®¤Î¼ÂÁõÎã(ȯŸ)

2D¥È¥Ã¥×¥Ó¥å¡¼¥¢¥¯¥·¥ç¥ó(¥µ¥Ð¥¤¥Ð¡¼É÷)

private



¤³¤Î¥µ¥¤¥ÈÆâ¤ÎºîÉʤϥæ¥Ë¥Æ¥£¤Á¤ã¤ó¥é¥¤¥»¥ó¥¹¾ò¹à¤Î¸µ¤ËÄ󶡤µ¤ì¤Æ¤¤¤Þ¤¹¡£

´ÉÍý¿Í/Éû´ÉÍý¿Í¤Î¤ßÊÔ½¸¤Ç¤­¤Þ¤¹