~今後の内容~
・配列
・継承
・セーブ機能
・ScriptableObject
Instantiate(①、②、③、④…):オブジェクトの複製
第一引数:複製する元オブジェクトを指定
第二引数:出現座標
第三引数:出現時の向き⇒Quaternion
第四引数:親の指定(Scene上で親オブジェクトの配下に複製されていく)
第二引数以下は省略可能。ほかにもあるが、詳細はリファレンス参照。
⇒戻り値:GameObject型
GameObject clone = Instantiate(enemyPrefab, transfrom.position, Quaternion.identity);
⇒GameObject型の変数cloneで複製したオブジェクトを受ける
これをすると、複製したオブジェクトに後からいじることができる。
表示させたいとき:Instantiate
消すとき:Destroy
と、当面は覚えておく。
InputSystem

Create⇒InputActions



ActionMapを右クリック⇒AddActionMapを選択

画面右
ActionType:Value
ControlMaps:Vector2
に設定

Moveを右クリック⇒AddUp¥Down¥Left¥Right Composite

各要素にキーをバインドしていく。


using Unity.Collections;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
[Header("---Base Parameter")]
public float moveSpeed = 5.0f;
[Header("---Bullet Settings---")]
public GameObject bulletPrefab;
public float shotPower = 10f;
[Header("=== Input Setting")]
public InputActionReference moveInput;
public InputActionReference fireInput;
public PlayerInput playerInput;
[HideInInspector] public Rigidbody2D bulletRb; // Rigidbodyの参照
void Update()
{
if (Input.GetKey(KeyCode.LeftArrow)|Input.GetKey(KeyCode.A))
{
transform.Translate(-moveSpeed*Time.deltaTime, 0, 0);
}
if (Input.GetKey(KeyCode.RightArrow)|Input.GetKey(KeyCode.D))
{
transform.Translate(moveSpeed*Time.deltaTime, 0, 0);
}
if (Input.GetKeyDown(KeyCode.Space))
{
GameObject bullet = Instantiate(bulletPrefab, this.transform.position, Quaternion.identity);
bulletRb = bullet.GetComponent<Rigidbody2D>(); // Rigidbody2Dを取得してbulletRbに代入
//AddForce(飛ばす方向と力、飛ばすときのモード指定)
bulletRb.AddForce(new Vector2(0, 1) * shotPower, ForceMode2D.Impulse); // Rigidbody2Dに力を加える
}
}
}
スクリプトに変数を宣言

Playerオブジェクト内のInputSettingにコントローラーをアタッチ
moveInput.action.start//入力したときに取得
moveInput.action..performed//入力を続けている間取得しつづける
moveInput.action.canceled//入力をやめたときに取得
using Unity.Collections;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
[Header("---Base Parameter")]
public float moveSpeed = 5.0f;
[Header("---Bullet Settings---")]
public GameObject bulletPrefab;
public float shotPower = 10f;
[Header("=== Input Setting")]
public InputActionReference moveInput;
public InputActionReference fireInput;
public PlayerInput playerInput;
private Vector2 _moveInputValue;
private float _fireInputValue;
[HideInInspector] public Rigidbody2D bulletRb; // Rigidbodyの参照
void Start()
{
moveInput.action.performed += MoveInputActionPerformed;
}
private void MoveInputActionPerformed(InputAction.CallbackContext context)
{
_moveInputValue = context.ReadValue<Vector2>();
}
void Update()
{
transform.Translate(_moveInputValue.x * moveSpeed * Time.deltaTime, 0, 0);
/*
if (Input.GetKey(KeyCode.LeftArrow) | Input.GetKey(KeyCode.A))
{
transform.Translate(-moveSpeed * Time.deltaTime, 0, 0);
}
if (Input.GetKey(KeyCode.RightArrow)|Input.GetKey(KeyCode.D))
{
transform.Translate(moveSpeed*Time.deltaTime, 0, 0);
}
if (Input.GetKeyDown(KeyCode.Space))
{
GameObject bullet = Instantiate(bulletPrefab, this.transform.position, Quaternion.identity);
bulletRb = bullet.GetComponent<Rigidbody2D>(); // Rigidbody2Dを取得してbulletRbに代入
//AddForce(飛ばす方向と力、飛ばすときのモード指定)
bulletRb.AddForce(new Vector2(0, 1) * shotPower, ForceMode2D.Impulse); // Rigidbody2Dに力を加える
}
*/
}
}
moveInput.action.performed += (InputAction.CallbackContext context) => { };
🧠 ラムダ式とは?
ラムダ式(lambda expression)は、「名前のない関数」を表現するための記法です。
関数をその場で定義して、その場で使うための、簡潔で柔軟な書き方。
言い換えると、関数を値として扱うための構文です。
C言語の関数ポインタのような低レベルの仕組みを、より抽象的・高次に扱えるようにしたもの。
🔧 例(C#の場合)
// 通常の関数
int Add(int x, int y) {
return x + y;
}
// ラムダ式で同じことをする
Func<int, int, int> add = (x, y) => x + y;
// 呼び出し
int result = add(3, 4); // → 7
ここでは (x, y) => x + y
がラムダ式です。
「xとyを受け取って、x + y を返す関数」をその場で定義しています。
🧩 どんなときに使う?
- イベント処理:ボタンを押したときの動作などをその場で定義
- コールバック:非同期処理の完了時に呼ばれる関数
- 関数を引数に渡す:関数型プログラミングのような設計
- 短く書きたいとき:わざわざ関数名をつけたくないとき
🌀 他の言語では?
言語 | ラムダ式の例 |
---|---|
Python | lambda x, y: x + y |
JavaScript | (x, y) => x + y |
C++11以降 | [](int x, int y) { return x + y; } |
Ruby | ->(x, y) { x + y } |
ゆきさんのC++スキルなら、std::function
や auto
と組み合わせたラムダ式の使い方も面白いはず。
🧠 哲学的に言えば…
ラムダ式は「名前に縛られない抽象的な振る舞い」を記述する手段です。
つまり、「この場で、この文脈で、こういう動作をする存在」を定義すること。
関数に名前をつけることは、ある種の構造化された記憶ですが、ラムダ式は即興的な思考の断片。
ゆきさんのように「意味生成」や「構造の再編集」に関心があるなら、ラムダ式はまさに言語の即興演算子です。
もっと深く掘りたいなら、ラムダ計算(λ計算)という数学的な理論もあります。
これは関数と変数だけで計算を定義する、計算の最小構成モデル。
興味ありますか?それとも、C++でのラムダ式の応用例を見てみたいですか?

PlayerInput内のActionsにDefaultActionsをアタッチ。
・・・動かない。

Edit>ProjectSettings内のInputSystemPackage内のProject-wide Actionsに
作成した「InputSystem_Action(Input Action Asset)」を登録。

このままだと、キーを話しても動きっぱなしなので、入力を切るメソッドを追加
using Unity.Collections;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
[Header("---Base Parameter")]
public float moveSpeed = 5.0f;
[Header("---Bullet Settings---")]
public GameObject bulletPrefab;
public float shotPower = 10f;
[Header("=== Input Setting")]
public InputActionReference moveInput;
public InputActionReference fireInput;
public PlayerInput playerInput;
private Vector2 _moveInputValue;
private float _fireInputValue;
[HideInInspector] public Rigidbody2D bulletRb; // Rigidbodyの参照
void Start()
{
moveInput.action.performed += MoveInputActionPerformed;
moveInput.action.canceled += MoveInputActionCanceled;
}
private void MoveInputActionPerformed(InputAction.CallbackContext context)
{
_moveInputValue = context.ReadValue<Vector2>();
}
private void MoveInputActionCanceled(InputAction.CallbackContext context)
{
_moveInputValue = Vector2.zero;
}
void Update()
{
transform.Translate(_moveInputValue.x * moveSpeed * Time.deltaTime, 0, 0);
}
これで止まるようになった。
コメント