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

¡¡°ÜÆ°ÍѤΥ­¡¼ÆþÎϤËÌð°õ¥­¡¼¤òÍøÍѤ¹¤ë¾ì¹ç¡¢Input.GetAxis ¥á¥½¥Ã¥É¤Î³èÍѤ¬°ìÈÌŪ¤Ç¤¹¤¬¡¢KeyCode ¤Ë¹ç¤ï¤»¤Æ if ʸ¤ä switch ʸ¤òÍøÍѤ·¤¿Ê¬´ô½èÍý¤â¼ÂÁõ²Äǽ¤Ç¤¹¡£
¤¿¤À¤·Ê¬´ô¤Î¿ô¤¬Â¿¤¯¤Ê¤Ã¤Æ¤·¤Þ¤¦¾å¤Ë¡¢¥­¡¼¤ÎÄɲääÊѹ¹¤ÎÅÔÅÙ¡¢Ê¬´ô¤ò½ñ¤­Ä¾¤¹É¬Íפ¬¤¢¤ë¤¿¤á¡¢Á´ÂÎŪ¤Ë¾éĹŪ¤Ê¥½¡¼¥¹¥³¡¼¥É¤Ë¤Ê¤ê¡¢Êݼ餬º¤Æñ¤Ë¤Ê¤ê¤Þ¤¹¡£

¡¡¤½¤¦¤¤¤Ã¤¿½èÍý¤ò·Ú¸º¤¹¤ëÊýË¡¤È¤·¤Æ»öÁ°¤Ë Dictionary ¤Ê¤É¤òÍøÍѤ·¤Æ¡¢KeyCode ¤È¤½¤ì¤Ëɳ¤Å¤¯½èÍý¤òÅÐÏ¿¤·¤Æ¤ª¤¯¥¢¥×¥í¡¼¥Á¤ÎÊýË¡¤ò¾Ò²ð¤·¤Þ¤¹¡£
¤³¤¦¤¤¤Ã¤¿É³ÉÕ¤±¤Î¤³¤È¤ò¥Þ¥Ã¥Ô¥ó¥°¤È¸Æ¤Ó¤Þ¤¹¡£



¡¡¤³¤³¤Ç¤Ï¼ÂÁõÎã¤È¤·¤Æ¡¢£³£Ä¶õ´ÖÆâ¤Î°ÜÆ°¤È²óž¤Ë¤Ä¤¤¤Æ KeyCode ¤ò¥Þ¥Ã¥Ô¥ó¥°¤·¤Æ¤¤¤Þ¤¹¡£
¼ÂºÝ¤Î°ÜÆ°¤È²óžÉôʬ¤Ë¤Ä¤¤¤Æ¤Ï¡¢LINQ ¤òÍøÍѤ·¤Æ¼ÂÁõ¤·¤Æ¤¤¤Þ¤¹¡£



£±¡¥ÊªÍý±é»»¤òÍøÍѤ·¤Ê¤¤¥±¡¼¥¹


¡¡Transform ¤òÍøÍѤ·¤¿°ÜÆ°¤Î¥±¡¼¥¹¤Ç¤¹¡£


using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class KeyCodeMappingTransformController : MonoBehaviour
{
    [SerializeField] private float moveSpeed = 1.0f;  // °Üư®ÅÙ

    private Vector3 prevPosition;   ¡¡¡¡¡¡¡¡¡¡¡¡¡¡    // Á°²ó¤ÎºÂɸ
¡¡¡¡public Vector3 PrevPosition => prevPosition;      // ¥×¥í¥Ñ¥Æ¥£


    // KeyCode ¤È°ÜÆ°Êý¸þ¤ò¥Þ¥Ã¥Ô¥ó¥°
    private Dictionary<KeyCode, Vector3> directionMappings = new ()
    {
        { KeyCode.UpArrow, Vector3.up },
        { KeyCode.DownArrow, Vector3.down },
        { KeyCode.LeftArrow, Vector3.left },
        { KeyCode.RightArrow, Vector3.right }
    };

    // KeyCode ¤È²óžÊý¸þ¤ò¥Þ¥Ã¥Ô¥ó¥°
    private Dictionary<KeyCode, Quaternion> rotationMappings = new ()
    {
        { KeyCode.UpArrow, Quaternion.Euler(0, 0, 0) },
        { KeyCode.DownArrow, Quaternion.Euler(0, 0, 180) },
        { KeyCode.LeftArrow, Quaternion.Euler(0, 0, 90) },
        { KeyCode.RightArrow, Quaternion.Euler(0, 0, 270) }
    };


    void Update()
    {
        Move();
    }

    /// <summary>
    /// °ÜÆ°
    /// </summary>
    private void Move()
    {
        Vector3 position = transform.position;

	// °ÜÆ°Á°¤Î°ÌÃÖ¤òÊݸ
        prevPosition = position;

        // ¥­¡¼ÆþÎϤˤè¤ë°ÜÆ°Êý¸þ¤Î³Îǧ
        KeyCode keyCode = directionMappings
            .Where(kvp => Input.GetKey(kvp.Key))
            .Select(kvp => kvp.Key)
            .SingleOrDefault();

        if (keyCode != KeyCode.None)
        {
¡¡¡¡¡¡¡¡¡¡¡¡// °ÜÆ°
            position += directionMappings[keyCode] * moveSpeed;
            transform.position = position;

¡¡¡¡¡¡¡¡¡¡¡¡// ²óž
            Turn(keyCode);
        }
    }

    /// <summary>
    /// ²óž
    /// </summary>
    /// <param name="keyCode"></param>
    private void Turn(KeyCode keyCode)
    {
        if (rotationMappings.TryGetValue(keyCode, out Quaternion rotation))
        {
            // ²óž
            transform.rotation = rotation;
        }
    }
}


¡ã¥Þ¥Ã¥Ô¥ó¥°¤È LINQ ¤òÍøÍѤ·¤¿½èÍý¡ä


¡¡²¼µ­¤Î½èÍý¤ÎÉôʬ¤Ë¤ª¤¤¤Æ¡¢KeyCode ¤È¤½¤ì¤ËÂбþ¤¹¤ëÊý¸þ¡¢¤ª¤è¤Ó²óž³ÑÅÙ¤òɳ¤Å¤±¤·¤Æ¤¤¤Þ¤¹¡£

    // KeyCode ¤È°ÜÆ°Êý¸þ¤ò¥Þ¥Ã¥Ô¥ó¥°
    private Dictionary<KeyCode, Vector3> directionMappings = new ()
    {
        { KeyCode.UpArrow, Vector3.up },
        { KeyCode.DownArrow, Vector3.down },
        { KeyCode.LeftArrow, Vector3.left },
        { KeyCode.RightArrow, Vector3.right }
    };

    // KeyCode ¤È²óžÊý¸þ¤ò¥Þ¥Ã¥Ô¥ó¥°
    private Dictionary<KeyCode, Quaternion> rotationMappings = new ()
    {
        { KeyCode.UpArrow, Quaternion.Euler(0, 0, 0) },
        { KeyCode.DownArrow, Quaternion.Euler(0, 0, 180) },
        { KeyCode.LeftArrow, Quaternion.Euler(0, 0, 90) },
        { KeyCode.RightArrow, Quaternion.Euler(0, 0, 270) }
    };

¡¡Dictionary ¤Ç¤Ï Key ¤ò»ØÄꤹ¤ë¤³¤È¤Ç Value ¤ò»²¾È¤¹¤ë¤³¤È¤¬²Äǽ¤Ç¤¹¡£
º£²ó¤Î¾ì¹ç¤Ç¤Ï¡¢Key ¤ò KeyCode¡¢Value ¤ò Vector3 ¤È¤·¤Æ£±¥»¥Ã¥È¤ËÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£
¤½¤ì¤ò³ÆÊý¸þʬÍÑ°Õ¤·¤Æ¤ª¤¯¤³¤È¤Ë¤è¤ê¡¢Ìð°õ¥­¡¼¤Î¥­¡¼ÆþÎϤÈÊý¸þ¤È¤ò£±ÂУ±¤Çɳ¤Å¤±¤Æ¤¤¤Þ¤¹¡£

¡¡²óž¤Î¾ì¹ç¤âƱÍͤǤ¹¡£



¡¡¤³¤Î¤è¤¦¤Ë¤·¤Æ»öÁ°¤Ë¾ðÊó¤ÎɳÉÕ¤±¤·¤Æ¤ª¤¯¤³¤È¤ò¥Þ¥Ã¥Ô¥ó¥°¤È¤¤¤¤¤Þ¤¹¡£

¡¡¼ÂºÝ¤Ë¤Ï°Ê²¼¤ÎÉôʬ¤ÇÍøÍѤµ¤ì¤Æ¤¤¤Þ¤¹¡£
¤³¤Á¤é¤Î½èÍý¤Ë¤Ï LINQ ¤òÍøÍѤ·¡¢¾Êάµ­Ë¡¤Ë¤è¤Ã¤Æ½èÍý¤ò¼ÂÁõ¤·¤Æ¤¤¤Þ¤¹¡£

        // ¥­¡¼ÆþÎϤˤè¤ë°ÜÆ°Êý¸þ¤Î³Îǧ
        KeyCode keyCode = directionMappings
            .Where(kvp => Input.GetKey(kvp.Key))
            .Select(kvp => kvp.Key)
            .SingleOrDefault();

¡¡Where ¥á¥½¥Ã¥É¤Î¾ò·ï¼°¤È¤·¤Æ Input.GetKey(kvp.Key) ¥á¥½¥Ã¥É¤ò¼Â¹Ô¤·¤Æ¤¤¤Þ¤¹¡£
¤³¤ÎÉôʬ¤¬ Dictionary ¤È¤·¤Æ¥Þ¥Ã¥Ô¥ó¥°¤µ¤ì¤Æ¤¤¤ë KeyCode ¤¬»²¾È¤µ¤ì¤Æ¤¤¤Þ¤¹¡£

¡¡¥Þ¥Ã¥Ô¥ó¥°ºÑ¤Î¤¤¤º¤ì¤«¤Î KeyCode ¤Ë³ºÅö¤·¤¿¾ì¹ç¡¢ Select ¥á¥½¥Ã¥É¤òÍøÍѤ·¤Æ KeyCode ¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë Dicitionary ¤Î Key ¤ò¼èÆÀ¤·¤Þ¤¹¡£
ºÇ¸å¤Ë SingleOrDefault ¥á¥½¥Ã¥É¤òÍøÍѤ·¤Æ¡¢¼èÆÀ¤·¤¿¾ðÊó¤ò£±¤Ä¼è¤ê½Ð¤·¡¢ºÇ½é¤Ë = ¤Îº¸ÊÕ¤ËÍÑ°Õ¤·¤¿ keyCode ÊÑ¿ô¤ËÂåÆþ¤¹¤ë¤³¤È¤Ç¡¢º£²ó¥­¡¼ÆþÎϤµ¤ì¤¿ KeyCode ¤òÆÃÄꤷ¤Æ¤¤¤Þ¤¹¡£



¡¡¤½¤Î¸å¡¢¤½¤Î²¼¤Ë³¤¯½èÍý¤Ç KeyCode ¤ò¥Ï¥ó¥É¥ê¥ó¥°¤·¡¢
¥Þ¥Ã¥Ô¥ó¥° ¤µ¤ì¤Æ¤¤¤ë KeyCode ¤ò¼èÆÀ¤Ç¤­¤Æ¤¤¤ë¾ì¹ç¤Î¤ß¡¢°ÜÆ°¤Î½èÍý¤È²óž¤Î½èÍý¤Ë¤Ä¤Ê¤¬¤ë¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£

¡¡¡¡¡¡¡¡if (keyCode != KeyCode.None)
        {
¡¡¡¡¡¡¡¡¡¡¡¡// °ÜÆ°
            position += directionMappings[keyCode] * moveSpeed;
            transform.position = position;

¡¡¡¡¡¡¡¡¡¡¡¡// ²óž
            Turn(keyCode);
        }



¡¡°ÜÆ°¤Î½èÍý¤Ë¤Ï Dictionary ¤Î Value ¤Î¾ðÊó¤òÍøÍѤ·¤Æ¤¤¤Þ¤¹¡£

// °ÜÆ°
position += directionMappings[keyCode] * moveSpeed;

¡¡directionMappings[keyCode] ¤Î¤è¤¦¤Ë Dictionary ¤ò Key ¤Ç»ØÄꤹ¤ë¤³¤È¤Ë¤è¤ê¡¢
¤½¤Î Key ¤È¥»¥Ã¥È¤Ë¤Ê¤Ã¤Æ¤¤¤ë Value ¤ò¼è¤ê½Ð¤¹¤³¤È¤¬²Äǽ¤Ç¤¹¡£

¡¡²¾¤Ë Key ¤¬ KeyCode.UpArrow ¤Ç¤¢¤ë¾ì¹ç¡¢ Vector3.up ¤ÎÃͤ¬ Value ¤È¤·¤Æ¼è¤ê½Ð¤µ¤ì¡¢
Àè¤Û¤É¤Î½èÍý¤ËŬÍѤµ¤ì¤Æ¤¤¤Þ¤¹¡£

¡¡Å¬ÍѤ·¤Æ¼¡¤Î¤è¤¦¤ËÆɤßÊѤ¨¤Þ¤¹¡£


// °ÜÆ°(KeyCode.UpArrow ¤Î¤È¤­)
position += Vector3.up * moveSpeed;

¡¡¼ÂºÝ¤Ë¥×¥í¥°¥é¥à¤Ï¡¢¤³¤Î¤è¤¦¤ÊÃͤȤ·¤Æ½èÍý¤·¤Æ¤¤¤Þ¤¹¡£

¡¡ÊÑ¿ô¤¬½Ð¤Æ¤­¤¿¤È¤­¤Ë¤Ï¡¢¾ï¤Ë¤½¤ÎÃͤò¸«¤Æ¡¢Ç¾Æâ¤ÇÃÖ¤­´¹¤¨¤Æ¤¤¤¯¤è¤¦¤Ë¿´¤¬¤±¤Æ¤¤¤­¤Þ¤·¤ç¤¦¡£


¡ãLinq ¤Îµ¡Ç½¡¡¡ÁWhere ¥á¥½¥Ã¥É¡¢elect ¥á¥½¥Ã¥É¡¢SingleOrDefault ¥á¥½¥Ã¥É¡Á¡ä


¡¡Linq¡Ê¥ê¥ó¥¯¡Ë¤È¤Ï¡¢¥³¥ì¥¯¥·¥ç¥ó(Dictionary ¤ä List ¤Ê¤É)¤ÎÍ×ÁǤòÁàºî¤·¤Æ¡¢¸¡º÷¤·¤¿¤ê½¸·×¤¹¤ë½èÍý¤ò´Ê·é¤Ëµ­½Ò¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¥é¥¤¥Ö¥é¥ê(Ê£¿ô¤Îµ¡Ç½¤ò¤Þ¤È¤á¤¿¤â¤Î)¤Ç¤¹¡£

¡¡Linq¤ò»ÈÍѤ¹¤ë¤¿¤á¤Ë¤Ïusing ¤ÎÀë¸À¤¬É¬Íפˤʤê¤Þ¤¹¡£º£²óºîÀ®¤·¤¿¥á¥½¥Ã¥ÉÆâ¤ËÅо줷¤Æ¤¤¤ë½èÍý¤Ç¤¹¤Î¤Ç¡¢Éü½¬¤·¤ÆÆɤ߲ò¤±¤ë¤è¤¦¤Ë¤·¤Æ¤¤¤­¤Þ¤·¤ç¤¦¡£

using System.Linq;



¡¡Linq¤òµ­½Ò¤¹¤ëºÝ¤Ë¤Ï¥é¥à¥À¼°¤Îµ­½Ò¤òÍѤ¤¤Þ¤¹¡£¥é¥à¥À¼°¤Ë¤Ä¤¤¤Æ¤Ï¤³¤Á¤é¤ò¤´³Îǧ¤¯¤À¤µ¤¤¡£
SamuraiBlogÍÍ
¡ÚC#ÆþÌç¡ÛLINQ¤Î»È¤¤ÊýÁí¤Þ¤È¤á(Select¡¢Where¡¢GroupBy¤Ê¤É)
https://www.sejuku.net/blog/56519

¡¡Linq¤Ë¤Ï¿¤¯¤Îµ¡Ç½¤¬¤¢¤ê¤Þ¤¹¤¬º£²óÍøÍѤ·¤Æ¤¤¤ëµ¡Ç½¤Ë¤Ä¤¤¤Æ¤Þ¤È¤á¤Æ¤ª¤­¤Þ¤¹¡£
¤½¤Î¤Û¤«¤Îµ¡Ç½¤Ë¤Ä¤¤¤Æ¤Ïµ­»ö¤¬¤¿¤¯¤µ¤ó¤¢¤ê¤Þ¤¹¤¬¡¢¤³¤Á¤é¤Î¥µ¥¤¥È¤â»²¹Í¤Ë¤Ê¤ê¤Þ¤¹¡£
ÃÏÊ¿Àþ¤Ë¹Ô¤¯ÍÍ
LINQ¤Î³ÈÄ¥¥á¥½¥Ã¥É°ìÍ÷¤È¡¢¤Û¤ÜÁ´Éô¤Î¥µ¥ó¥×¥ë¤òºî¤Ã¤Æ¤ß¤Þ¤·¤¿¡£
https://yujisoftware.hatenablog.com/entry/20111031...


£±¡¥Where ¥á¥½¥Ã¥É

¡¡ÇÛÎó¤ä List ¤Ê¤É¤Î¥³¥ì¥¯¥·¥ç¥ó¤ËÂФ·¤Æ¥Õ¥£¥ë¥¿¥ê¥ó¥°½èÍý¤ò¹Ô¤¤¡¢»ØÄꤷ¤¿¾ò·ï¤òËþ¤¿¤¹Í×ÁǤΤߤò¼èÆÀ¤¹¤ë¤³¤È¤¬¤Ç¤­¤ëµ¡Ç½¤Ç¤¹¡£
°ú¿ô¤Ë¤Ï¥Õ¥£¥ë¥¿¥ê¥ó¥°¤ò¹Ô¤¦¾ò·ï¼°¤ò»ØÄꤷ¤Þ¤¹¡£


        // ¥­¡¼ÆþÎϤˤè¤ë°ÜÆ°Êý¸þ¤Î³Îǧ
        KeyCode keyCode = directionMappings
            .Where(kvp => Input.GetKey(kvp.Key))
            .Select(kvp => kvp.Key)
            .SingleOrDefault();

¡¡º£²ó¤Î¼ÂÁõÎã¤Ç¤Ï Where ¥á¥½¥Ã¥É¤Î¾ò·ï¼°¤Ë kvp => Input.GetKey(kvp.Key) ¤ò»ØÄꤷ¤Æ¤¤¤Þ¤¹¡£
¤³¤ì¤Ï Dicitionary ·¿¤Î¥³¥ì¥¯¥·¥ç¥ó¤Ç¤¢¤ë directionMappings ÊÑ¿ô¤ÎÍ×ÁǤò£±¤Ä¤º¤Ä¥Õ¥£¥ë¥¿¥ê¥ó¥°¤·¡¢¾åµ­¤Î¾ò·ï¤Ë¹ç¤¦Í×ÁǤΤߤò¼èÆÀ¤·¤Æ¤¤¤Þ¤¹¡£
kvp ÊÑ¿ô¤Ë¤Ï directionMappings ÊÑ¿ô¤ÎÍ×ÁǤǤ¢¤ë Dicitionary<KeyCode, Vector3> ·¿¤Î¾ðÊ󤬣±¤Ä¤º¤Ä½çÈÖ¤ËÂåÆþ¤µ¤ì¡¢
Input.GetKey(kvp.Key) ¥á¥½¥Ã¥É¤Î¼Â¹Ô·ë²Ì¤Ç¤¢¤ëÌá¤êÃͤξðÊó¤È¡¢Dicitionary<KeyCode, Vector3> ·¿ ¤Î Key ¤Ç¤¢¤ë KeyCode ¤È¤ò¾È¹ç¤·¤Æ¤¤¤Þ¤¹¡£

¡¡¤³¤Î½èÍý¤Ï IEnumerable<T> ¤ÎÌá¤êÃͤò»ý¤Ä¤¿¤á¡¢¥á¥½¥Ã¥É¥Á¥§¡¼¥ó¤ò¹Ô¤¦¤³¤È¤Ç¡¢¤³¤Î½èÍý¤Î·ë²Ì¤ò¼¡¤Î½èÍý¤Ë¤Ä¤Ê¤²¤Æ¤¤¤¯¤³¤È¤¬²Äǽ¤Ç¤¹¡£
º£²ó¤Ç¤¢¤ì¤Ð¡¢¥Õ¥£¥ë¥¿¥ê¥ó¥°¤·¤Æ¾È¹ç¤Î¾ò·ï¤òËþ¤¿¤·¤¿ Dicitionary<KeyCode, Vector3> ·¿¤Î¾ðÊó¤ò¼èÆÀ¤¹¤ë¤³¤È¤¬½ÐÍè¤Þ¤¹¡£


»²¹Í¥µ¥¤¥È
MicroSoft
Enumerable.Where ¥á¥½¥Ã¥É
https://docs.microsoft.com/ja-jp/dotnet/api/system...
±¢´³¤·Ãæ¤Î¥²¡¼¥à³«È¯¥á¥â
¡ÚC#,LINQ¡ÛWhere¡ÁÇÛÎó¤ä¥ê¥¹¥È¤ò»ØÄꤷ¤¿¾ò·ï¤Ç¥Õ¥£¥ë¥¿¥ê¥ó¥°¤·¤¿¤¤¤È¤­¡Á
https://www.urablog.xyz/entry/2018/06/27/070000
.NET Column ÍÍ
¡ÚLINQ¤Î¥á¥½¥Ã¥É¾Ò²ð¤½¤Î3¡ÛWhere¤Ç¾ò·ï¤Ë¹ç¤¦¥Ç¡¼¥¿¤ò¼èÆÀ¤¹¤ë
https://www.fenet.jp/dotnet/column/language/1458/


£²¡¥Select ¥á¥½¥Ã¥É

¡¡¼Í±Æ½èÍý¤È¸Æ¤Ð¤ì¤ëµ¡Ç½¤Ç¤¹¡£¼Í±Æ¤È¤Ï¥Ç¡¼¥¿¥Ù¡¼¥¹ÍѤÎÀìÌçÍѸì¤Ç¡¢¥Æ¡¼¥Ö¥ë(¼Â¥Ç¡¼¥¿)¤«¤éÆÃÄê¤ÎÎó¤Î¥Ç¡¼¥¿¤Î¤ß¤ò¼è¤ê½Ð¤¹¤³¤È¤ò¸À¤¤¤Þ¤¹¡£

¡¡Select ¥á¥½¥Ã¥É¤Ç¤Ï°ú¿ô¤Ë¤·¤¿¾ò·ï¤ò¸µ¤Ë¡¢¾È¹ç¤Ç¤­¤¿¥Ç¡¼¥¿¤Î¤ß¤ò¼è¤ê½Ð¤¹Áàºî¤ò¹Ô¤¤¤Þ¤¹¡£
¤³¤Î¥á¥½¥Ã¥É¤ÎÌá¤êÃÍ¤Ï IEnumerable<TResult> ¤Ç¤¢¤ê¡¢¤½¤Î¾ðÊó¤ò¼èÆÀ¤·¤ÆÍøÍѤ·¤¿¤¤¾ì¹ç¤Ë¤Ï¡¢
¥á¥½¥Ã¥É¥Á¥§¡¼¥ó¤òÍøÍѤ·¤Æ¡¢ToList ¥á¥½¥Ã¥É¤ä ToArray ¥á¥½¥Ã¥É¤òÍøÍѤ·¤ÆÊ£¿ô¤Î¾ðÊó¤ò List ¤äÇÛÎó¤È¤·¤Æ°·¤¨¤ë¾õÂÖ¤«
SingleOrDefault ¥á¥½¥Ã¥É¤ä FirstOrDefault ¥á¥½¥Ã¥É¤òÍøÍѤ·¤Æ£±¤Ä¤À¤±°·¤¨¤ë¾õÂ֤ˤ·¤Þ¤¹¡£


        // ¥­¡¼ÆþÎϤˤè¤ë°ÜÆ°Êý¸þ¤Î³Îǧ
        KeyCode keyCode = directionMappings
            .Where(kvp => Input.GetKey(kvp.Key))
            .Select(kvp => kvp.Key)
            .SingleOrDefault();

¡¡¤³¤Á¤é¤Î¼ÂÁõÎã¤Ç¤Ï¡¢Where ¥á¥½¥Ã¥É¤Î½èÍý¤Î·ë²Ì¤ËÂФ·¤Æ¡¢Select ¥á¥½¥Ã¥É¤òÍøÍѤ·¤Æ¤¤¤Þ¤¹¡£
.Select(kvp => kvp.Key) ¤ÎÉôʬ¤Ë¤Ê¤ê¤Þ¤¹¡£

¡¡Where ¥á¥½¥Ã¥É¤ÎÌá¤êÃͤηë²Ì¤Î³ÆÍ×ÁǤò kvp ÊÑ¿ô¤ËÂåÆþ¤·¡¢¤½¤ÎÃ椫¤é Key ÊÑ¿ô¤Î¾ðÊó¤ò¼è¤ê½Ð¤·¤Þ¤¹¡£
º£²ó¤Î¾ì¹ç¡¢Where ¥á¥½¥Ã¥É¤ÎÌá¤êÃͤϣ±¤Ä¤À¤±¤Ç¤¹¤Î¤Ç¡¢¤½¤ÎÌá¤êÃͤËÂФ·¤Æ Key ÊÑ¿ô¤Î¾ðÊó¤ò¼è¤ê½Ð¤·¤Æ¤¤¤Þ¤¹¡£


»²¹Í¥µ¥¤¥È
MicroSoft
Enumerable.Select ¥á¥½¥Ã¥É
https://docs.microsoft.com/ja-jp/dotnet/api/system...
.NET Column ÍÍ
¡ÚLINQ¤ÎSelect¥á¥½¥Ã¥É¤Î½ñ¤­Êý3Áª¡ÃLINQ¤Ë¤Ä¤¤¤Æ¤Ê¤É¤ò¾Ò²ð
https://www.fenet.jp/dotnet/column/language/1454/
Qiita @t_takahari ÍÍ
LINQ¤Î¤½¤ÎForEach¡¢¼Â¤ÏSelect¤Ç½ñ¤­´¹¤¨¤é¤ì¤ë¤«¤â
https://qiita.com/t_takahari/items/6dc72f48b1ebdfe...


£³¡¥SingleOrDefault ¥á¥½¥Ã¥É

¡¡SingleOrDefault() ¤Ï¡¢IEnumerable<T> ¥·¡¼¥±¥ó¥¹¤«¤éñ°ì¤ÎÍ×ÁǤòÊÖ¤¹ LINQ ¥á¥½¥Ã¥É¤Ç¤¹¡£
IEnumerable<T> ¤¬¶õ¤Ç¤¢¤ë¤«¡¢Ê£¿ô¤ÎÍ×ÁǤò»ý¤Ã¤Æ¤¤¤ë¾ì¹ç¤äÍ×ÁǤ¬£±¤Ä¤â¸«¤Ä¤«¤é¤Ê¤¤¾ì¹ç¤Ë¤Ï null ¤òÌᤷ¤Þ¤¹
(Îã³°¥¨¥é¡¼¤Ï½Ð¤Þ¤»¤ó¤Î¤Ç¡¢Àµ¤·¤¯Æ°ºî¤·¤Þ¤¹¡£)


        // °ÜÆ°Êý¸þ¤Î³Îǧ
        KeyCode keyCode = directionMappings
            .Where(kvp => Input.GetKey(kvp.Key))
            .Select(kvp => kvp.Key)
            .SingleOrDefault();

¡¡º£²ó¤Î¾ì¹ç¡¢directionMappings ¤«¤é Input.GetKey() ¤Ë¹çÃפ¹¤ë KeyCode ¤ò¼èÆÀ¤¹¤ëÌÜŪ¤Ç»È¤ï¤ì¤Æ¤¤¤Þ¤¹¡£
Select(kvp => kvp.Key) ¤Ç KeyCode ¤¬£±¤Ä¤Î IEnumerable<KeyCode> ¤Ë¹Ê¤ê¹þ¤Þ¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢
SingleOrDefault() ¤ò»È¤¦¤³¤È¤ÇľÀÜñ°ì¤Î KeyCode ¤ò¼èÆÀ¤Ç¤­¤Þ¤¹¡£

¡¡¤Þ¤¿º£²ó¤ÏÍøÍѤ·¤Æ¤¤¤Þ¤»¤ó¤¬¡¢SingleOrDefault() ¤Ï°ú¿ô¤ò»ØÄꤹ¤ë¤³¤È¤â¤Ç¤­¡¢
¤½¤Î¾ì¹ç¤Ë¤Ï¡¢Ê£¿ô¤ÎÍ×ÁǤ«¤é¾ò·ï¤Ë¹çÃפ¹¤ëÍ×ÁǤò£±¤Ä¤À¤±¼è¤ê½Ð¤¹¤³¤È¤¬½ÐÍè¤Þ¤¹¡£


»²¹Í¥µ¥¤¥È
MicroSoft
Enumerable.SingleOrDefault ¥á¥½¥Ã¥É
https://learn.microsoft.com/ja-jp/dotnet/api/syste...
Dawn Coding Academy ÍÍ
¡ÚC#ÆþÌç¡ÛSingleOrDefault¤Î»È¤¤Êý¤ò²òÀâ¡ÚLINQ¡Û
https://pa-n-da-blog.com/c_linq_singleordefault/



¡¡¤Ê¤ª¡¢¾åµ­¤Î£³¤Ä¤Î LINQ ¤òÍøÍѤ»¤º¤Ë½ñ¤¤¤¿¾ì¹ç¤Ï¡¢°Ê²¼¤Î¤è¤¦¤Ê½èÍý¤Ë¤Ê¤ê¤Þ¤¹¡£


  // °ÜÆ°Êý¸þ¤Î³Îǧ
    KeyCode pressedKeyCode = KeyCode.None;

    // Á´¤Æ¤Î¥­¡¼¤ÈÂбþ¤¹¤ëÊý¸þ¤ò³Îǧ
    foreach (var keyMapping in directionMappings)
    {
        // ¥­¡¼¤¬²¡¤µ¤ì¤Æ¤¤¤ë¤«¤ò³Îǧ
        if (Input.GetKey(keyMapping.Key))
        {
            // ¥­¡¼¤¬²¡¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¡¢¤½¤Î¥­¡¼¥³¡¼¥É¤òµ­Ï¿
            pressedKeyCode = keyMapping.Key;

            // °ì¤Ä¸«¤Ä¤«¤Ã¤¿¤é¥ë¡¼¥×¤ò½ªÎ»
            break;
        }
    }

¡¡LINQ ¤Î½èÍý¤¬¤É¤Î¤è¤¦¤ËÆ°¤¤¤Æ¤¤¤ë¤Î¤«³Îǧ¤Ç¤­¤Þ¤¹¡£
Ʊ»þ¤Ë¡¢¤É¤ì¤À¤±¤Î½èÍý¤¬¾Êά¤µ¤ì¤Æ¤¤¤ë¤«¤âʬ¤«¤ê¤Þ¤¹¡£


£²¡¥ÊªÍý±é»»¤òÍøÍѤ¹¤ë¥±¡¼¥¹


¡¡Rigidbody ¤òÍøÍѤ·¤¿°ÜÆ°¤Î¥±¡¼¥¹¤Ç¤¹¡£
¥­¡¼ÆþÎϤȲóž½èÍý¤Ï Update ¥á¥½¥Ã¥É¡¢°ÜÆ°½èÍý¤Ï FixedUpdate ¥á¥½¥Ã¥É¤Èʬ¤±¤Æ³èÍѤ·¤Þ¤¹¡£


using System.Collections.Generic;
using UnityEngine;
using System.Linq;

[RequireComponent(typeof(Rigidbody))]
public class KeyCodeMappingRigidbodyController : MonoBehaviour
{
    [SerializeField] private float moveSpeed = 1.0f;  // °Üư®ÅÙ

    private Rigidbody rb;
    private KeyCode currentKeyCode = KeyCode.None;   // ¸½ºß²¡¤µ¤ì¤Æ¤¤¤ë¥­¡¼

    // KeyCode ¤È°ÜÆ°Êý¸þ¤ò¥Þ¥Ã¥Ô¥ó¥°
    private Dictionary<KeyCode, Vector3> directionMappings = new()
    {
        { KeyCode.UpArrow, Vector3.up },
        { KeyCode.DownArrow, Vector3.down },
        { KeyCode.LeftArrow, Vector3.left },
        { KeyCode.RightArrow, Vector3.right }
    };

    // KeyCode ¤È²óžÊý¸þ¤ò¥Þ¥Ã¥Ô¥ó¥°
    private Dictionary<KeyCode, Quaternion> rotationMappings = new()
    {
        { KeyCode.UpArrow, Quaternion.Euler(0, 0, 0) },
        { KeyCode.DownArrow, Quaternion.Euler(0, 0, 180) },
        { KeyCode.LeftArrow, Quaternion.Euler(0, 0, 90) },
        { KeyCode.RightArrow, Quaternion.Euler(0, 0, 270) }
    };

    private void Start()
¡¡¡¡{
    ¡¡¡¡if (TryGetComponent(out rb))
    ¡¡¡¡{
        ¡¡¡¡rb.freezeRotation = true; // ²óž¤òʪÍý¥¨¥ó¥¸¥ó¤ËǤ¤»¤Ê¤¤¤è¤¦¤Ë¤¹¤ë
    ¡¡¡¡}
¡¡¡¡}

    private void Update()
    {
        // ¥­¡¼ÆþÎϤˤè¤ë°ÜÆ°Êý¸þ¤Î³Îǧ
        currentKeyCode = GetPressedKeyCode();

    ¡¡¡¡if (currentKeyCode == KeyCode.None)
    ¡¡¡¡{
        ¡¡¡¡return;
    ¡¡¡¡}

        // ²óž¤ÏʪÍý±é»»¤Ç¤Ï¤Ê¤¤¤¿¤á¡¢Update¤Ç¸Æ¤Ó½Ð¤¹
        Turn(currentKeyCode);
    }

    /// <summary>
    /// ¥­¡¼ÆþÎϤˤè¤ë°ÜÆ°Êý¸þ¤Î³Îǧ
    /// </summary>
    /// <returns></returns>
    private KeyCode GetPressedKeyCode()
    {
        return directionMappings
            .Where(kvp => Input.GetKey(kvp.Key))
            .Select(kvp => kvp.Key)
            .SingleOrDefault();
    }

    /// <summary>
    /// ²óž
    /// </summary>
    /// <param name="keyCode"></param>
    private void Turn(KeyCode keyCode)
    {
        if (rotationMappings.TryGetValue(keyCode, out Quaternion rotation))
        {
            // ²óž
            transform.rotation = rotation;
        }
    }

¡¡¡¡private void FixedUpdate()
¡¡¡¡{
    ¡¡¡¡if (currentKeyCode == KeyCode.None)
    ¡¡¡¡{
        ¡¡¡¡return;
    ¡¡¡¡}

    ¡¡¡¡// ʪÍý±é»»¤ËɬÍפʾðÊó¤ò°ú¿ô¤È¤·¤ÆÅϤ¹
    ¡¡¡¡Move(currentKeyCode);
¡¡¡¡}

¡¡¡¡/// <summary>
¡¡¡¡/// °ÜÆ°
¡¡¡¡/// </summary>
¡¡¡¡private void Move(KeyCode keyCode)
¡¡¡¡{
    ¡¡¡¡// °ÜÆ°
    ¡¡¡¡Vector3 velocity = directionMappings[keyCode] * moveSpeed;
    ¡¡¡¡rb.velocity = velocity;
¡¡¡¡}
}

¡¡Áá´ü return ½èÍý¤Ë¤è¤ê¡¢if ʸ¤Ë¤è¤ë¥Í¥¹¥È¤ò̵¤¯¤·¤Æ²ÄÆÉÀ­¤Î¹â¤¤½èÍý¤ò¼Â¸½¤·¤Æ¤¤¤Þ¤¹¡£

    /// <summary>
    /// °ÜÆ°
    /// </summary>
    private void Move(KeyCode keyCode)
    {
        if (keyCode != KeyCode.None)
        {
            // °ÜÆ°
            Vector3 velocity = directionMappings[keyCode] * moveSpeed;
            rb.velocity = velocity;
        }
    }

¡¡¤³¤Î¤è¤¦¤Ê½ñ¼°¤Ç¤Ï¤Ê¤¯¤Æ¡¢»öÁ°¤Ë°ÜÆ°²Äǽ¤Ç¤¢¤ë¤³¤È¤òÀººº¤·¤¿¾å¡¢Move ¥á¥½¥Ã¥É¤Ç¤Ï¼Â¹ÔÌ¿Îá¤À¤±¤òµ­½Ò¤·¤Æ¤¤¤Þ¤¹¡£


¡¡¡¡private void FixedUpdate()
¡¡¡¡{
    ¡¡¡¡if (currentKeyCode == KeyCode.None)¡¡¡¡//¡¡¢«¡¡Move ¤Ç¤­¤ë¤«¥Á¥§¥Ã¥¯¤·¡¢¤Ç¤­¤Ê¤¤¤Ê¤é¡¢¤½¤â¤½¤â Move ¤·¤Ê¤¤
    ¡¡¡¡{
        ¡¡¡¡return;
    ¡¡¡¡}

    ¡¡¡¡// ʪÍý±é»»¤ËɬÍפʾðÊó¤ò°ú¿ô¤È¤·¤ÆÅϤ¹
    ¡¡¡¡Move(currentKeyCode);
¡¡¡¡}

¡¡¡¡/// <summary>
¡¡¡¡/// °ÜÆ°
¡¡¡¡/// </summary>
¡¡¡¡private void Move(KeyCode keyCode)
¡¡¡¡{
    ¡¡¡¡// °ÜÆ°
    ¡¡¡¡Vector3 velocity = directionMappings[keyCode] * moveSpeed;
    ¡¡¡¡rb.velocity = velocity;
¡¡¡¡}

¡¡¥Í¥Ã¥È¤Îµ­»ö¤ä¡¢ChatGPT ¤Î²óÅúÎã¤Î¿¤¯¤Ï if ʸ¤Ë¤è¤ë¥Í¥¹¥È½èÍý¤¬Â¿¤¯¤Ê¤ê¤¬¤Á¤Ç¤¹¡£

¡¡Áá´ü return ¤ò³èÍѤ·¡¢¡Öif ¤Ê¤é¤Ð ¡Á ¤¹¤ë¡×¤È¤¤¤¦½ñ¼°¤À¤±¤Ç¤Ï¤Ê¤¯¡¢¡Öif ¤Ê¤é¤Ð ¡Á ¤·¤Ê¤¤¡×¤È¤¤¤¦½ñ¼°¤Î½ñ¤­Êý¤È³èÍÑË¡¤ò³Ð¤¨¤Æ¤¤¤­¤Þ¤·¤ç¤¦¡£


£³¡¥Input.GetAxis ¥á¥½¥Ã¥É¤òÍøÍѤ·¤¿ÊªÍý±é»»¤Ë¤è¤ë°ÜÆ°½èÍý¤ÎÎã


¡¡¾åµ­¤Î£²¤Ä°Ê³°¤ËÍøÍѤµ¤ì¤Æ¤¤¤ë¡¢°ìÈÌŪ¤Ê¼ÂÁõÎã¤âÄ󼨤·¤Æ¤ª¤­¤Þ¤¹¡£


using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class AxisMappingRigidbodyController : MonoBehaviour
{
    [SerializeField] private float moveSpeed = 1.0f;  // °Üư®ÅÙ

    private Rigidbody rb;
    private float horizontalInput;
    private float verticalInput;

    private void Start()
    {
        if (TryGetComponent(out rb))
        {
            rb.freezeRotation = true; // ²óž¤òʪÍý¥¨¥ó¥¸¥ó¤ËǤ¤»¤Ê¤¤¤è¤¦¤Ë¤¹¤ë
        }
    }

    private void Update()
    {
        // ¿åÊ¿Êý¸þ¤È¿âľÊý¸þ¤ÎÆþÎϤò¼èÆÀ
        horizontalInput = Input.GetAxis("Horizontal");
        verticalInput = Input.GetAxis("Vertical");

        // ²óž¤ÏʪÍý±é»»¤Ç¤Ï¤Ê¤¤¤¿¤á¡¢Update¤Ç¸Æ¤Ó½Ð¤¹
        Turn();
    }

    private void FixedUpdate()
    {
        // ʪÍý±é»»¤ËɬÍפʾðÊó¤ò°ú¿ô¤È¤·¤ÆÅϤ¹
        Move(horizontalInput, verticalInput);
    }

    /// <summary>
    /// °ÜÆ°
    /// </summary>
    private void Move(float horizontal, float vertical)
    {
        // °ÜÆ°
        Vector3 movement = new Vector3(horizontal, 0, vertical) * moveSpeed;
        rb.velocity = movement;
    }

    /// <summary>
    /// ²óž
    /// </summary>
    private void Turn()
    {
        // Êý¸þ¥Ù¥¯¥È¥ë¤òµá¤á¡¢¤½¤ÎÊý¸þ¤ò¸þ¤¯¤è¤¦¤Ë²óž
        if (horizontalInput != 0 || verticalInput != 0)
        {
            Vector3 direction = new Vector3(horizontalInput, 0, verticalInput);
            Quaternion toRotation = Quaternion.LookRotation(direction, Vector3.up);
            transform.rotation = toRotation;
        }
    }
}

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


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

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

Menu


´ðÁÃ

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

private



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

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