ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
| この記事の要点 |
|
Old Input Manager の基本
Unity 標準搭載 (Project Settings → Player → Active Input Handling で切り替え可能)。最も簡単なキーボード入力取得方法:
using UnityEngine;
public class PlayerInput : MonoBehaviour
{
void Update()
{
// 押されている間 true
if (Input.GetKey(KeyCode.W))
{
Debug.Log("W キー押下中");
}
// 押された瞬間だけ true (1 フレームだけ)
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("ジャンプ!");
}
// 離された瞬間だけ true
if (Input.GetKeyUp(KeyCode.Space))
{
Debug.Log("ジャンプキー離した");
}
// 修飾キー併用
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.W))
{
Debug.Log("Shift + W ダッシュ");
}
}
}
KeyCode 一覧
| カテゴリ | KeyCode |
|---|---|
| アルファベット | KeyCode.A 〜 KeyCode.Z |
| 数字 | KeyCode.Alpha0 〜 KeyCode.Alpha9 (上段)、KeyCode.Keypad0 〜 (テンキー) |
| 矢印 | KeyCode.UpArrow / DownArrow / LeftArrow / RightArrow |
| 修飾 | KeyCode.LeftShift / RightShift / LeftControl / LeftAlt / LeftCommand |
| スペース等 | KeyCode.Space / Return / Escape / Tab / Backspace |
| ファンクション | KeyCode.F1 〜 KeyCode.F15 |
| マウス | KeyCode.Mouse0 〜 Mouse6 |
Input.GetAxis: 軸入力 (移動)
キーボード上下左右 や WASD を「-1.0 〜 +1.0」の浮動小数で取得できます。スムージング付き:
public class PlayerMover : MonoBehaviour
{
public float speed = 5f;
void Update()
{
// Project Settings → Input Manager で定義済の軸
float h = Input.GetAxis("Horizontal"); // A/D, ←/→
float v = Input.GetAxis("Vertical"); // W/S, ↑/↓
// GetAxisRaw はスムージング無し (-1, 0, 1 のみ)
float hRaw = Input.GetAxisRaw("Horizontal");
Vector3 move = new Vector3(h, 0, v) * speed * Time.deltaTime;
transform.Translate(move);
}
}
Update vs FixedUpdate
入力検出は Update() で、物理オブジェクト (Rigidbody) の操作は FixedUpdate() で行うのが鉄則です:
public class PhysicsMover : MonoBehaviour
{
private Rigidbody rb;
private bool jumpRequested = false;
private Vector3 moveInput;
void Start()
{
rb = GetComponent();
}
void Update()
{
// ★ 入力は Update で (取りこぼし防止)
moveInput = new Vector3(
Input.GetAxisRaw("Horizontal"),
0,
Input.GetAxisRaw("Vertical")
);
if (Input.GetKeyDown(KeyCode.Space))
jumpRequested = true; // フラグだけ立てる
}
void FixedUpdate()
{
// ★ 物理処理は FixedUpdate で
rb.MovePosition(rb.position + moveInput * 5f * Time.fixedDeltaTime);
if (jumpRequested)
{
rb.AddForce(Vector3.up * 5f, ForceMode.Impulse);
jumpRequested = false;
}
}
}
新 Input System (推奨)
Unity 2019.1+ で Package Manager からインストール可能。Unity 2022.3 LTS 以降は標準的な選択肢です。
# インストール手順
# 1. Window → Package Manager → Unity Registry
# 2. "Input System" を選択 → Install
# 3. Project Settings → Player → Active Input Handling
# → "Input System Package (New)" または "Both"
# 4. Unity 再起動
方式 A: 直接呼び出し (Keyboard.current)
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerNew : MonoBehaviour
{
void Update()
{
// 現在のキーボード
var kb = Keyboard.current;
if (kb == null) return;
if (kb.wKey.isPressed)
Debug.Log("W 押下中");
if (kb.spaceKey.wasPressedThisFrame)
Debug.Log("Space 押された瞬間");
if (kb.escapeKey.wasReleasedThisFrame)
Debug.Log("Esc 離した");
}
}
方式 B: Input Action Asset (推奨)
Project ウィンドウで Create → Input Actions → 「PlayerControls」 を作成。エディタでアクションを定義し、コードからは抽象的に呼び出します:
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerActionBased : MonoBehaviour
{
public InputActionAsset actions;
private InputAction moveAction;
private InputAction jumpAction;
void OnEnable()
{
moveAction = actions.FindAction("Move");
jumpAction = actions.FindAction("Jump");
jumpAction.performed += OnJump;
moveAction.Enable();
jumpAction.Enable();
}
void OnDisable()
{
jumpAction.performed -= OnJump;
moveAction.Disable();
jumpAction.Disable();
}
void Update()
{
Vector2 m = moveAction.ReadValue();
transform.Translate(new Vector3(m.x, 0, m.y) * Time.deltaTime * 5f);
}
private void OnJump(InputAction.CallbackContext ctx)
{
Debug.Log("Jump triggered!");
}
}
Old vs New Input System
| 項目 | Old Input Manager | New Input System |
|---|---|---|
| 呼び出し | Input.GetKey(KeyCode.W) | Keyboard.current.wKey.isPressed / Action |
| キーバインド変更 | 面倒 (要再起動) | ランタイム変更可 |
| コントローラ対応 | 限定的 | 多様な機種に対応 |
| マルチプレイヤー | 困難 | PlayerInputManager で容易 |
| イベント駆動 | × | ○ (callbacks) |
| 学習コスト | 低 | 中 |
| 推奨度 | レガシー / 互換用途 | 新規プロジェクト |
SendMessage 方式 (PlayerInput コンポーネント)
// GameObject に PlayerInput コンポーネントを付与し
// "Behavior" を "Send Messages" に設定すると
// 命名規則でメソッドが自動呼び出しされる
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerSendMessage : MonoBehaviour
{
public void OnMove(InputValue value)
{
Vector2 v = value.Get();
Debug.Log($"Move: {v}");
}
public void OnJump()
{
Debug.Log("Jump");
}
public void OnFire(InputValue value)
{
if (value.isPressed) Debug.Log("Fire pressed");
}
}
FAQ
Q: Input.GetKeyDown が反応しない
A: FixedUpdate に書いていないか確認 (1 フレーム入力は Update でしか捕捉できない)。または新 Input System に切り替え済で Old を呼んでいるとエラー。
Q: 日本語キーボード固有のキー
A: KeyCode に半角全角等は無いが、新 Input System なら Keyboard.current.imeSelected 等が利用可能。
Q: 複数プレイヤーで別キーボード対応
A: 新 Input System の PlayerInputManager + Player Input Action Asset を使う。
ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
子ページはありません
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript base URL 取得方法|window.location.origin と SSR/Node.js 対応
- 4 YouTube Data API v3 エラー一覧|403/400/404 の主要原因と切り分け
- 5 Spring Frameworkのアノテーション一覧
- 6 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 7 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- Laravel キャッシュクリア完全ガイド(cache:clear / config:clear / 2026-05-18 07:42:07
- プロジェクトの作成と削除 2026-05-18 07:42:07
- インストール直後にNetbeansが反応しない 2026-05-18 07:42:07
- 動画やチャンネルの検索 2026-05-18 07:42:07
- APIキー取得方法 2026-05-18 07:42:07
- チャンネル情報の取得 2026-05-18 07:42:07
- API 入門 — Web API(REST / GraphQL / gRPC / 2026-05-18 07:42:07
- インストール(eclipseプラグイン) 2026-05-18 07:42:07
- Laravel「Dotenv values containing spaces must be surrounded 2026-05-18 07:42:07
- エラー一覧 2026-05-18 07:42:07
- curl: (51) SSL: certificate subject name '~' does not match 2026-05-18 07:42:07
- インストール方法(Windows版) 2026-05-18 07:42:07
- JSONから配列に変換 2026-05-18 07:42:07
- 処理を一定時間待つ 2026-05-18 07:42:07
- A non well formed numeric value encountered 2026-05-18 07:42:07
コメントを削除してもよろしいでしょうか?