この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン | |||
wiki:unity:tips:015 [2015/01/06 14:13] step |
— (現在) | ||
---|---|---|---|
ライン 1: | ライン 1: | ||
- | ====== 015 ユーザー入力を受け取る ====== | ||
- | |||
- | ===== 概要 ===== | ||
- | Inputクラスを使って、ユーザーの入力情報を受け取ります。 | ||
- | |||
- | ==== キーが押されたか? ==== | ||
- | <code csharp> | ||
- | if (Input.GetKeyDown("a")) | ||
- | { | ||
- | Debug.Log("a!"); | ||
- | } | ||
- | if (Input.GetButtonDown("Jump")) | ||
- | { | ||
- | Debug.Log("Jump!"); | ||
- | } | ||
- | if (Input.GetKeyDown("up")) | ||
- | { | ||
- | Debug.Log("up!"); | ||
- | } | ||
- | </code> | ||
- | |||
- | |||
- | ==== キーが押されたか? ==== | ||
- | <code csharp> | ||
- | if (Input.GetKeyUp("b")) | ||
- | { | ||
- | Debug.Log("b!"); | ||
- | } | ||
- | if (Input.GetButtonUp("Fire1")) | ||
- | { | ||
- | Debug.Log("Fire1!"); | ||
- | } | ||
- | </code> | ||
- | |||
- | ==== キーを押し続けているか? ==== | ||
- | <code csharp> | ||
- | if (Input.GetKey("c")) | ||
- | { | ||
- | Debug.Log("c!"); | ||
- | } | ||
- | if (Input.GetButton("Fire2")) | ||
- | { | ||
- | Debug.Log("Fire2!"); | ||
- | } | ||
- | </code> | ||
- | |||
- | ==== 水平方向の入力量(-1.0fから1.0f) ==== | ||
- | <code csharp> | ||
- | float horizontal = Input.GetAxis("Horizontal"); | ||
- | </code> | ||
- | |||
- | ==== 垂直方向の入力量(-1.0fから1.0f) ==== | ||
- | <code csharp> | ||
- | float vertical = Input.GetAxis("Vertical"); | ||
- | </code> | ||
- | |||
- | |||
- | ==== マウス入力 ==== | ||
- | <code csharp> | ||
- | if (Input.GetMouseButtonDown(0)) | ||
- | { | ||
- | Debug.Log("Left Click!"); // 左クリック。 | ||
- | } | ||
- | if (Input.GetMouseButtonDown(1)) | ||
- | { | ||
- | Debug.Log("Right Click!"); // 右クリック。 | ||
- | } | ||
- | if (Input.GetMouseButtonDown(2)) | ||
- | { | ||
- | Debug.Log("Center Click!"); // 中央クリック。 | ||
- | } | ||
- | </code> | ||
- | GetMouseButtonDown( 0 ) はシングルタッチと同等です。 | ||
- | |||
- | ==== マウスの座標を取得 ==== | ||
- | <code csharp> | ||
- | Vector2 mousePos = Input.mousePosition; | ||
- | </code> | ||
- | |||
- | ==== マウスホイールの移動量を取得 ==== | ||
- | <code csharp> | ||
- | float wheel = Input.GetAxis("Mouse ScrollWheel"); | ||
- | </code> | ||
- | |||
- | ==== タッチ入力 ==== | ||
- | <code csharp> | ||
- | for( int i = 0; i < Input.touchCount; i++ ) | ||
- | { | ||
- | Touch touch = Input.GetTouch(i); | ||
- | Vector2 pos = touch.position; | ||
- | |||
- | switch( touch.phase ) | ||
- | { | ||
- | case Began: | ||
- | print( "touch Began!" ); // タッチ開始。 | ||
- | break; | ||
- | |||
- | case Moved: | ||
- | print( "touch Moved!" ); // タッチしていて移動中。 | ||
- | break; | ||
- | |||
- | case Stationary: | ||
- | print( "touch Stationary!" ); // タッチしているが移動していない。 | ||
- | break; | ||
- | |||
- | case Ended: | ||
- | print( "touch Ended!" ); // タッチ終了(離した)。 | ||
- | break; | ||
- | |||
- | case Canceled: // システムによってキャンセルした。 | ||
- | print( "touch Canceled!" ); | ||
- | break; | ||
- | }; | ||
- | } | ||
- | </code> | ||
- | |||
- | ==== 加速度センサーの値 ==== | ||
- | <code csharp> | ||
- | Vector3 lowPassValue = Input.acceleration; | ||
- | </code> | ||
- | |||
- | ==== ジャイロセンサー有効 ==== | ||
- | <code csharp> | ||
- | Input.gyro.enabled = true; | ||
- | </code> | ||
- | |||
- | ==== キーバインドの確認 ==== | ||
- | Jumpなどキーの割り当てはEdit -> Project Settings -> Input で確認できます。 | ||
- | {{:wiki:unity:tips:unity_input_keybind.png?300|}} | ||
- | |||
- | |||
- | [[http://ws.cis.sojo-u.ac.jp/~izumi/Unity_Documentation_jp/Documentation/Manual/Input.html|Unity - Input]] | ||
- | |||
- | |||