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; } } }
// 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) } };
// キー入力による移動方向の確認 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); }
// 移動 position += directionMappings[keyCode] * moveSpeed;
// 移動(KeyCode.UpArrow のとき) position += Vector3.up * moveSpeed;
using System.Linq;
SamuraiBlog様
【C#入門】LINQの使い方総まとめ(Select、Where、GroupByなど)
https://www.sejuku.net/blog/56519
地平線に行く様
LINQの拡張メソッド一覧と、ほぼ全部のサンプルを作ってみました。
https://yujisoftware.hatenablog.com/entry/20111031...
// キー入力による移動方向の確認 KeyCode keyCode = directionMappings .Where(kvp => Input.GetKey(kvp.Key)) .Select(kvp => kvp.Key) .SingleOrDefault();
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/
// キー入力による移動方向の確認 KeyCode keyCode = directionMappings .Where(kvp => Input.GetKey(kvp.Key)) .Select(kvp => kvp.Key) .SingleOrDefault();
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...
// 移動方向の確認 KeyCode keyCode = directionMappings .Where(kvp => Input.GetKey(kvp.Key)) .Select(kvp => kvp.Key) .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/
// 移動方向の確認 KeyCode pressedKeyCode = KeyCode.None; // 全てのキーと対応する方向を確認 foreach (var keyMapping in directionMappings) { // キーが押されているかを確認 if (Input.GetKey(keyMapping.Key)) { // キーが押されている場合、そのキーコードを記録 pressedKeyCode = keyMapping.Key; // 一つ見つかったらループを終了 break; } }
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; } }
/// <summary> /// 移動 /// </summary> private void Move(KeyCode keyCode) { if (keyCode != KeyCode.None) { // 移動 Vector3 velocity = directionMappings[keyCode] * moveSpeed; rb.velocity = velocity; } }
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; }
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; } } }