![見出し画像](https://assets.st-note.com/production/uploads/images/70720983/rectangle_large_type_2_534a865931d82ff19d5b9a9f0dbf9f87.png?width=1200)
[Unity Blocks 4]スクリプトを書いてオブジェクトを動的に置く
※先に次回のものを見た方がいいかもしれません・・・
スクリプトを書く
スクリプトのフォルダーを作成して、フォルダー内で作成→C#スクリプトを選択すると少しのコードが書かれているスクリプトファイルができると思います。
この少し書かれているコードの説明を簡単にすると
void Start():プログラムの最初に1回だけ呼び出す関数。(最初にアクティブになったときでも)基本的には初期設定用で使ってます
void Update():マイフレームアクティブな状態であれば実行する関数。常に動き続けるので、重い処理や、1回だけ動かすだけで良い場合は使うべきでない。Startよりも後に実行される。
ここでいうアクティブかどうかはシーンビュー上でオブジェクトがみえていたらアクティブぐらいの認識で大丈夫です。
スクリプトに書き込む内容は以下です
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dot : MonoBehaviour
{
Vector3 SpawnPoint;
void Start()
{
SpawnPoint = this.transform.position;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
GameObject NewPiece = Instantiate((GameObject)Resources.Load("P3-2"));
NewPiece.transform.position = SpawnPoint;
}
}
}
内容的には、
Start:これを追加したオブジェクトの位置を記憶する。
Update:Fキーを押した時に、Resourcesフォルダー内の“P3-2”のオブジェクトを生成し、位置をStartで記憶した位置に置く
です。
ドット用の画像を選択して、インスペクター画面にスクリプトファイルをドラッグ&ドロップすることで、スクリプトをアタッチ(っていうのか?)できます。
動かしてみる
この状態で、プログラムを再生して、Fキーを押してみましょう。
何度も押すと何度も出てきます。
確認が終われば、プログラムを停止させてください。生成したオブジェクトは全て消えてくれます。
まあ画像の中央にドットがないので、いい感じの場所に画像を配置してもいい感じのところにピースが出ません。少しコードを修正します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dot : MonoBehaviour
{
Vector3 SpawnPoint;
Vector3 DotPoint;
void Start()
{
SpawnPoint = new Vector3(-0.065f, 0.3f, 0.065f);
DotPoint = this.transform.position;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
GameObject NewPiece = Instantiate((GameObject)Resources.Load("P3-2"));
NewPiece.transform.position = SpawnPoint;
}
if (Input.GetKeyDown(KeyCode.W))
{
Vector3 diff = new Vector3(0f, 0f, 0.01f);
this.transform.position += diff;
SpawnPoint += diff;
}
if (Input.GetKeyDown(KeyCode.S))
{
Vector3 diff = new Vector3(0f, 0f, -0.01f);
this.transform.position += diff;
SpawnPoint += diff;
}
if (Input.GetKeyDown(KeyCode.D))
{
Vector3 diff = new Vector3(0.01f, 0f, 0f);
this.transform.position += diff;
SpawnPoint += diff;
}
if (Input.GetKeyDown(KeyCode.A))
{
Vector3 diff = new Vector3(-0.01f, 0f, 0f);
this.transform.position += diff;
SpawnPoint += diff;
}
}
}
これでWASDでドットの位置を動かし、その位置に基点となるピースが来るようにオブジェクトが生成されると思います。
コードを見やすく、簡単に書く
これで一旦目標の場所にピースを置くことはできましたが、同じようなコードが続いているので、関数を使って簡単に書いてみます。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dot : MonoBehaviour
{
Vector3 SpawnPoint;
Vector3 DotPoint;
void Start()
{
SpawnPoint = new Vector3(-0.065f, 0.3f, 0.065f);
DotPoint = this.transform.position;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
GameObject NewPiece = Instantiate((GameObject)Resources.Load("P3-2"));
NewPiece.transform.position = SpawnPoint;
}
if (Input.GetKeyDown(KeyCode.W))
MoveSpawnPoint(new Vector3(0f, 0f, 0.01f));
if (Input.GetKeyDown(KeyCode.S))
MoveSpawnPoint(new Vector3(0f, 0f, -0.01f));
if (Input.GetKeyDown(KeyCode.D))
MoveSpawnPoint(new Vector3(0.01f, 0f, 0f));
if (Input.GetKeyDown(KeyCode.A))
MoveSpawnPoint(new Vector3(-0.01f, 0f, 0f));
}
void MoveSpawnPoint(Vector3 diff)
{
this.transform.position += diff;
SpawnPoint += diff;
}
}
多少はすっきり見えると思います。本当はswitch文を使いたいですが、キーの判定では少しめんどくさいみたいです。
もし、WASDを押した時に新たに動きを追加する場合でもこれなら4行追加でなく、MoveSpawnPoint関数に1行の追加で大丈夫です。また、「4行追加するつもりが、3行だけしか追加していなかった」なんてミスによるバグも減らせます。
このように関数を使ってできるだけ共通の処理を一つにまとめたりすることがゲームを拡張していく上で大切なので意識してください(自戒)